Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run command plugin in whole package if no targets defined #5789

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

#### Bug Fixes

* None.
* Run command plugin in whole package if no targets are defined in the
package manifest.
[SimplyDanny](https://github.com/SimplyDanny)
[#5787](https://github.com/realm/SwiftLint/issues/5787)

## 0.57.0: Squeaky Clean Cycle

Expand Down
64 changes: 37 additions & 27 deletions Plugins/SwiftLintCommandPlugin/SwiftLintCommandPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,52 @@ struct SwiftLintCommandPlugin: CommandPlugin {
let targets = targetNames.isEmpty
? context.package.targets
: try context.package.targets(named: targetNames)
let tool = try context.tool(named: "swiftlint")
guard !targets.isEmpty else {
try run(with: context, arguments: arguments)
return
}
for target in targets {
guard let target = target.sourceModule else {
Diagnostics.warning("Target '\(target.name)' is not a source module; skipping it")
continue
}
try run(in: target.directory.string, for: target.name, with: context, arguments: arguments)
}
}

let process = Process()
process.currentDirectoryURL = URL(fileURLWithPath: context.package.directory.string)
process.executableURL = URL(fileURLWithPath: tool.path.string)
process.arguments = arguments
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]
private func run(in directory: String = ".",
for targetName: String? = nil,
with context: PluginContext,
arguments: [String]) throws {
let process = Process()
process.currentDirectoryURL = URL(fileURLWithPath: context.package.directory.string)
process.executableURL = URL(fileURLWithPath: try context.tool(named: "swiftlint").path.string)
process.arguments = arguments
if !arguments.contains("analyze") {
// The analyze command does not support the `--cache-path` argument.
process.arguments! += ["--cache-path", "\(context.pluginWorkDirectory.string)"]
}
process.arguments! += [directory]

try process.run()
process.waitUntilExit()
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")
}
let module = targetName.map { "module '\($0)'" } ?? "package"
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 '\(target.name)'
"""
)
}
if process.terminationStatus != EXIT_SUCCESS {
Diagnostics.error("""
Command found error violations or unsuccessfully stopped running with \
exit code \(process.terminationStatus) in \(module)
"""
)
}
}
}