Files
SwiftLint/Plugins/SwiftLintPlugin/SwiftLintPlugin.swift
T
Tony Arnold ab143685a4 Use a binary target for the build tool plugin (#4603)
* Use a binary target for the build tool plugin

* Merge `push_version` and `release` make commands

Instead of running `make push_version "0.2.0: Tumble Dry"` and then
`make release`, now run `make release "0.2.0: Tumble Dry"`, which will
build the release artifacts and update/push the new version to GitHub.

This allows the artifacts to use the new version, update the artifact
bundle checksum in the package manifest, then tag the release.

The Releasing.md instructions were updated to reflect this new workflow.

* Add `SwiftLintSourcePlugin` source plugin for SwiftPM

* Add changelog entry

* Remove SwiftLintSourcePlugin for now

* Build from Source on Linux

* Use a lower-level method of checking if a file is accessible

This shouldn’t trigger sandbox violations, I hope…

* Prevent an infinite recursion of the filesystem root

* Remove unnecessary logging

* Quieten the output so that Xcode only prints violations

* Break up comment to avoid line length warning

* Fix capitalization of Glibc import

Co-authored-by: JP Simard <jp@jpsim.com>
2022-11-29 18:10:47 -05:00

97 lines
3.0 KiB
Swift

import Foundation
import PackagePlugin
@main
struct SwiftLintPlugin: BuildToolPlugin {
func createBuildCommands(
context: PackagePlugin.PluginContext,
target: PackagePlugin.Target
) async throws -> [PackagePlugin.Command] {
guard let sourceTarget = target as? SourceModuleTarget else {
return []
}
let inputFilePaths = sourceTarget.sourceFiles(withSuffix: "swift")
.map(\.path)
guard inputFilePaths.isEmpty == false else {
// Don't lint anything if there are no Swift source files in this target
return []
}
let swiftlint = try context.tool(named: "swiftlint")
var arguments: [String] = [
"lint",
"--quiet",
"--cache-path", "\(context.pluginWorkDirectory)"
]
// Manually look for configuration files, to avoid issues when the plugin does not execute our tool from the
// package source directory.
if let configuration = context.package.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]
)
]
}
}
#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)
guard inputFilePaths.isEmpty == false else {
// Don't lint anything if there are no Swift source files in this target
return []
}
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