Skip to content

Commit

Permalink
🔄 Workflow in 'realm/SwiftLint' synced local 'Plugins/' with remote '…
Browse files Browse the repository at this point in the history
…Plugins/'
  • Loading branch information
SimplyDanny committed Jul 24, 2024
1 parent 9073dad commit 2dc958c
Showing 1 changed file with 39 additions and 29 deletions.
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 2dc958c

Please sign in to comment.