69 lines
2.4 KiB
Swift
69 lines
2.4 KiB
Swift
import Foundation
|
|
import PackagePlugin
|
|
|
|
@main
|
|
struct SwiftLintPlugin: BuildToolPlugin {
|
|
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
|
|
guard let sourceTarget = target as? SourceModuleTarget else {
|
|
return []
|
|
}
|
|
return createBuildCommands(
|
|
inputFiles: sourceTarget.sourceFiles(withSuffix: "swift").map(\.path),
|
|
packageDirectory: context.package.directory,
|
|
workingDirectory: context.pluginWorkDirectory,
|
|
tool: try context.tool(named: "swiftlint")
|
|
)
|
|
}
|
|
|
|
private func createBuildCommands(inputFiles: [Path],
|
|
packageDirectory: Path,
|
|
workingDirectory: Path,
|
|
tool: PluginContext.Tool) -> [Command] {
|
|
if inputFiles.isEmpty {
|
|
// Don't lint anything if there are no Swift source files in this target
|
|
return []
|
|
}
|
|
|
|
var arguments = [
|
|
"lint",
|
|
"--quiet",
|
|
"--cache-path", "\(workingDirectory)"
|
|
]
|
|
|
|
// Manually look for configuration files, to avoid issues when the plugin does not execute our tool from the
|
|
// package source directory.
|
|
if let configuration = packageDirectory.firstConfigurationFileInParentDirectories() {
|
|
arguments.append(contentsOf: ["--config", "\(configuration.string)"])
|
|
}
|
|
arguments += inputFiles.map(\.string)
|
|
|
|
return [
|
|
.buildCommand(
|
|
displayName: "SwiftLint",
|
|
executable: tool.path,
|
|
arguments: arguments,
|
|
inputFiles: inputFiles,
|
|
outputFiles: [workingDirectory]
|
|
)
|
|
]
|
|
}
|
|
}
|
|
|
|
#if canImport(XcodeProjectPlugin)
|
|
import XcodeProjectPlugin
|
|
|
|
extension SwiftLintPlugin: XcodeBuildToolPlugin {
|
|
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
|
|
let inputFilePaths = target.inputFiles
|
|
.filter { $0.type == .source && $0.path.extension == "swift" }
|
|
.map(\.path)
|
|
return createBuildCommands(
|
|
inputFiles: inputFilePaths,
|
|
packageDirectory: context.xcodeProject.directory,
|
|
workingDirectory: context.pluginWorkDirectory,
|
|
tool: try context.tool(named: "swiftlint")
|
|
)
|
|
}
|
|
}
|
|
#endif
|