@@ -59,8 +59,8 @@ extension SwiftJava {
5959 swiftModule
6060 }
6161
62- @Argument ( help: " The input file, which is either a swift-java configuration file or (if '-jar' was specified) a Jar file. " )
63- var input : String ?
62+ @Option ( help: " A prefix that will be added to the names of the Swift types " )
63+ var swiftPrefix : String ?
6464 }
6565}
6666
@@ -137,14 +137,19 @@ extension SwiftJava.ConfigureCommand {
137137
138138 print ( " [debug][swift-java] Importing classpath entry: \( entry) " )
139139 if entry. hasSuffix ( " .jar " ) {
140+ print ( " [debug][swift-java] Importing classpath as JAR file: \( entry) " )
140141 let jarFile = try JarFile ( entry, false , environment: environment)
141142 try addJavaToSwiftMappings (
142143 to: & config,
143144 forJar: jarFile,
144145 environment: environment
145146 )
146- } else if FileManager . default. fileExists ( atPath: entry) {
147- log. warning ( " Currently unable handle directory classpath entries for config generation! Skipping: \( entry) " )
147+ } else if FileManager . default. fileExists ( atPath: entry) , let entryURL = URL ( string: entry) {
148+ print ( " [debug][swift-java] Importing classpath as directory: \( entryURL) " )
149+ try addJavaToSwiftMappings (
150+ to: & configuration,
151+ forDirectory: entryURL
152+ )
148153 } else {
149154 log. warning ( " Classpath entry does not exist, skipping: \( entry) " )
150155 }
@@ -162,62 +167,82 @@ extension SwiftJava.ConfigureCommand {
162167 )
163168 }
164169
170+ mutating func addJavaToSwiftMappings(
171+ to configuration: inout Configuration ,
172+ forDirectory url: Foundation . URL
173+ ) throws {
174+ let enumerator = FileManager . default. enumerator ( atPath: url. path ( ) )
175+
176+ while let filePath = enumerator? . nextObject ( ) as? String {
177+ try addJavaToSwiftMappings ( to: & configuration, fileName: filePath)
178+ }
179+ }
180+
165181 mutating func addJavaToSwiftMappings(
166182 to configuration: inout Configuration ,
167183 forJar jarFile: JarFile ,
168184 environment: JNIEnvironment
169185 ) throws {
170- let log = Self . log
171-
172186 for entry in jarFile. entries ( ) ! {
173- // We only look at class files in the Jar file.
174- guard entry. getName ( ) . hasSuffix ( " .class " ) else {
175- continue
176- }
187+ try addJavaToSwiftMappings ( to: & configuration, fileName: entry. getName ( ) )
188+ }
189+ }
177190
178- // Skip some "common" files we know that would be duplicated in every jar
179- guard !entry. getName ( ) . hasPrefix ( " META-INF " ) else {
180- continue
181- }
182- guard !entry. getName ( ) . hasSuffix ( " package-info " ) else {
183- continue
184- }
185- guard !entry. getName ( ) . hasSuffix ( " package-info.class " ) else {
186- continue
187- }
191+ mutating func addJavaToSwiftMappings(
192+ to configuration: inout Configuration ,
193+ fileName: String
194+ ) throws {
195+ // We only look at class files
196+ guard fileName. hasSuffix ( " .class " ) else {
197+ return
198+ }
188199
189- // If this is a local class, it cannot be mapped into Swift.
190- if entry. getName ( ) . isLocalJavaClass {
191- continue
192- }
200+ // Skip some "common" files we know that would be duplicated in every jar
201+ guard !fileName. hasPrefix ( " META-INF " ) else {
202+ return
203+ }
204+ guard !fileName. hasSuffix ( " package-info " ) else {
205+ return
206+ }
207+ guard !fileName. hasSuffix ( " package-info.class " ) else {
208+ return
209+ }
193210
194- let javaCanonicalName = String ( entry. getName ( ) . replacing ( " / " , with: " . " )
195- . dropLast ( " .class " . count) )
211+ // If this is a local class, it cannot be mapped into Swift.
212+ if fileName. isLocalJavaClass {
213+ return
214+ }
196215
197- guard SwiftJava . shouldImport ( javaCanonicalName: javaCanonicalName, commonOptions: self . commonOptions) else {
198- log. info ( " Skip importing class: \( javaCanonicalName) due to include/exclude filters " )
199- continue
200- }
216+ let javaCanonicalName = String ( fileName. replacing ( " / " , with: " . " )
217+ . dropLast ( " .class " . count) )
201218
202- if configuration. classes ? [ javaCanonicalName] != nil {
203- // We never overwrite an existing class mapping configuration.
204- // E.g. the user may have configured a custom name for a type.
205- continue
206- }
219+ guard SwiftJava . shouldImport ( javaCanonicalName: javaCanonicalName, commonOptions: self . commonOptions) else {
220+ log. info ( " Skip importing class: \( javaCanonicalName) due to include/exclude filters " )
221+ return
222+ }
207223
208- if configuration. classes == nil {
209- configuration. classes = [ : ]
210- }
224+ if configuration. classes ? [ javaCanonicalName] != nil {
225+ // We never overwrite an existing class mapping configuration.
226+ // E.g. the user may have configured a custom name for a type.
227+ return
228+ }
211229
212- if let configuredSwiftName = configuration. classes![ javaCanonicalName] {
213- log. info ( " Java type ' \( javaCanonicalName) ' already configured as ' \( configuredSwiftName) ' Swift type. " )
214- } else {
215- log. info ( " Configure Java type ' \( javaCanonicalName) ' as ' \( javaCanonicalName. defaultSwiftNameForJavaClass. bold) ' Swift type. " )
216- }
230+ if configuration. classes == nil {
231+ configuration. classes = [ : ]
232+ }
217233
218- configuration. classes![ javaCanonicalName] =
219- javaCanonicalName. defaultSwiftNameForJavaClass
234+ var swiftName = javaCanonicalName. defaultSwiftNameForJavaClass
235+ if let swiftPrefix {
236+ swiftName = " \( swiftPrefix) \( swiftName) "
220237 }
238+
239+ if let configuredSwiftName = configuration. classes![ javaCanonicalName] {
240+ log. info ( " Java type ' \( javaCanonicalName) ' already configured as ' \( configuredSwiftName) ' Swift type. " )
241+ } else {
242+ log. info ( " Configure Java type ' \( javaCanonicalName) ' as ' \( swiftName. bold) ' Swift type. " )
243+ }
244+
245+ configuration. classes![ javaCanonicalName] = swiftName
221246 }
222247}
223248
0 commit comments