Skip to content

Commit 207bd13

Browse files
authored
Merge pull request #94 from DougGregor/java2swift-output-dir
Java2Swift: Provide defaults for the output directory and create it before writing files
2 parents 234c301 + 8cc89d9 commit 207bd13

File tree

1 file changed

+53
-10
lines changed

1 file changed

+53
-10
lines changed

Sources/Java2Swift/JavaToSwift.swift

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,49 @@ struct JavaToSwift: ParsableCommand {
5050
var classpath: [String] = []
5151

5252
@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
5454

5555
@Argument(
5656
help:
5757
"The input file, which is either a Java2Swift configuration file or (if '-jar' was specified) a Jar file."
5858
)
5959
var input: String
6060

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+
6196
/// Describes what kind of generation action is being performed by
6297
/// Java2Swift.
6398
enum GenerationMode {
@@ -209,23 +244,31 @@ struct JavaToSwift: ParsableCommand {
209244
}
210245
}
211246

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 {
214249
print("// \(filename) - \(description)")
215250
print(contents)
216251
return
217252
}
218253

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)
225268
print(" done.")
226269
}
227270

228-
func emitConfiguration(
271+
mutating func emitConfiguration(
229272
forJarFile jarFileName: String,
230273
classPath: String,
231274
environment: JNIEnvironment

0 commit comments

Comments
 (0)