1212//
1313//===----------------------------------------------------------------------===//
1414
15- import ArgumentParser
1615import Foundation
1716import SwiftSyntax
1817import SwiftSyntaxBuilder
1918import JavaKitShared
19+ import JavaKitConfigurationShared // TODO: this should become SwiftJavaConfigurationShared
2020
21- /// Command-line utility, similar to `jextract` to export Swift types to Java.
22- public struct SwiftToJava : ParsableCommand {
23- public init ( ) { }
21+ public struct SwiftToJava {
22+ let config : Configuration
2423
25- public static var _commandName : String {
26- " jextract-swift "
24+ public init ( config : Configuration ) {
25+ self . config = config
2726 }
2827
29- @Option ( help: " The package the generated Java code should be emitted into. " )
30- var packageName : String
31-
32- @Option (
33- name: . shortAndLong,
34- help: " The directory in which to output the generated Swift files and manifest. " )
35- var outputDirectoryJava : String = " .build/jextract-swift/generated "
36-
37- @Option ( help: " Swift output directory " )
38- var outputDirectorySwift : String
39-
40- @Option (
41- name: . long,
42- help: " Name of the Swift module to import (and the swift interface files belong to) " )
43- var swiftModule : String
44-
45- @Option ( name: . shortAndLong, help: " Configure the level of lots that should be printed " )
46- var logLevel : Logger . Level = . info
47-
48- @Argument ( help: " The Swift files or directories to recursively export to Java. " )
49- var input : [ String ]
50-
5128 public func run( ) throws {
52- let inputPaths = self . input. dropFirst ( ) . map { URL ( string: $0) ! }
29+ guard let swiftModule = config. swiftModule else {
30+ fatalError ( " Missing '--swift-module' name. " )
31+ }
5332
5433 let translator = Swift2JavaTranslator (
55- javaPackage: packageName ,
34+ javaPackage: config . javaPackage ?? " " , // no package is ok, we'd generate all into top level
5635 swiftModuleName: swiftModule
5736 )
58- translator. log. logLevel = logLevel
37+ translator. log. logLevel = config. logLevel ?? . info
38+
39+ if config. javaPackage == nil || config. javaPackage!. isEmpty {
40+ translator. log. warning ( " Configured java package is '', consider specifying concrete package for generated sources. " )
41+ }
42+
43+ print ( " ===== CONFIG ==== \( config) " )
44+
45+ guard let inputSwift = config. inputSwiftDirectory else {
46+ fatalError ( " Missing '--swift-input' directory! " )
47+ }
48+
49+ let inputPaths = inputSwift. split ( separator: " , " ) . map { URL ( string: String ( $0) ) ! }
50+ translator. log. info ( " Input paths = \( inputPaths) " )
5951
6052 var allFiles : [ URL ] = [ ]
6153 let fileManager = FileManager . default
6254 let log = translator. log
63-
55+
6456 for path in inputPaths {
65- log. debug ( " Input path: \( path) " )
57+ log. info ( " Input path: \( path) " )
6658 if isDirectory ( url: path) {
6759 if let enumerator = fileManager. enumerator ( at: path, includingPropertiesForKeys: nil ) {
6860 for case let fileURL as URL in enumerator {
@@ -88,10 +80,21 @@ public struct SwiftToJava: ParsableCommand {
8880 translator. add ( filePath: file. path, text: text)
8981 }
9082
83+ guard let outputSwiftDirectory = config. outputSwiftDirectory else {
84+ fatalError ( " Missing --output-swift directory! " )
85+ }
86+ guard let outputJavaDirectory = config. outputJavaDirectory else {
87+ fatalError ( " Missing --output-java directory! " )
88+ }
89+
9190 try translator. analyze ( )
92- try translator. writeSwiftThunkSources ( outputDirectory: outputDirectorySwift)
93- try translator. writeExportedJavaSources ( outputDirectory: outputDirectoryJava)
94- print ( " [swift-java] Generated Java sources ( \( packageName) ) in: \( outputDirectoryJava) / " )
91+
92+ try translator. writeSwiftThunkSources ( outputDirectory: outputSwiftDirectory)
93+ print ( " [swift-java] Generated Swift sources (module: ' \( config. swiftModule ?? " " ) ') in: \( outputSwiftDirectory) / " )
94+
95+ try translator. writeExportedJavaSources ( outputDirectory: outputJavaDirectory)
96+ print ( " [swift-java] Generated Java sources (package: ' \( config. javaPackage ?? " " ) ') in: \( outputJavaDirectory) / " )
97+
9598 print ( " [swift-java] Imported Swift module ' \( swiftModule) ': " + " done. " . green)
9699 }
97100
@@ -102,16 +105,6 @@ public struct SwiftToJava: ParsableCommand {
102105
103106}
104107
105- extension Logger . Level : ExpressibleByArgument {
106- public var defaultValueDescription : String {
107- " log level "
108- }
109- public private( set) static var allValueStrings : [ String ] =
110- [ " trace " , " debug " , " info " , " notice " , " warning " , " error " , " critical " ]
111-
112- public private( set) static var defaultCompletionKind : CompletionKind = . default
113- }
114-
115108func isDirectory( url: URL ) -> Bool {
116109 var isDirectory : ObjCBool = false
117110 _ = FileManager . default. fileExists ( atPath: url. path, isDirectory: & isDirectory)
0 commit comments