1515import Foundation
1616import PackagePlugin
1717
18+ fileprivate let Java2SwiftConfigFileName = " Java2Swift.config "
19+
1820@main
1921struct Java2SwiftBuildToolPlugin : BuildToolPlugin {
2022 func createBuildCommands( context: PluginContext , target: Target ) throws -> [ Command ] {
@@ -24,42 +26,39 @@ struct Java2SwiftBuildToolPlugin: BuildToolPlugin {
2426 // so we cannot eliminate this deprecation warning.
2527 let sourceDir = target. directory. string
2628
27- // Read a configuration file JavaKit.config from the target that provides
28- // information needed to call Java2Swift .
29+ // The name of the configuration file JavaKit.config from the target for
30+ // which we are generating Swift wrappers for Java classes .
2931 let configFile = URL ( filePath: sourceDir)
3032 . appending ( path: " Java2Swift.config " )
3133 let configData = try Data ( contentsOf: configFile)
3234 let config = try JSONDecoder ( ) . decode ( Configuration . self, from: configData)
3335
3436 /// Find the manifest files from other Java2Swift executions in any targets
3537 /// this target depends on.
36- var manifestFiles : [ URL ] = [ ]
37- func searchForManifestFiles ( in target: any Target ) {
38+ var dependentConfigFiles : [ ( String , URL ) ] = [ ]
39+ func searchForConfigFiles ( in target: any Target ) {
3840 let dependencyURL = URL ( filePath: target. directory. string)
3941
40- // Look for a checked-in manifest file.
41- let generatedManifestURL = dependencyURL
42- . appending ( path: " generated " )
43- . appending ( path: " \( target. name) .swift2java " )
44- let generatedManifestString = generatedManifestURL
42+ // Look for a config file within this target.
43+ let dependencyConfigURL = dependencyURL
44+ . appending ( path: Java2SwiftConfigFileName)
45+ let dependencyConfigString = dependencyConfigURL
4546 . path ( percentEncoded: false )
4647
47- if FileManager . default. fileExists ( atPath: generatedManifestString ) {
48- manifestFiles . append ( generatedManifestURL )
48+ if FileManager . default. fileExists ( atPath: dependencyConfigString ) {
49+ dependentConfigFiles . append ( ( target . name , dependencyConfigURL ) )
4950 }
50-
51- // TODO: Look for a manifest file that was built by the plugin itself.
5251 }
5352
5453 // Process direct dependencies of this target.
5554 for dependency in target. dependencies {
5655 switch dependency {
5756 case . target( let target) :
58- searchForManifestFiles ( in: target)
57+ searchForConfigFiles ( in: target)
5958
6059 case . product( let product) :
6160 for target in product. targets {
62- searchForManifestFiles ( in: target)
61+ searchForConfigFiles ( in: target)
6362 }
6463
6564 @unknown default :
@@ -69,16 +68,7 @@ struct Java2SwiftBuildToolPlugin: BuildToolPlugin {
6968
7069 // Process indirect target dependencies.
7170 for dependency in target. recursiveTargetDependencies {
72- searchForManifestFiles ( in: dependency)
73- }
74-
75- /// Determine the list of Java classes that will be translated into Swift,
76- /// along with the names of the corresponding Swift types. This will be
77- /// passed along to the Java2Swift tool.
78- let classes = config. classes. map { ( javaClassName, swiftName) in
79- ( javaClassName, swiftName ?? javaClassName. defaultSwiftNameForJavaClass)
80- } . sorted { ( lhs, rhs) in
81- lhs. 0 < rhs. 0
71+ searchForConfigFiles ( in: dependency)
8272 }
8373
8474 let outputDirectory = context. pluginWorkDirectoryURL
@@ -88,27 +78,26 @@ struct Java2SwiftBuildToolPlugin: BuildToolPlugin {
8878 " --module-name " , sourceModule. name,
8979 " --output-directory " , outputDirectory. path ( percentEncoded: false ) ,
9080 ]
91- if let classPath = config. classPath {
92- arguments += [ " -cp " , classPath]
93- }
94- arguments += manifestFiles. flatMap { manifestFile in
95- [ " --manifests " , manifestFile. path ( percentEncoded: false ) ]
96- }
97- arguments += classes. map { ( javaClassName, swiftName) in
98- " \( javaClassName) = \( swiftName) "
81+ arguments += dependentConfigFiles. flatMap { moduleAndConfigFile in
82+ let ( moduleName, configFile) = moduleAndConfigFile
83+ return [
84+ " --depends-on " ,
85+ " \( moduleName) = \( configFile. path ( percentEncoded: false ) ) "
86+ ]
9987 }
88+ arguments. append ( configFile. path ( percentEncoded: false ) )
10089
10190 /// Determine the set of Swift files that will be emitted by the Java2Swift
10291 /// tool.
103- let outputSwiftFiles = classes. map { ( javaClassName, swiftName) in
92+ let outputSwiftFiles = config . classes. map { ( javaClassName, swiftName) in
10493 outputDirectory. appending ( path: " \( swiftName) .swift " )
10594 } + [
10695 outputDirectory. appending ( path: " \( sourceModule. name) .swift2java " )
10796 ]
10897
10998 return [
11099 . buildCommand(
111- displayName: " Wrapping \( classes. count) Java classes target \( sourceModule. name) in Swift " ,
100+ displayName: " Wrapping \( config . classes. count) Java classes target \( sourceModule. name) in Swift " ,
112101 executable: try context. tool ( named: " Java2Swift " ) . url,
113102 arguments: arguments,
114103 inputFiles: [ configFile ] ,
@@ -139,16 +128,3 @@ func findJavaHome() -> String {
139128
140129 fatalError ( " Please set the JAVA_HOME environment variable to point to where Java is installed. " )
141130}
142-
143- extension String {
144- /// For a String that's of the form java.util.Vector, return the "Vector"
145- /// part.
146- fileprivate var defaultSwiftNameForJavaClass : String {
147- if let dotLoc = lastIndex ( of: " . " ) {
148- let afterDot = index ( after: dotLoc)
149- return String ( self [ afterDot... ] )
150- }
151-
152- return self
153- }
154- }
0 commit comments