@@ -25,14 +25,16 @@ import SwiftSyntaxBuilder
25
25
import JavaKitConfigurationShared
26
26
import JavaKitShared
27
27
28
+ protocol HasCommonOptions {
29
+ var commonOptions : SwiftJava . CommonOptions { get set }
30
+ }
31
+
32
+ protocol HasCommonJVMOptions {
33
+ var commonJVMOptions : SwiftJava . CommonJVMOptions { get set }
34
+ }
35
+
28
36
extension SwiftJava {
29
37
struct CommonOptions : ParsableArguments {
30
- @Option (
31
- name: [ . customLong( " cp " ) , . customLong( " classpath " ) ] ,
32
- help: " Class search path of directories and zip/jar files from which Java classes can be loaded. "
33
- )
34
- var classpath : [ String ] = [ ]
35
-
36
38
// TODO: clarify this vs outputSwift (history: outputSwift is jextract, and this was java2swift)
37
39
@Option ( name: . shortAndLong, help: " The directory in which to output the generated Swift files or the SwiftJava configuration file. " )
38
40
var outputDirectory : String ? = nil
@@ -43,7 +45,14 @@ extension SwiftJava {
43
45
@Option ( name: . shortAndLong, help: " Configure the level of logs that should be printed " )
44
46
var logLevel : Logger . Level = . info
45
47
}
48
+
46
49
struct CommonJVMOptions : ParsableArguments {
50
+ @Option (
51
+ name: [ . customLong( " cp " ) , . customLong( " classpath " ) ] ,
52
+ help: " Class search path of directories and zip/jar files from which Java classes can be loaded. "
53
+ )
54
+ var classpath : [ String ] = [ ]
55
+
47
56
@Option ( name: . shortAndLong, help: " While scanning a classpath, inspect only types included in this package " )
48
57
var filterJavaPackage : String ? = nil
49
58
}
@@ -60,6 +69,46 @@ protocol SwiftJavaBaseAsyncParsableCommand: AsyncParsableCommand {
60
69
61
70
}
62
71
72
+ extension SwiftJavaBaseAsyncParsableCommand {
73
+ mutating func writeContents(
74
+ _ contents: String ,
75
+ to filename: String , description: String ) throws {
76
+ try writeContents (
77
+ contents,
78
+ outputDirectoryOverride: self . actualOutputDirectory,
79
+ to: filename,
80
+ description: description)
81
+ }
82
+
83
+ mutating func writeContents(
84
+ _ contents: String ,
85
+ outputDirectoryOverride: Foundation . URL ? ,
86
+ to filename: String ,
87
+ description: String ) throws {
88
+ guard let outputDir = ( outputDirectoryOverride ?? actualOutputDirectory) else {
89
+ print ( " // \( filename) - \( description) " )
90
+ print ( contents)
91
+ return
92
+ }
93
+
94
+ // If we haven't tried to create the output directory yet, do so now before
95
+ // we write any files to it.
96
+ // if !createdOutputDirectory {
97
+ try FileManager . default. createDirectory (
98
+ at: outputDir,
99
+ withIntermediateDirectories: true
100
+ )
101
+ // createdOutputDirectory = true
102
+ //}
103
+
104
+ // Write the file:
105
+ let file = outputDir. appendingPathComponent ( filename)
106
+ print ( " [debug][swift-java] Writing \( description) to ' \( file. path) '... " , terminator: " " )
107
+ try contents. write ( to: file, atomically: true , encoding: . utf8)
108
+ print ( " done. " . green)
109
+ }
110
+ }
111
+
63
112
extension SwiftJavaBaseAsyncParsableCommand {
64
113
public mutating func run( ) async {
65
114
print ( " [info][swift-java] Run: \( CommandLine . arguments. joined ( separator: " " ) ) " )
@@ -168,33 +217,36 @@ extension SwiftJavaBaseAsyncParsableCommand {
168
217
}
169
218
170
219
extension SwiftJava {
171
- struct ConfigureCommand : SwiftJavaBaseAsyncParsableCommand {
220
+ struct ConfigureCommand : SwiftJavaBaseAsyncParsableCommand , HasCommonOptions , HasCommonJVMOptions {
172
221
static let configuration = CommandConfiguration (
173
222
commandName: " configure " ,
174
223
abstract: " Configure and emit a swift-java.config file based on an input dependency or jar file " )
175
224
176
225
// TODO: This should be a "make wrappers" option that just detects when we give it a jar
177
226
@Flag (
178
- help:
179
- " Specifies that the input is a Jar file whose public classes will be loaded. The output of Java2Swift will be a configuration file (Java2Swift.config) that can be used as input to a subsequent Java2Swift invocation to generate wrappers for those public classes. "
227
+ help: " Specifies that the input is a *.jar file whose public classes will be loaded. The output of swift-java will be a configuration file (swift-java.config) that can be used as input to a subsequent swift-java invocation to generate wrappers for those public classes. "
180
228
)
181
229
var jar : Bool = false
182
230
231
+ @Option (
232
+ name: . long,
233
+ help: " How to handle an existing swift-java.config; by default 'overwrite' by can be changed to amending a configuration "
234
+ )
235
+ var existingConfigFile : ExistingConfigFileMode = . overwrite
236
+ enum ExistingConfigFileMode : String , ExpressibleByArgument , Codable {
237
+ case overwrite
238
+ case amend
239
+ }
240
+
183
241
// FIXME: is it used?
184
242
@Option ( help: " The name of the Swift module into which the resulting Swift types will be generated. " )
185
243
var moduleName : String ? // TODO: rename to --swift-module?
186
244
187
- @Option (
188
- name: [ . customLong( " cp " ) , . customLong( " classpath " ) ] ,
189
- help: " Class search path of directories and zip/jar files from which Java classes can be loaded. "
190
- )
191
- var classpath : [ String ] = [ ]
192
-
193
245
@OptionGroup var commonOptions : SwiftJava . CommonOptions
194
246
@OptionGroup var commonJVMOptions : SwiftJava . CommonJVMOptions
195
247
196
248
@Argument (
197
- help: " The input file, which is either a Java2Swift configuration file or (if '-jar' was specified) a Jar file. "
249
+ help: " The input file, which is either a swift-java configuration file or (if '-jar' was specified) a Jar file. "
198
250
)
199
251
var input : String = " "
200
252
}
@@ -204,7 +256,7 @@ extension SwiftJava.ConfigureCommand {
204
256
mutating func runSwiftJavaCommand( config: inout Configuration ) async throws {
205
257
// Form a class path from all of our input sources:
206
258
// * Command-line option --classpath
207
- let classpathOptionEntries : [ String ] = classpath. flatMap { $0. split ( separator: " : " ) . map ( String . init) }
259
+ let classpathOptionEntries : [ String ] = self . commonJVMOptions . classpath. flatMap { $0. split ( separator: " : " ) . map ( String . init) }
208
260
let classpathFromEnv = ProcessInfo . processInfo. environment [ " CLASSPATH " ] ? . split ( separator: " : " ) . map ( String . init) ?? [ ]
209
261
let classpathFromConfig : [ String ] = config. classpath? . split ( separator: " : " ) . map ( String . init) ?? [ ]
210
262
print ( " [debug][swift-java] Base classpath from config: \( classpathFromConfig) " )
@@ -239,7 +291,7 @@ extension SwiftJava.ConfigureCommand {
239
291
}
240
292
let jvm = try JavaVirtualMachine . shared ( classpath: classpathEntries)
241
293
242
- try emitConfiguration ( classpath: classpath, environment: jvm. environment ( ) )
294
+ try emitConfiguration ( classpath: self . commonJVMOptions . classpath, environment: jvm. environment ( ) )
243
295
}
244
296
245
297
/// Get base configuration, depending on if we are to 'amend' or 'overwrite' the existing configuration.
@@ -249,7 +301,7 @@ extension SwiftJava.ConfigureCommand {
249
301
return ( false , . init( ) )
250
302
}
251
303
252
- switch self . existingConfig {
304
+ switch self . existingConfigFile {
253
305
case . overwrite:
254
306
// always make up a fresh instance if we're overwriting
255
307
return ( false , . init( ) )
0 commit comments