Skip to content

Commit 88ccc11

Browse files
committed
move the configure command into its own file/subcommand
1 parent fe40044 commit 88ccc11

File tree

6 files changed

+74
-68
lines changed

6 files changed

+74
-68
lines changed

.swift-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.1.2

Sources/SwiftJavaTool/CommonOptions.swift

Whitespace-only changes.

Sources/SwiftJavaTool/SwiftJava+EmitConfiguration.swift

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ import SwiftSyntaxBuilder
2525
import JavaKitConfigurationShared
2626
import JavaKitShared
2727

28+
protocol HasCommonOptions {
29+
var commonOptions: SwiftJava.CommonOptions { get set }
30+
}
31+
32+
protocol HasCommonJVMOptions {
33+
var commonJVMOptions: SwiftJava.CommonJVMOptions { get set }
34+
}
35+
2836
extension SwiftJava {
2937
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-
3638
// TODO: clarify this vs outputSwift (history: outputSwift is jextract, and this was java2swift)
3739
@Option(name: .shortAndLong, help: "The directory in which to output the generated Swift files or the SwiftJava configuration file.")
3840
var outputDirectory: String? = nil
@@ -43,7 +45,14 @@ extension SwiftJava {
4345
@Option(name: .shortAndLong, help: "Configure the level of logs that should be printed")
4446
var logLevel: Logger.Level = .info
4547
}
48+
4649
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+
4756
@Option(name: .shortAndLong, help: "While scanning a classpath, inspect only types included in this package")
4857
var filterJavaPackage: String? = nil
4958
}
@@ -60,6 +69,46 @@ protocol SwiftJavaBaseAsyncParsableCommand: AsyncParsableCommand {
6069

6170
}
6271

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+
63112
extension SwiftJavaBaseAsyncParsableCommand {
64113
public mutating func run() async {
65114
print("[info][swift-java] Run: \(CommandLine.arguments.joined(separator: " "))")
@@ -168,33 +217,36 @@ extension SwiftJavaBaseAsyncParsableCommand {
168217
}
169218

170219
extension SwiftJava {
171-
struct ConfigureCommand: SwiftJavaBaseAsyncParsableCommand {
220+
struct ConfigureCommand: SwiftJavaBaseAsyncParsableCommand, HasCommonOptions, HasCommonJVMOptions {
172221
static let configuration = CommandConfiguration(
173222
commandName: "configure",
174223
abstract: "Configure and emit a swift-java.config file based on an input dependency or jar file")
175224

176225
// TODO: This should be a "make wrappers" option that just detects when we give it a jar
177226
@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."
180228
)
181229
var jar: Bool = false
182230

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+
183241
// FIXME: is it used?
184242
@Option(help: "The name of the Swift module into which the resulting Swift types will be generated.")
185243
var moduleName: String? // TODO: rename to --swift-module?
186244

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-
193245
@OptionGroup var commonOptions: SwiftJava.CommonOptions
194246
@OptionGroup var commonJVMOptions: SwiftJava.CommonJVMOptions
195247

196248
@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."
198250
)
199251
var input: String = ""
200252
}
@@ -204,7 +256,7 @@ extension SwiftJava.ConfigureCommand {
204256
mutating func runSwiftJavaCommand(config: inout Configuration) async throws {
205257
// Form a class path from all of our input sources:
206258
// * 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) }
208260
let classpathFromEnv = ProcessInfo.processInfo.environment["CLASSPATH"]?.split(separator: ":").map(String.init) ?? []
209261
let classpathFromConfig: [String] = config.classpath?.split(separator: ":").map(String.init) ?? []
210262
print("[debug][swift-java] Base classpath from config: \(classpathFromConfig)")
@@ -239,7 +291,7 @@ extension SwiftJava.ConfigureCommand {
239291
}
240292
let jvm = try JavaVirtualMachine.shared(classpath: classpathEntries)
241293

242-
try emitConfiguration(classpath: classpath, environment: jvm.environment())
294+
try emitConfiguration(classpath: self.commonJVMOptions.classpath, environment: jvm.environment())
243295
}
244296

245297
/// Get base configuration, depending on if we are to 'amend' or 'overwrite' the existing configuration.
@@ -249,7 +301,7 @@ extension SwiftJava.ConfigureCommand {
249301
return (false, .init())
250302
}
251303

252-
switch self.existingConfig {
304+
switch self.existingConfigFile {
253305
case .overwrite:
254306
// always make up a fresh instance if we're overwriting
255307
return (false, .init())

Sources/SwiftJavaTool/SwiftJava+FetchDependencies.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ extension SwiftJava {
137137
// The file contents are just plain
138138
let contents = resolvedClasspath.classpath
139139

140-
print("[debug][swift-java] Resolved dependency: \(commonOptions.classpath)")
140+
print("[debug][swift-java] Resolved dependency: \(commonJVMOptions.classpath)")
141141

142142
// Write the file
143143
try writeContents(

Sources/SwiftJavaTool/SwiftJava.swift

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,6 @@ struct SwiftJava: SwiftJavaBaseAsyncParsableCommand {
103103
return nil
104104
}
105105
}
106-
107-
@Option(name: .shortAndLong, help: "How to handle an existing swift-java.config; by default 'overwrite' by can be changed to amending a configuration")
108-
var existingConfig: ExistingConfigFileMode = .overwrite
109-
public enum ExistingConfigFileMode: String, ExpressibleByArgument, Codable {
110-
case overwrite
111-
case amend
112-
}
113106

114107
// @Option(name: .shortAndLong, help: "While scanning a classpath, inspect only types included in this package")
115108
// var javaPackageFilter: String? = nil
@@ -119,9 +112,6 @@ struct SwiftJava: SwiftJavaBaseAsyncParsableCommand {
119112
// )
120113
var input: String? // FIXME: top level command cannot have input argument like this
121114

122-
/// Whether we have ensured that the output directory exists.
123-
var createdOutputDirectory: Bool = false
124-
125115
// FIXME: this is subcommands
126116
/// Describes what kind of generation action is being performed by swift-java.
127117
enum ToolMode {
@@ -231,7 +221,7 @@ struct SwiftJava: SwiftJavaBaseAsyncParsableCommand {
231221

232222
// Form a class path from all of our input sources:
233223
// * Command-line option --classpath
234-
let classpathOptionEntries: [String] = commonOptions.classpath.flatMap { $0.split(separator: ":").map(String.init) }
224+
let classpathOptionEntries: [String] = commonJVMOptions.classpath.flatMap { $0.split(separator: ":").map(String.init) }
235225
let classpathFromEnv = ProcessInfo.processInfo.environment["CLASSPATH"]?.split(separator: ":").map(String.init) ?? []
236226
let classpathFromConfig: [String] = config.classpath?.split(separator: ":").map(String.init) ?? []
237227
print("[debug][swift-java] Base classpath from config: \(classpathFromConfig)")
@@ -368,43 +358,6 @@ struct SwiftJava: SwiftJavaBaseAsyncParsableCommand {
368358
return (javaClassName, swiftName.javaClassNameToCanonicalName)
369359
}
370360

371-
mutating func writeContents(
372-
_ contents: String,
373-
to filename: String, description: String) throws {
374-
try writeContents(
375-
contents,
376-
outputDirectoryOverride: self.actualOutputDirectory,
377-
to: filename,
378-
description: description)
379-
}
380-
381-
mutating func writeContents(
382-
_ contents: String,
383-
outputDirectoryOverride: Foundation.URL?,
384-
to filename: String,
385-
description: String) throws {
386-
guard let outputDir = (outputDirectoryOverride ?? actualOutputDirectory) else {
387-
print("// \(filename) - \(description)")
388-
print(contents)
389-
return
390-
}
391-
392-
// If we haven't tried to create the output directory yet, do so now before
393-
// we write any files to it.
394-
if !createdOutputDirectory {
395-
try FileManager.default.createDirectory(
396-
at: outputDir,
397-
withIntermediateDirectories: true
398-
)
399-
createdOutputDirectory = true
400-
}
401-
402-
// Write the file:
403-
let file = outputDir.appendingPathComponent(filename)
404-
print("[debug][swift-java] Writing \(description) to '\(file.path)'... ", terminator: "")
405-
try contents.write(to: file, atomically: true, encoding: .utf8)
406-
print("done.".green)
407-
}
408361
}
409362

410363
enum JavaToSwiftError: Error {

Sources/SwiftJavaTool/SwiftJavaBaseAsyncParsableCommand.swift

Whitespace-only changes.

0 commit comments

Comments
 (0)