15
15
import Foundation
16
16
import PackagePlugin
17
17
18
+ fileprivate let Java2SwiftConfigFileName = " Java2Swift.config "
19
+
18
20
@main
19
21
struct Java2SwiftBuildToolPlugin : BuildToolPlugin {
20
22
func createBuildCommands( context: PluginContext , target: Target ) throws -> [ Command ] {
@@ -24,42 +26,39 @@ struct Java2SwiftBuildToolPlugin: BuildToolPlugin {
24
26
// so we cannot eliminate this deprecation warning.
25
27
let sourceDir = target. directory. string
26
28
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 .
29
31
let configFile = URL ( filePath: sourceDir)
30
32
. appending ( path: " Java2Swift.config " )
31
33
let configData = try Data ( contentsOf: configFile)
32
34
let config = try JSONDecoder ( ) . decode ( Configuration . self, from: configData)
33
35
34
36
/// Find the manifest files from other Java2Swift executions in any targets
35
37
/// 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 ) {
38
40
let dependencyURL = URL ( filePath: target. directory. string)
39
41
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
45
46
. path ( percentEncoded: false )
46
47
47
- if FileManager . default. fileExists ( atPath: generatedManifestString ) {
48
- manifestFiles . append ( generatedManifestURL )
48
+ if FileManager . default. fileExists ( atPath: dependencyConfigString ) {
49
+ dependentConfigFiles . append ( ( target . name , dependencyConfigURL ) )
49
50
}
50
-
51
- // TODO: Look for a manifest file that was built by the plugin itself.
52
51
}
53
52
54
53
// Process direct dependencies of this target.
55
54
for dependency in target. dependencies {
56
55
switch dependency {
57
56
case . target( let target) :
58
- searchForManifestFiles ( in: target)
57
+ searchForConfigFiles ( in: target)
59
58
60
59
case . product( let product) :
61
60
for target in product. targets {
62
- searchForManifestFiles ( in: target)
61
+ searchForConfigFiles ( in: target)
63
62
}
64
63
65
64
@unknown default :
@@ -69,16 +68,7 @@ struct Java2SwiftBuildToolPlugin: BuildToolPlugin {
69
68
70
69
// Process indirect target dependencies.
71
70
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)
82
72
}
83
73
84
74
let outputDirectory = context. pluginWorkDirectoryURL
@@ -88,27 +78,26 @@ struct Java2SwiftBuildToolPlugin: BuildToolPlugin {
88
78
" --module-name " , sourceModule. name,
89
79
" --output-directory " , outputDirectory. path ( percentEncoded: false ) ,
90
80
]
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
+ ]
99
87
}
88
+ arguments. append ( configFile. path ( percentEncoded: false ) )
100
89
101
90
/// Determine the set of Swift files that will be emitted by the Java2Swift
102
91
/// tool.
103
- let outputSwiftFiles = classes. map { ( javaClassName, swiftName) in
92
+ let outputSwiftFiles = config . classes. map { ( javaClassName, swiftName) in
104
93
outputDirectory. appending ( path: " \( swiftName) .swift " )
105
94
} + [
106
95
outputDirectory. appending ( path: " \( sourceModule. name) .swift2java " )
107
96
]
108
97
109
98
return [
110
99
. 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 " ,
112
101
executable: try context. tool ( named: " Java2Swift " ) . url,
113
102
arguments: arguments,
114
103
inputFiles: [ configFile ] ,
@@ -139,16 +128,3 @@ func findJavaHome() -> String {
139
128
140
129
fatalError ( " Please set the JAVA_HOME environment variable to point to where Java is installed. " )
141
130
}
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