@@ -16,20 +16,22 @@ import ArgumentParser
16
16
import _OpenAPIGeneratorCore
17
17
18
18
extension _Tool {
19
-
20
19
/// Runs the generator with the specified configuration values.
21
20
/// - Parameters:
22
21
/// - doc: A path to the OpenAPI document.
23
22
/// - configs: A list of generator configurations.
24
23
/// - pluginSource: The source of the generator invocation.
25
24
/// - outputDirectory: The directory to which the generator writes
26
25
/// the generated Swift files.
26
+ /// - isDryRun: A Boolean value that indicates whether this invocation should
27
+ /// be a dry run.
27
28
/// - diagnostics: A collector for diagnostics emitted by the generator.
28
29
static func runGenerator(
29
30
doc: URL ,
30
31
configs: [ Config ] ,
31
32
pluginSource: PluginSource ? ,
32
33
outputDirectory: URL ,
34
+ isDryRun: Bool ,
33
35
diagnostics: any DiagnosticCollector
34
36
) throws {
35
37
let docData : Data
@@ -45,6 +47,7 @@ extension _Tool {
45
47
config: config,
46
48
outputDirectory: outputDirectory,
47
49
outputFileName: config. mode. outputFileName,
50
+ isDryRun: isDryRun,
48
51
diagnostics: diagnostics
49
52
)
50
53
}
@@ -59,7 +62,8 @@ extension _Tool {
59
62
try replaceFileContents (
60
63
inDirectory: outputDirectory,
61
64
fileName: mode. outputFileName,
62
- with: { Data ( ) }
65
+ with: { Data ( ) } ,
66
+ isDryRun: isDryRun
63
67
)
64
68
}
65
69
}
@@ -74,62 +78,63 @@ extension _Tool {
74
78
/// the generated Swift file.
75
79
/// - outputFileName: The file name to which the generator writes
76
80
/// the generated Swift file.
81
+ /// - isDryRun: A Boolean value that indicates whether this invocation should
82
+ /// be a dry run.
77
83
/// - diagnostics: A collector for diagnostics emitted by the generator.
78
84
static func runGenerator(
79
85
doc: URL ,
80
86
docData: Data ,
81
87
config: Config ,
82
88
outputDirectory: URL ,
83
89
outputFileName: String ,
90
+ isDryRun: Bool ,
84
91
diagnostics: any DiagnosticCollector
85
92
) throws {
86
- let didChange = try replaceFileContents (
93
+ try replaceFileContents (
87
94
inDirectory: outputDirectory,
88
- fileName: outputFileName
89
- ) {
90
- let output = try _OpenAPIGeneratorCore. runGenerator (
91
- input: . init( absolutePath: doc, contents: docData) ,
92
- config: config,
93
- diagnostics: diagnostics
94
- )
95
- return output. contents
96
- }
97
- print ( " File \( outputFileName) : \( didChange ? " changed " : " unchanged " ) " )
95
+ fileName: outputFileName,
96
+ with: {
97
+ let output = try _OpenAPIGeneratorCore. runGenerator (
98
+ input: . init( absolutePath: doc, contents: docData) ,
99
+ config: config,
100
+ diagnostics: diagnostics
101
+ )
102
+ return output. contents
103
+ } ,
104
+ isDryRun: isDryRun
105
+ )
98
106
}
99
107
100
108
/// Evaluates a closure to generate file data and writes the data to disk
101
- /// if the data is different than the current file contents.
109
+ /// if the data is different than the current file contents. Will write to disk
110
+ /// only if `isDryRun` is set as `false`.
102
111
/// - Parameters:
103
112
/// - path: A path to the file.
104
113
/// - contents: A closure evaluated to produce the file contents data.
114
+ /// - isDryRun: A Boolean value that indicates whether this invocation should
115
+ /// be a dry run. File system changes will not be written to disk in this mode.
105
116
/// - Throws: When writing to disk fails.
106
- /// - Returns: `true` if the generated contents changed, otherwise `false`.
107
- @discardableResult
108
117
static func replaceFileContents(
109
118
inDirectory outputDirectory: URL ,
110
119
fileName: String ,
111
- with contents: ( ) throws -> Data
112
- ) throws -> Bool {
120
+ with contents: ( ) throws -> Data ,
121
+ isDryRun: Bool
122
+ ) throws {
113
123
let fileManager = FileManager . default
124
+ let path = outputDirectory. appendingPathComponent ( fileName)
125
+ let data = try contents ( )
114
126
115
- // Create directory if it doesn't exist.
116
- if !fileManager. fileExists ( atPath: outputDirectory. path) {
127
+ if let existingData = try ? Data ( contentsOf: path) , existingData == data {
128
+ print ( " File \( path. lastPathComponent) already up to date. " )
129
+ return
130
+ }
131
+ print ( " Writing data to file \( path. lastPathComponent) ... " )
132
+ if !isDryRun {
117
133
try fileManager. createDirectory (
118
134
at: outputDirectory,
119
135
withIntermediateDirectories: true
120
136
)
121
- }
122
-
123
- let path = outputDirectory. appendingPathComponent ( fileName)
124
- let data = try contents ( )
125
- guard fileManager. fileExists ( atPath: path. path) else {
126
- return fileManager. createFile ( atPath: path. path, contents: data)
127
- }
128
- let existingData = try ? Data ( contentsOf: path)
129
- guard existingData == data else {
130
137
try data. write ( to: path)
131
- return true
132
138
}
133
- return false
134
139
}
135
140
}
0 commit comments