-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathplugin.swift
69 lines (58 loc) · 2.3 KB
/
plugin.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import PackagePlugin
import Foundation
@main
struct LintPlugin {
private func lint(tool: PluginContext.Tool, targetDirectories: [String], configurationFilePath: String?) throws {
let swiftFormatExec = URL(fileURLWithPath: tool.path.string)
var swiftFormatArgs = ["lint"]
swiftFormatArgs.append(contentsOf: targetDirectories)
swiftFormatArgs.append(contentsOf: ["--recursive", "--parallel", "--strict"])
if let configurationFilePath {
swiftFormatArgs.append(contentsOf: ["--configuration", configurationFilePath])
}
let process = try Process.run(swiftFormatExec, arguments: swiftFormatArgs)
process.waitUntilExit()
if process.terminationReason == .exit && process.terminationStatus == 0 {
print("Linted the source code.")
}
else {
let problem = "\(process.terminationReason):\(process.terminationStatus)"
Diagnostics.error("swift-format invocation failed: \(problem)")
}
}
}
extension LintPlugin: CommandPlugin {
func performCommand(
context: PluginContext,
arguments: [String]
) async throws {
let swiftFormatTool = try context.tool(named: "swift-format")
// Extract the arguments that specify what targets to format.
var argExtractor = ArgumentExtractor(arguments)
let targetNames = argExtractor.extractOption(named: "target")
let targetDirectories = try context.package.targets(named: targetNames)
.compactMap { $0 as? SourceModuleTarget }
.map(\.directory.string)
let configurationFilePath = argExtractor.extractOption(named: "configuration").first
try lint(
tool: swiftFormatTool,
targetDirectories: targetDirectories,
configurationFilePath: configurationFilePath
)
}
}
#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin
extension LintPlugin: XcodeCommandPlugin {
func performCommand(context: XcodeProjectPlugin.XcodePluginContext, arguments: [String]) throws {
let swiftFormatTool = try context.tool(named: "swift-format")
var argExtractor = ArgumentExtractor(arguments)
let configurationFilePath = argExtractor.extractOption(named: "configuration").first
try lint(
tool: swiftFormatTool,
targetDirectories: [context.xcodeProject.directory.string],
configurationFilePath: configurationFilePath
)
}
}
#endif