Skip to content

Commit f1ffba9

Browse files
committed
resolve comments
1 parent 00a8462 commit f1ffba9

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

Sources/swift-openapi-generator/runGenerator.swift

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ extension _Tool {
4444
let filePathForMode: (GeneratorMode) -> URL = { mode in
4545
outputDirectory.appendingPathComponent(mode.outputFileName)
4646
}
47-
if isDryRun {
48-
print("--------------------------------")
49-
print("Dry run mode: No files will be created or modified")
50-
}
5147
for config in configs {
5248
try runGenerator(
5349
doc: doc,
@@ -85,7 +81,7 @@ extension _Tool {
8581
isDryRun: Bool,
8682
diagnostics: any DiagnosticCollector
8783
) throws {
88-
try replaceFileContents(
84+
let result = try replaceFileContents(
8985
at: outputFilePath,
9086
with: {
9187
let output = try _OpenAPIGeneratorCore.runGenerator(
@@ -97,44 +93,49 @@ extension _Tool {
9793
},
9894
isDryRun: isDryRun
9995
)
96+
if result.didCreate && isDryRun {
97+
print("File \(outputFilePath.lastPathComponent) does not exist.\nCreating new file...")
98+
}
99+
if result.didChange {
100+
print("File \(outputFilePath.lastPathComponent) will be overwritten.")
101+
} else {
102+
print("File \(outputFilePath.lastPathComponent) will remain unchanged.")
103+
}
100104
}
101105

102106
/// Evaluates a closure to generate file data and writes the data to disk
103-
/// if the data is different than the current file contents.
107+
/// if the data is different than the current file contents. Will only write to disk
108+
/// if `isDryRun` is `false`.
104109
/// - Parameters:
105110
/// - path: A path to the file.
106111
/// - contents: A closure evaluated to produce the file contents data.
107112
/// - isDryRun: A Boolean value that indicates whether this invocation should
108113
/// be a dry run. File system changes will not be written to disk in this mode.
109114
/// - Throws: When writing to disk fails.
110-
/// - Returns: `true` if the generated contents changed, otherwise `false`.
115+
/// - Returns: A tuple of two booleans didChange and didCreate.
116+
/// The former will be `true` if the generated contents changed, otherwise `false` and
117+
/// the latter will be `true` if the file did not exist before, otherwise `false`.
111118
@discardableResult
112119
static func replaceFileContents(
113120
at path: URL,
114121
with contents: () throws -> Data,
115122
isDryRun: Bool
116-
) throws -> Bool {
123+
) throws -> (didChange: Bool, didCreate: Bool) {
117124
let data = try contents()
118125
let didChange: Bool
126+
var didCreate = false
119127
if FileManager.default.fileExists(atPath: path.path) {
120128
let existingData = try? Data(contentsOf: path)
121129
didChange = existingData != data
122-
if didChange {
123-
print("File \(path.lastPathComponent) will be overwritten.")
124-
} else {
125-
print("File \(path.lastPathComponent) will remain unchanged.")
126-
}
127130
} else {
128-
print("File \(path.lastPathComponent) does not exist.\nCreating new file...")
129131
didChange = true
132+
didCreate = true
130133
}
131134
if didChange {
132-
if isDryRun {
133-
print("Writing data to \(path.lastPathComponent)...")
134-
} else {
135+
if !isDryRun {
135136
try data.write(to: path)
136137
}
137138
}
138-
return didChange
139+
return (didChange, didCreate)
139140
}
140141
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit cfe078072ebc2fa5ee9fe5f956a1f7afb07d724c

0 commit comments

Comments
 (0)