Skip to content

Commit 1c30288

Browse files
committed
Refactor to converge many command line tools into a single "swift-java"
And we'll do subcommands instead.
1 parent cb4fa3d commit 1c30288

22 files changed

+60
-89
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ DerivedData/
1111
*.class
1212
bin/
1313
BuildLogic/out/
14+
**/.index-build/
15+
.build-vscode/
1416

1517
# Ignore gradle build artifacts
1618
.gradle

Makefile

+10-10
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,29 @@ SAMPLES_DIR := "Samples"
5656
all:
5757
@echo "Welcome to swift-java! There are several makefile targets to choose from:"
5858
@echo " javakit-run: Run the JavaKit example program that uses Java libraries from Swift."
59-
@echo " javakit-generate: Regenerate the Swift wrapper code for the various JavaKit libraries from Java. This only has to be done when changing the Java2Swift tool."
59+
@echo " javakit-generate: Regenerate the Swift wrapper code for the various JavaKit libraries from Java. This only has to be done when changing the SwiftJava tool."
6060

61-
$(BUILD_DIR)/debug/libJavaKit.$(LIB_SUFFIX) $(BUILD_DIR)/debug/Java2Swift:
61+
$(BUILD_DIR)/debug/libJavaKit.$(LIB_SUFFIX) $(BUILD_DIR)/debug/SwiftJava:
6262
swift build
6363

6464
javakit-run:
6565
cd Samples/JavaKitSampleApp && swift build && java -cp .build/plugins/outputs/javakitsampleapp/JavaKitExample/destination/JavaCompilerPlugin/Java -Djava.library.path=.build/debug com.example.swift.JavaKitSampleMain
6666

67-
Java2Swift: $(BUILD_DIR)/debug/Java2Swift
67+
SwiftJava: $(BUILD_DIR)/debug/SwiftJava
6868

69-
generate-JavaKit: Java2Swift
69+
generate-JavaKit: SwiftJava
7070
mkdir -p Sources/JavaKit/generated
71-
$(BUILD_DIR)/debug/Java2Swift --module-name JavaKit -o Sources/JavaKit/generated Sources/JavaKit/swift-java.config
71+
$(BUILD_DIR)/debug/SwiftJava --module-name JavaKit -o Sources/JavaKit/generated Sources/JavaKit/swift-java.config
7272

73-
generate-JavaKitCollection: Java2Swift
73+
generate-JavaKitCollection: SwiftJava
7474
mkdir -p Sources/JavaKitCollection/generated
75-
$(BUILD_DIR)/debug/Java2Swift --module-name JavaKitCollection --depends-on JavaKit=Sources/JavaKit/swift-java.config -o Sources/JavaKitCollection/generated Sources/JavaKitCollection/swift-java.config
75+
$(BUILD_DIR)/debug/SwiftJava --module-name JavaKitCollection --depends-on JavaKit=Sources/JavaKit/swift-java.config -o Sources/JavaKitCollection/generated Sources/JavaKitCollection/swift-java.config
7676

77-
generate-JavaKitFunction: Java2Swift
77+
generate-JavaKitFunction: SwiftJava
7878
mkdir -p Sources/JavaKitFunction/generated
79-
$(BUILD_DIR)/debug/Java2Swift --module-name JavaKitFunction --depends-on JavaKit=Sources/JavaKit/swift-java.config -o Sources/JavaKitFunction/generated Sources/JavaKitFunction/swift-java.config
79+
$(BUILD_DIR)/debug/SwiftJava --module-name JavaKitFunction --depends-on JavaKit=Sources/JavaKit/swift-java.config -o Sources/JavaKitFunction/generated Sources/JavaKitFunction/swift-java.config
8080

81-
generate-JavaKitReflection: Java2Swift generate-JavaKit generate-JavaKitCollection
81+
generate-JavaKitReflection: SwiftJava generate-JavaKit generate-JavaKitCollection
8282
mkdir -p Sources/JavaKitReflection/generated
8383
$(BUILD_DIR)/debug/Java2Swift --module-name JavaKitReflection --depends-on JavaKit=Sources/JavaKit/swift-java.config --depends-on JavaKitCollection=Sources/JavaKitCollection/swift-java.config -o Sources/JavaKitReflection/generated Sources/JavaKitReflection/swift-java.config
8484

Package.swift

+14-21
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ let package = Package(
8787
),
8888

8989
.executable(
90-
name: "Java2Swift",
91-
targets: ["Java2Swift"]
90+
name: "SwiftJavaTool",
91+
targets: ["SwiftJavaTool"]
9292
),
9393

9494
// ==== Plugin for building Java code
@@ -101,17 +101,17 @@ let package = Package(
101101

102102
// ==== Plugin for wrapping Java classes in Swift
103103
.plugin(
104-
name: "Java2SwiftPlugin",
104+
name: "SwiftJavaPlugin",
105105
targets: [
106-
"Java2SwiftPlugin"
106+
"SwiftJavaPlugin"
107107
]
108108
),
109109

110110
// ==== jextract-swift (extract Java accessors from Swift interface files)
111111

112112
.executable(
113113
name: "jextract-swift",
114-
targets: ["JExtractSwiftTool"]
114+
targets: ["SwiftJavaTool"]
115115
),
116116

117117
// Support library written in Swift for SwiftKit "Java"
@@ -257,10 +257,10 @@ let package = Package(
257257
),
258258

259259
.plugin(
260-
name: "Java2SwiftPlugin",
260+
name: "SwiftJavaPlugin",
261261
capability: .buildTool(),
262262
dependencies: [
263-
"Java2Swift"
263+
"SwiftJavaTool"
264264
]
265265
),
266266

@@ -320,7 +320,7 @@ let package = Package(
320320
),
321321

322322
.executableTarget(
323-
name: "Java2Swift",
323+
name: "SwiftJavaTool",
324324
dependencies: [
325325
.product(name: "SwiftBasicFormat", package: "swift-syntax"),
326326
.product(name: "SwiftSyntax", package: "swift-syntax"),
@@ -330,6 +330,7 @@ let package = Package(
330330
"JavaKitJar",
331331
"JavaKitNetwork",
332332
"Java2SwiftLib",
333+
"JExtractSwift", // TODO: Swift2JavaLib
333334
"JavaKitShared",
334335
],
335336

@@ -356,21 +357,11 @@ let package = Package(
356357
]
357358
),
358359

359-
.executableTarget(
360-
name: "JExtractSwiftTool",
361-
dependencies: [
362-
"JExtractSwift",
363-
],
364-
swiftSettings: [
365-
.swiftLanguageMode(.v5)
366-
]
367-
),
368-
369360
.plugin(
370361
name: "JExtractSwiftPlugin",
371362
capability: .buildTool(),
372363
dependencies: [
373-
"JExtractSwiftTool"
364+
"SwiftJavaTool"
374365
]
375366
),
376367
.plugin(
@@ -380,7 +371,7 @@ let package = Package(
380371
permissions: [
381372
]),
382373
dependencies: [
383-
"JExtractSwiftTool"
374+
"SwiftJavaTool"
384375
]
385376
),
386377

@@ -414,7 +405,9 @@ let package = Package(
414405

415406
.testTarget(
416407
name: "Java2SwiftTests",
417-
dependencies: ["Java2SwiftLib"],
408+
dependencies: [
409+
"Java2SwiftLib"
410+
],
418411
swiftSettings: [
419412
.swiftLanguageMode(.v5),
420413
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])

Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
3434

3535
// The name of the configuration file JavaKit.config from the target for
3636
// which we are generating Swift wrappers for Java classes.
37-
let configFile = URL(filePath: sourceDir).appending(path: "Java2Swift.config")
37+
let configFile = URL(filePath: sourceDir).appending(path: "swift-java.config")
3838
let config: Configuration?
3939

4040
if let configData = try? Data(contentsOf: configFile) {

Plugins/Java2SwiftPlugin/Java2SwiftPlugin.swift renamed to Plugins/SwiftJavaPlugin/Java2SwiftPlugin.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import PackagePlugin
1818
fileprivate let SwiftJavaConfigFileName = "swift-java.config"
1919

2020
@main
21-
struct Java2SwiftBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
21+
struct SwiftJavaBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
2222

2323
var pluginName: String = "swift-java"
2424
var verbose: Bool = getEnvironmentBool("SWIFT_JAVA_VERBOSE")
@@ -27,7 +27,7 @@ struct Java2SwiftBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
2727
log("Create build commands for target '\(target.name)'")
2828
guard let sourceModule = target.sourceModule else { return [] }
2929

30-
let executable = try context.tool(named: "Java2Swift").url
30+
let executable = try context.tool(named: "SwiftJavaTool").url
3131
var commands: [Command] = []
3232

3333
// Note: Target doesn't have a directoryURL counterpart to directory,
@@ -44,7 +44,7 @@ struct Java2SwiftBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
4444
log("Config was: \(config)")
4545
var javaDependencies = config.dependencies ?? []
4646

47-
/// Find the manifest files from other Java2Swift executions in any targets
47+
/// Find the manifest files from other SwiftJava executions in any targets
4848
/// this target depends on.
4949
var dependentConfigFiles: [(String, URL)] = []
5050
func searchForConfigFiles(in target: any Target) {
@@ -107,7 +107,7 @@ struct Java2SwiftBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
107107
let classes = config.classes ?? [:]
108108
print("Classes to wrap: \(classes.map(\.key))")
109109

110-
/// Determine the set of Swift files that will be emitted by the Java2Swift tool.
110+
/// Determine the set of Swift files that will be emitted by the SwiftJava tool.
111111
// TODO: this is not precise and won't work with more advanced Java files, e.g. lambdas etc.
112112
let outputDirectoryGenerated = self.outputDirectory(context: context, generated: true)
113113
let outputSwiftFiles = classes.map { (javaClassName, swiftName) in
@@ -199,7 +199,7 @@ struct Java2SwiftBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
199199
}
200200
}
201201

202-
extension Java2SwiftBuildToolPlugin {
202+
extension SwiftJavaBuildToolPlugin {
203203
func argumentsModuleName(sourceModule: Target) -> [String] {
204204
return [
205205
"--module-name", sourceModule.name

Samples/JavaDependencySampleApp/Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ let package = Package(
7676
.swiftLanguageMode(.v5),
7777
],
7878
plugins: [
79-
.plugin(name: "Java2SwiftPlugin", package: "swift-java"),
79+
.plugin(name: "SwiftJavaPlugin", package: "swift-java"),
8080
]
8181
),
8282

@@ -94,7 +94,7 @@ let package = Package(
9494
],
9595
plugins: [
9696
// .plugin(name: "SwiftJavaBootstrapJavaPlugin", package: "swift-java"),
97-
.plugin(name: "Java2SwiftPlugin", package: "swift-java"),
97+
.plugin(name: "SwiftJavaPlugin", package: "swift-java"),
9898
]
9999
),
100100

Samples/JavaKitSampleApp/Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ let package = Package(
7777
plugins: [
7878
.plugin(name: "JavaCompilerPlugin", package: "swift-java"),
7979
.plugin(name: "JExtractSwiftPlugin", package: "swift-java"),
80-
.plugin(name: "Java2SwiftPlugin", package: "swift-java"),
80+
.plugin(name: "SwiftJavaPlugin", package: "swift-java"),
8181
]
8282
),
8383
]

Samples/JavaProbablyPrime/Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let package = Package(
3434
.swiftLanguageMode(.v5)
3535
],
3636
plugins: [
37-
.plugin(name: "Java2SwiftPlugin", package: "swift-java"),
37+
.plugin(name: "SwiftJavaPlugin", package: "swift-java"),
3838
]
3939
),
4040
]

Samples/JavaProbablyPrime/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ swift run JavaProbablyPrime <very big number>
88

99
The package itself demonstrates how to:
1010

11-
* Use the Java2Swift build tool plugin to wrap the `java.math.BigInteger` type in Swift.
11+
* Use the SwiftJava build tool plugin to wrap the `java.math.BigInteger` type in Swift.
1212
* Create an instance of `BigInteger` in Swift and use its `isProbablyPrime`.

Samples/JavaSieve/Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ let package = Package(
5858
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
5959
],
6060
plugins: [
61-
.plugin(name: "Java2SwiftPlugin", package: "swift-java"),
61+
.plugin(name: "SwiftJavaPlugin", package: "swift-java"),
6262
]
6363
),
6464

@@ -75,7 +75,7 @@ let package = Package(
7575
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
7676
],
7777
plugins: [
78-
.plugin(name: "Java2SwiftPlugin", package: "swift-java"),
78+
.plugin(name: "SwiftJavaPlugin", package: "swift-java"),
7979
]
8080
),
8181
]

Samples/JavaSieve/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
This package contains an example program that demonstrates importing a Java library distributed as a Jar file into Swift and using some APIs from that library. It demonstrates how to:
44

5-
* Use the Java2Swift tool to discover the classes in a Jar file and make them available in Swift
6-
* Layer Swift wrappers for Java classes as separate Swift modules using Java2Swift
5+
* Use the SwiftJava tool to discover the classes in a Jar file and make them available in Swift
6+
* Layer Swift wrappers for Java classes as separate Swift modules using SwiftJava
77
* Access static methods of Java classes from Swift
88

99
This example wraps an [open-source Java library](https://github.com/gazman-sdk/quadratic-sieve-Java) implementing the [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) algorithm for finding prime numbers, among other algorithms. To get started, clone that repository and build a Jar file containing the library:

Sources/JExtractSwiftTool/JExtractSwiftTool.swift

-23
This file was deleted.

Sources/JavaKitConfigurationShared/Configuration.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public struct Configuration: Codable {
2929

3030
// ==== java 2 swift ---------------------------------------------------------
3131

32-
/// The Java class path that should be passed along to the Java2Swift tool.
32+
/// The Java class path that should be passed along to the SwiftJava tool.
3333
public var classpath: String? = nil
3434

3535
public var classpathEntries: [String] {

Sources/Java2Swift/JavaToSwift.swift renamed to Sources/SwiftJavaTool/JavaToSwift.swift

+5-6
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,23 @@ import SwiftSyntaxBuilder
2424
import JavaKitConfigurationShared
2525
import JavaKitShared
2626

27-
/// Command-line utility to drive the export of Java classes into Swift types.
2827
@main
2928
struct JavaToSwift: AsyncParsableCommand {
30-
static var _commandName: String { "Java2Swift" }
29+
static var _commandName: String { "swift-java" }
3130

3231
@Option(help: "The name of the Swift module into which the resulting Swift types will be generated.")
3332
var moduleName: String?
3433

3534
@Option(
3635
help:
37-
"A Java2Swift configuration file for a given Swift module name on which this module depends, e.g., JavaKitJar=Sources/JavaKitJar/Java2Swift.config. There should be one of these options for each Swift module that this module depends on (transitively) that contains wrapped Java sources."
36+
"A swift-java configuration file for a given Swift module name on which this module depends, e.g., JavaKitJar=Sources/JavaKitJar/swift-java.config. There should be one of these options for each Swift module that this module depends on (transitively) that contains wrapped Java sources."
3837
)
3938
var dependsOn: [String] = []
4039

4140
// TODO: This should be a "make wrappers" option that just detects when we give it a jar
4241
@Flag(
4342
help:
44-
"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."
43+
"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."
4544
)
4645
var jar: Bool = false
4746

@@ -59,7 +58,7 @@ struct JavaToSwift: AsyncParsableCommand {
5958
)
6059
var swiftNativeImplementation: [String] = []
6160

62-
@Option(name: .shortAndLong, help: "The directory in which to output the generated Swift files or the Java2Swift configuration file.")
61+
@Option(name: .shortAndLong, help: "The directory in which to output the generated Swift files or the swift-java configuration file.")
6362
var outputDirectory: String? = nil
6463

6564

@@ -88,7 +87,7 @@ struct JavaToSwift: AsyncParsableCommand {
8887

8988
@Argument(
9089
help:
91-
"The input file, which is either a Java2Swift configuration file or (if '-jar' was specified) a Jar file."
90+
"The input file, which is either a swift-java configuration file or (if '-jar' was specified) a Jar file."
9291
)
9392
var input: String
9493

Tests/Java2SwiftTests/Java2SwiftTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class Java2SwiftTests: XCTestCase {
100100
if let APRIL = classObj.APRIL {
101101
self = APRIL
102102
} else {
103-
fatalError("Enum value APRIL was unexpectedly nil, please re-run Java2Swift on the most updated Java class")
103+
fatalError("Enum value APRIL was unexpectedly nil, please re-run SwiftJava on the most updated Java class")
104104
}
105105
""",
106106
"""
@@ -361,7 +361,7 @@ class Java2SwiftTests: XCTestCase {
361361
if let APRIL = classObj.APRIL {
362362
self.init(javaHolder: APRIL.javaHolder)
363363
} else {
364-
fatalError("Enum value APRIL was unexpectedly nil, please re-run Java2Swift on the most updated Java class")
364+
fatalError("Enum value APRIL was unexpectedly nil, please re-run SwiftJava on the most updated Java class")
365365
}
366366
""",
367367
"""

0 commit comments

Comments
 (0)