Skip to content

Commit 0b0b22b

Browse files
authored
Improve _format command (#3684)
This makes 2 changes to `_format`: 1. Only invoke `swift-format` once, this is helpful for larger projects 2. Include the `Package.swift` file for formatting
1 parent 9625021 commit 0b0b22b

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

Sources/Commands/SwiftPackageTool.swift

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -265,26 +265,23 @@ extension SwiftPackageTool {
265265
: swiftFormatFlags
266266

267267
// Process each target in the root package.
268-
for target in package.targets {
269-
for file in target.sources.paths {
270-
// Only process Swift sources.
271-
guard let ext = file.extension, ext == SupportedLanguageExtension.swift.rawValue else {
272-
continue
273-
}
268+
let paths = package.targets.flatMap { target in
269+
target.sources.paths.filter { file in
270+
file.extension == SupportedLanguageExtension.swift.rawValue
271+
}
272+
}.map { $0.pathString }
274273

275-
let args = [swiftFormat.pathString] + formatOptions + [file.pathString]
276-
print("Running:", args.map{ $0.spm_shellEscaped() }.joined(separator: " "))
274+
let args = [swiftFormat.pathString] + formatOptions + [rootManifest.path.pathString] + paths
275+
print("Running:", args.map{ $0.spm_shellEscaped() }.joined(separator: " "))
277276

278-
let result = try Process.popen(arguments: args)
279-
let output = try (result.utf8Output() + result.utf8stderrOutput())
277+
let result = try Process.popen(arguments: args)
278+
let output = try (result.utf8Output() + result.utf8stderrOutput())
280279

281-
if result.exitStatus != .terminated(code: 0) {
282-
print("Non-zero exit", result.exitStatus)
283-
}
284-
if !output.isEmpty {
285-
print(output)
286-
}
287-
}
280+
if result.exitStatus != .terminated(code: 0) {
281+
print("Non-zero exit", result.exitStatus)
282+
}
283+
if !output.isEmpty {
284+
print(output)
288285
}
289286
}
290287
}

0 commit comments

Comments
 (0)