1212//
1313//===----------------------------------------------------------------------===//
1414
15+ import Logging
1516import ArgumentParser
1617import Foundation
1718import SwiftJavaToolLib
@@ -27,6 +28,9 @@ import SwiftJavaShared
2728
2829extension SwiftJava {
2930 struct ConfigureCommand : SwiftJavaBaseAsyncParsableCommand , HasCommonOptions , HasCommonJVMOptions {
31+
32+ static let log : Logging . Logger = Logger ( label: " swift-java: \( configuration. commandName!) " )
33+
3034 static let configuration = CommandConfiguration (
3135 commandName: " configure " ,
3236 abstract: " Configure and emit a swift-java.config file based on an input dependency or jar file " )
@@ -63,12 +67,12 @@ extension SwiftJava {
6367extension SwiftJava . ConfigureCommand {
6468 mutating func runSwiftJavaCommand( config: inout Configuration ) async throws {
6569 let classpathEntries = self . configureCommandJVMClasspath (
66- searchDirs: [ self . effectiveSwiftModuleURL] , config: config)
70+ searchDirs: [ self . effectiveSwiftModuleURL] , config: config, log : Self . log )
6771
6872 let jvm =
6973 try self . makeJVM ( classpathEntries: classpathEntries)
7074
71- try emitConfiguration ( classpath : self . commonJVMOptions . classpath , environment: jvm. environment ( ) )
75+ try emitConfiguration ( classpathEntries : classpathEntries , environment: jvm. environment ( ) )
7276 }
7377
7478 /// Get base configuration, depending on if we are to 'amend' or 'overwrite' the existing configuration.
@@ -93,32 +97,42 @@ extension SwiftJava.ConfigureCommand {
9397
9498 // TODO: make this perhaps "emit type mappings"
9599 mutating func emitConfiguration(
96- classpath : [ String ] ,
100+ classpathEntries : [ String ] ,
97101 environment: JNIEnvironment
98102 ) throws {
103+ var log = Self . log
104+ log. logLevel = . init( rawValue: self . logLevel. rawValue) !
105+
106+
107+ log. info ( " Run: emit configuration... " )
108+ var ( amendExistingConfig, configuration) = try getBaseConfigurationForWrite ( )
109+
99110 if let filterJavaPackage = self . commonJVMOptions. filterJavaPackage {
100- print ( " [java-swift][debug] Generate Java->Swift type mappings. Active filter: \( filterJavaPackage) " )
111+ log. debug ( " Generate Java->Swift type mappings. Active filter: \( filterJavaPackage) " )
112+ } else if let filterJavaPackage = configuration. filterJavaPackage {
113+ // take the package filter from the configuration file
114+ self . commonJVMOptions. filterJavaPackage = filterJavaPackage
115+ } else {
116+ log. debug ( " Generate Java->Swift type mappings. No package filter applied. " )
101117 }
102- print ( " [java-swift][debug] Classpath: \( classpath ) " )
118+ log . debug ( " Classpath: \( classpathEntries ) " )
103119
104- if classpath . isEmpty {
105- print ( " [java-swift][warning] Classpath is empty!" )
120+ if classpathEntries . isEmpty {
121+ log . warning ( " Classpath is empty! " )
106122 }
107123
108124 // Get a fresh or existing configuration we'll amend
109- var ( amendExistingConfig, configuration) = try getBaseConfigurationForWrite ( )
110125 if amendExistingConfig {
111- print ( " [swift-java] Amend existing swift-java.config file..." )
126+ log . info ( " Amend existing swift-java.config file... " )
112127 }
113- configuration. classpath = classpath . joined ( separator: " : " ) // TODO: is this correct?
128+ configuration. classpath = classpathEntries . joined ( separator: " : " ) // TODO: is this correct?
114129
115130 // Import types from all the classpath entries;
116131 // Note that we use the package level filtering, so users have some control over what gets imported.
117- let classpathEntries = classpath. split ( separator: " : " ) . map ( String . init)
118132 for entry in classpathEntries {
119133 guard fileOrDirectoryExists ( at: entry) else {
120134 // We only log specific jars missing, as paths may be empty directories that won't hurt not existing.
121- print ( " [debug][swift-java] Classpath entry does not exist: \( entry) " )
135+ log . debug ( " Classpath entry does not exist: \( entry) " )
122136 continue
123137 }
124138
@@ -131,9 +145,9 @@ extension SwiftJava.ConfigureCommand {
131145 environment: environment
132146 )
133147 } else if FileManager . default. fileExists ( atPath: entry) {
134- print ( " [warning][swift-java] Currently unable handle directory classpath entries for config generation! Skipping: \( entry) " )
148+ log . warning ( " Currently unable handle directory classpath entries for config generation! Skipping: \( entry) " )
135149 } else {
136- print ( " [warning][swift-java] Classpath entry does not exist, skipping: \( entry) " )
150+ log . warning ( " Classpath entry does not exist, skipping: \( entry) " )
137151 }
138152 }
139153
@@ -154,6 +168,8 @@ extension SwiftJava.ConfigureCommand {
154168 forJar jarFile: JarFile ,
155169 environment: JNIEnvironment
156170 ) throws {
171+ let log = Self . log
172+
157173 for entry in jarFile. entries ( ) ! {
158174 // We only look at class files in the Jar file.
159175 guard entry. getName ( ) . hasSuffix ( " .class " ) else {
@@ -179,6 +195,7 @@ extension SwiftJava.ConfigureCommand {
179195 let javaCanonicalName = String ( entry. getName ( ) . replacing ( " / " , with: " . " )
180196 . dropLast ( " .class " . count) )
181197
198+
182199 if let filterJavaPackage = self . commonJVMOptions. filterJavaPackage,
183200 !javaCanonicalName. hasPrefix ( filterJavaPackage) {
184201 // Skip classes which don't match our expected prefix
@@ -191,7 +208,17 @@ extension SwiftJava.ConfigureCommand {
191208 continue
192209 }
193210
194- configuration. classes ? [ javaCanonicalName] =
211+ if configuration. classes == nil {
212+ configuration. classes = [ : ]
213+ }
214+
215+ if let configuredSwiftName = configuration. classes![ javaCanonicalName] {
216+ log. info ( " Java type ' \( javaCanonicalName) ' already configured as ' \( configuredSwiftName) ' Swift type. " )
217+ } else {
218+ log. info ( " Configure Java type ' \( javaCanonicalName) ' as ' \( javaCanonicalName. defaultSwiftNameForJavaClass. bold) ' Swift type. " )
219+ }
220+
221+ configuration. classes![ javaCanonicalName] =
195222 javaCanonicalName. defaultSwiftNameForJavaClass
196223 }
197224 }
0 commit comments