Extract common plugin execution code to method (#4690)
This commit is contained in:
parent
82249f5ed4
commit
f8f2317bdb
|
@ -3,46 +3,47 @@ import PackagePlugin
|
||||||
|
|
||||||
@main
|
@main
|
||||||
struct SwiftLintPlugin: BuildToolPlugin {
|
struct SwiftLintPlugin: BuildToolPlugin {
|
||||||
func createBuildCommands(
|
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
|
||||||
context: PackagePlugin.PluginContext,
|
|
||||||
target: PackagePlugin.Target
|
|
||||||
) async throws -> [PackagePlugin.Command] {
|
|
||||||
guard let sourceTarget = target as? SourceModuleTarget else {
|
guard let sourceTarget = target as? SourceModuleTarget else {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
return createBuildCommands(
|
||||||
|
inputFiles: sourceTarget.sourceFiles(withSuffix: "swift").map(\.path),
|
||||||
|
packageDirectory: context.package.directory,
|
||||||
|
workingDirectory: context.pluginWorkDirectory,
|
||||||
|
tool: try context.tool(named: "swiftlint")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
let inputFilePaths = sourceTarget.sourceFiles(withSuffix: "swift")
|
private func createBuildCommands(inputFiles: [Path],
|
||||||
.map(\.path)
|
packageDirectory: Path,
|
||||||
|
workingDirectory: Path,
|
||||||
guard inputFilePaths.isEmpty == false else {
|
tool: PluginContext.Tool) -> [Command] {
|
||||||
|
if inputFiles.isEmpty {
|
||||||
// Don't lint anything if there are no Swift source files in this target
|
// Don't lint anything if there are no Swift source files in this target
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
let swiftlint = try context.tool(named: "swiftlint")
|
var arguments = [
|
||||||
var arguments: [String] = [
|
|
||||||
"lint",
|
"lint",
|
||||||
"--quiet",
|
"--quiet",
|
||||||
"--cache-path", "\(context.pluginWorkDirectory)"
|
"--cache-path", "\(workingDirectory)"
|
||||||
]
|
]
|
||||||
|
|
||||||
// Manually look for configuration files, to avoid issues when the plugin does not execute our tool from the
|
// Manually look for configuration files, to avoid issues when the plugin does not execute our tool from the
|
||||||
// package source directory.
|
// package source directory.
|
||||||
if let configuration = context.package.directory.firstConfigurationFileInParentDirectories() {
|
if let configuration = packageDirectory.firstConfigurationFileInParentDirectories() {
|
||||||
arguments.append(contentsOf: [
|
arguments.append(contentsOf: ["--config", "\(configuration.string)"])
|
||||||
"--config", "\(configuration.string)"
|
|
||||||
])
|
|
||||||
}
|
}
|
||||||
|
arguments += inputFiles.map(\.string)
|
||||||
arguments += inputFilePaths.map(\.string)
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
.buildCommand(
|
.buildCommand(
|
||||||
displayName: "SwiftLint",
|
displayName: "SwiftLint",
|
||||||
executable: swiftlint.path,
|
executable: tool.path,
|
||||||
arguments: arguments,
|
arguments: arguments,
|
||||||
inputFiles: inputFilePaths,
|
inputFiles: inputFiles,
|
||||||
outputFiles: [context.pluginWorkDirectory]
|
outputFiles: [workingDirectory]
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -52,45 +53,16 @@ struct SwiftLintPlugin: BuildToolPlugin {
|
||||||
import XcodeProjectPlugin
|
import XcodeProjectPlugin
|
||||||
|
|
||||||
extension SwiftLintPlugin: XcodeBuildToolPlugin {
|
extension SwiftLintPlugin: XcodeBuildToolPlugin {
|
||||||
func createBuildCommands(
|
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
|
||||||
context: XcodePluginContext,
|
|
||||||
target: XcodeTarget
|
|
||||||
) throws -> [Command] {
|
|
||||||
let inputFilePaths = target.inputFiles
|
let inputFilePaths = target.inputFiles
|
||||||
.filter { $0.type == .source && $0.path.extension == "swift" }
|
.filter { $0.type == .source && $0.path.extension == "swift" }
|
||||||
.map(\.path)
|
.map(\.path)
|
||||||
|
return createBuildCommands(
|
||||||
guard inputFilePaths.isEmpty == false else {
|
inputFiles: inputFilePaths,
|
||||||
// Don't lint anything if there are no Swift source files in this target
|
packageDirectory: context.xcodeProject.directory,
|
||||||
return []
|
workingDirectory: context.pluginWorkDirectory,
|
||||||
}
|
tool: try context.tool(named: "swiftlint")
|
||||||
|
)
|
||||||
let swiftlint = try context.tool(named: "swiftlint")
|
|
||||||
var arguments: [String] = [
|
|
||||||
"lint",
|
|
||||||
"--quiet",
|
|
||||||
"--cache-path", "\(context.pluginWorkDirectory)"
|
|
||||||
]
|
|
||||||
|
|
||||||
// Xcode build tool plugins don't seem to run from the project source directory, so our auto-discovery of
|
|
||||||
// configuration files doesn't work. We approximate it here.
|
|
||||||
if let configuration = context.xcodeProject.directory.firstConfigurationFileInParentDirectories() {
|
|
||||||
arguments.append(contentsOf: [
|
|
||||||
"--config", "\(configuration.string)"
|
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
arguments += inputFilePaths.map(\.string)
|
|
||||||
|
|
||||||
return [
|
|
||||||
.buildCommand(
|
|
||||||
displayName: "SwiftLint",
|
|
||||||
executable: swiftlint.path,
|
|
||||||
arguments: arguments,
|
|
||||||
inputFiles: inputFilePaths,
|
|
||||||
outputFiles: [context.pluginWorkDirectory]
|
|
||||||
)
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue