@@ -50,14 +50,49 @@ struct JavaToSwift: ParsableCommand {
50
50
var classpath : [ String ] = [ ]
51
51
52
52
@Option ( name: . shortAndLong, help: " The directory in which to output the generated Swift files or the Java2Swift configuration file. " )
53
- var outputDirectory : String = " . "
53
+ var outputDirectory : String ? = nil
54
54
55
55
@Argument (
56
56
help:
57
57
" The input file, which is either a Java2Swift configuration file or (if '-jar' was specified) a Jar file. "
58
58
)
59
59
var input : String
60
60
61
+ /// Whether we have ensured that the output directory exists.
62
+ var createdOutputDirectory : Bool = false
63
+
64
+ /// The output directory in which to place the generated files, which will
65
+ /// be the specified directory (--output-directory or -o option) if given,
66
+ /// or a default directory derived from the other command-line arguments.
67
+ ///
68
+ /// Returns `nil` only when we should emit the files to standard output.
69
+ var actualOutputDirectory : Foundation . URL ? {
70
+ if let outputDirectory {
71
+ if outputDirectory == " - " {
72
+ return nil
73
+ }
74
+
75
+ return URL ( fileURLWithPath: outputDirectory)
76
+ }
77
+
78
+ // Put the result into Sources/\(moduleName).
79
+ let baseDir = URL ( fileURLWithPath: " . " )
80
+ . appendingPathComponent ( " Sources " , isDirectory: true )
81
+ . appendingPathComponent ( moduleName, isDirectory: true )
82
+
83
+ // For generated Swift sources, put them into a "generated" subdirectory.
84
+ // The configuration file goes at the top level.
85
+ let outputDir : Foundation . URL
86
+ if jar {
87
+ outputDir = baseDir
88
+ } else {
89
+ outputDir = baseDir
90
+ . appendingPathComponent ( " generated " , isDirectory: true )
91
+ }
92
+
93
+ return outputDir
94
+ }
95
+
61
96
/// Describes what kind of generation action is being performed by
62
97
/// Java2Swift.
63
98
enum GenerationMode {
@@ -209,23 +244,31 @@ struct JavaToSwift: ParsableCommand {
209
244
}
210
245
}
211
246
212
- func writeContents( _ contents: String , to filename: String , description: String ) throws {
213
- if outputDirectory == " - " {
247
+ mutating func writeContents( _ contents: String , to filename: String , description: String ) throws {
248
+ guard let outputDir = actualOutputDirectory else {
214
249
print ( " // \( filename) - \( description) " )
215
250
print ( contents)
216
251
return
217
252
}
218
253
219
- print ( " Writing \( description) to ' \( filename) '... " , terminator: " " )
220
- try contents. write (
221
- to: Foundation . URL ( fileURLWithPath: outputDirectory) . appendingPathComponent ( filename) ,
222
- atomically: true ,
223
- encoding: . utf8
224
- )
254
+ // If we haven't tried to create the output directory yet, do so now before
255
+ // we write any files to it.
256
+ if !createdOutputDirectory {
257
+ try FileManager . default. createDirectory (
258
+ at: outputDir,
259
+ withIntermediateDirectories: true
260
+ )
261
+ createdOutputDirectory = true
262
+ }
263
+
264
+ // Write the file:
265
+ let file = outputDir. appendingPathComponent ( filename)
266
+ print ( " Writing \( description) to ' \( file. path) '... " , terminator: " " )
267
+ try contents. write ( to: file, atomically: true , encoding: . utf8)
225
268
print ( " done. " )
226
269
}
227
270
228
- func emitConfiguration(
271
+ mutating func emitConfiguration(
229
272
forJarFile jarFileName: String ,
230
273
classPath: String ,
231
274
environment: JNIEnvironment
0 commit comments