Skip to content

Commit

Permalink
Support --target paths being passed to command plugin (#5696)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Jul 24, 2024
1 parent b257cd2 commit 7904d9a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
68 changes: 39 additions & 29 deletions Plugins/SwiftLintCommandPlugin/SwiftLintCommandPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)'"
)
}
}
}
}

0 comments on commit 7904d9a

Please sign in to comment.