@@ -44,10 +44,6 @@ extension _Tool {
44
44
let filePathForMode : ( GeneratorMode ) -> URL = { mode in
45
45
outputDirectory. appendingPathComponent ( mode. outputFileName)
46
46
}
47
- if isDryRun {
48
- print ( " -------------------------------- " )
49
- print ( " Dry run mode: No files will be created or modified " )
50
- }
51
47
for config in configs {
52
48
try runGenerator (
53
49
doc: doc,
@@ -85,7 +81,7 @@ extension _Tool {
85
81
isDryRun: Bool ,
86
82
diagnostics: any DiagnosticCollector
87
83
) throws {
88
- try replaceFileContents (
84
+ let result = try replaceFileContents (
89
85
at: outputFilePath,
90
86
with: {
91
87
let output = try _OpenAPIGeneratorCore. runGenerator (
@@ -97,44 +93,49 @@ extension _Tool {
97
93
} ,
98
94
isDryRun: isDryRun
99
95
)
96
+ if result. didCreate && isDryRun {
97
+ print ( " File \( outputFilePath. lastPathComponent) does not exist. \n Creating 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
+ }
100
104
}
101
105
102
106
/// 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`.
104
109
/// - Parameters:
105
110
/// - path: A path to the file.
106
111
/// - contents: A closure evaluated to produce the file contents data.
107
112
/// - isDryRun: A Boolean value that indicates whether this invocation should
108
113
/// be a dry run. File system changes will not be written to disk in this mode.
109
114
/// - 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`.
111
118
@discardableResult
112
119
static func replaceFileContents(
113
120
at path: URL ,
114
121
with contents: ( ) throws -> Data ,
115
122
isDryRun: Bool
116
- ) throws -> Bool {
123
+ ) throws -> ( didChange : Bool , didCreate : Bool ) {
117
124
let data = try contents ( )
118
125
let didChange : Bool
126
+ var didCreate = false
119
127
if FileManager . default. fileExists ( atPath: path. path) {
120
128
let existingData = try ? Data ( contentsOf: path)
121
129
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
- }
127
130
} else {
128
- print ( " File \( path. lastPathComponent) does not exist. \n Creating new file... " )
129
131
didChange = true
132
+ didCreate = true
130
133
}
131
134
if didChange {
132
- if isDryRun {
133
- print ( " Writing data to \( path. lastPathComponent) ... " )
134
- } else {
135
+ if !isDryRun {
135
136
try data. write ( to: path)
136
137
}
137
138
}
138
- return didChange
139
+ return ( didChange, didCreate )
139
140
}
140
141
}
0 commit comments