From 7904d9a4389ccfeee8df5b0093613a1f6cfeefad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danny=20M=C3=B6sch?= Date: Wed, 24 Jul 2024 22:57:24 +0200 Subject: [PATCH] Support `--target` paths being passed to command plugin (#5696) --- CHANGELOG.md | 4 ++ .../SwiftLintCommandPlugin.swift | 68 +++++++++++-------- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73c18130f6..2eaaf5c738 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/Plugins/SwiftLintCommandPlugin/SwiftLintCommandPlugin.swift b/Plugins/SwiftLintCommandPlugin/SwiftLintCommandPlugin.swift index 7ad7275dd5..3ff555170e 100644 --- a/Plugins/SwiftLintCommandPlugin/SwiftLintCommandPlugin.swift +++ b/Plugins/SwiftLintCommandPlugin/SwiftLintCommandPlugin.swift @@ -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)'" + ) + } } } }