-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathSwiftLintCommandPlugin.swift
More file actions
106 lines (92 loc) · 3.74 KB
/
SwiftLintCommandPlugin.swift
File metadata and controls
106 lines (92 loc) · 3.74 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import Foundation
import PackagePlugin
private extension URL {
var filepath: String {
withUnsafeFileSystemRepresentation { String(cString: $0!) }
}
}
private let commandsNotExpectingPaths: Set<String> = [
"docs",
"generate-docs",
"baseline",
"reporters",
"rules",
"version",
]
private let commandsWithoutCachPathOption: Set<String> = commandsNotExpectingPaths.union([
"analyze",
])
@main
struct SwiftLintCommandPlugin: CommandPlugin {
func performCommand(context: PluginContext, arguments: [String]) throws {
try lintFiles(context: context, arguments: arguments)
}
}
#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin
extension SwiftLintCommandPlugin: XcodeCommandPlugin {
func performCommand(context: XcodePluginContext, arguments: [String]) throws {
try lintFiles(context: context, arguments: arguments)
}
}
#endif
extension SwiftLintCommandPlugin {
private func lintFiles(context: some CommandContext, arguments: [String]) throws {
guard !arguments.contains("--cache-path") else {
Diagnostics.error("Caching is managed by the plugin and so setting `--cache-path` is not allowed")
return
}
var argExtractor = ArgumentExtractor(arguments)
let targetNames = argExtractor.extractOption(named: "target")
let remainingArguments = argExtractor.remainingArguments
if !commandsNotExpectingPaths.isDisjoint(with: remainingArguments) {
try lintFiles(with: context, arguments: remainingArguments)
return
}
guard !targetNames.isEmpty else {
if let pathArgument = remainingArguments.last, FileManager.default.fileExists(atPath: pathArgument) {
Diagnostics.remark("No targets provided. Files provided in path arguments will be linted.")
try lintFiles(in: [], with: context, arguments: remainingArguments)
} else {
try lintFiles(with: context, arguments: remainingArguments)
}
return
}
for target in try context.targets(named: targetNames) {
try lintFiles(in: target.paths, for: target.name, with: context, arguments: remainingArguments)
}
}
private func lintFiles(in paths: [URL] = [URL.currentDirectory()],
for targetName: String? = nil,
with context: some CommandContext,
arguments: [String]) throws {
let process = Process()
process.currentDirectoryURL = context.workingDirectory
process.executableURL = try context.tool
process.arguments = arguments
if commandsWithoutCachPathOption.isDisjoint(with: arguments) {
process.arguments! += ["--cache-path", context.cacheDirectory.filepath]
}
if commandsNotExpectingPaths.isDisjoint(with: arguments) {
process.arguments! += paths.map(\.filepath)
}
try process.run()
process.waitUntilExit()
let module = targetName.map { "\(context.subUnitName) '\($0)'" } ?? context.unitName
switch process.terminationReason {
case .exit:
Diagnostics.remark("Finished running in \(module)")
case .uncaughtSignal:
Diagnostics.error("Got uncaught signal while running in \(module)")
@unknown default:
Diagnostics.error("Stopped running in \(module) due to unexpected termination reason")
}
if process.terminationStatus != EXIT_SUCCESS {
Diagnostics.error("""
Command found error violations or unsuccessfully stopped running with \
exit code \(process.terminationStatus) in \(module)
"""
)
}
}
}