Support --target paths being passed to command plugin (#5696)

This commit is contained in:
Danny Mösch
2024-07-24 22:57:24 +02:00
committed by GitHub
parent b257cd2c17
commit 7904d9a438
2 changed files with 43 additions and 29 deletions
+4
View File
@@ -46,6 +46,10 @@
[SimplyDanny](https://github.com/SimplyDanny)
[#2120](https://github.com/realm/SwiftLint/issues/2120)
* Support `--target` paths being passed to command plugin by Xcode.
[SimplyDanny](https://github.com/SimplyDanny)
[#5603](https://github.com/realm/SwiftLint/issues/5603)
* Add modified configurations to examples in rule documentation.
[SimplyDanny](https://github.com/SimplyDanny)
@@ -3,38 +3,48 @@ import PackagePlugin
@main
struct SwiftLintCommandPlugin: CommandPlugin {
func performCommand(
context: PluginContext,
arguments: [String]
) throws {
let tool: PluginContext.Tool = try context.tool(named: "swiftlint")
// Caching is managed internally because the cache must be located within the `pluginWorkDirectory`.
if arguments.contains("--cache-path") {
Diagnostics.error("Setting Cache Path Not Allowed")
func performCommand(context: PluginContext, 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
}
let process: Process = .init()
process.currentDirectoryURL = URL(fileURLWithPath: context.package.directory.string)
process.executableURL = URL(fileURLWithPath: tool.path.string)
// The analyze command does not support the `--cache-path` argument
if arguments.contains("analyze") {
var argExtractor = ArgumentExtractor(arguments)
let targetNames = argExtractor.extractOption(named: "target")
let targets = targetNames.isEmpty
? context.package.targets
: try context.package.targets(named: targetNames)
let tool = try context.tool(named: "swiftlint")
for target in targets {
guard let target = target.sourceModule else {
Diagnostics.warning("Target '\(target.name)' is not a source module; skipping it")
continue
}
let process = Process()
process.currentDirectoryURL = URL(fileURLWithPath: context.package.directory.string)
process.executableURL = URL(fileURLWithPath: tool.path.string)
process.arguments = arguments
} else {
process.arguments = arguments + ["--cache-path", "\(context.pluginWorkDirectory.string)"]
}
try process.run()
process.waitUntilExit()
switch process.terminationReason {
case .exit:
break
case .uncaughtSignal:
Diagnostics.error("Uncaught Signal")
@unknown default:
Diagnostics.error("Unexpected Termination Reason")
}
guard process.terminationStatus == EXIT_SUCCESS else {
Diagnostics.error("Command Failed")
return
if !arguments.contains("analyze") {
// The analyze command does not support the `--cache-path` argument.
process.arguments! += ["--cache-path", "\(context.pluginWorkDirectory.string)"]
}
process.arguments! += [target.directory.string]
try process.run()
process.waitUntilExit()
switch process.terminationReason {
case .exit:
Diagnostics.remark("Finished running in module '\(target.name)'")
case .uncaughtSignal:
Diagnostics.error("Got uncaught signal while running in module '\(target.name)'")
@unknown default:
Diagnostics.error("Stopped running in module '\(target.name) due to unexpected termination reason")
}
if process.terminationStatus != EXIT_SUCCESS {
Diagnostics.warning(
"Command found violations or unsuccessfully stopped running in module '\(target.name)'"
)
}
}
}
}