-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* CONFIG: Updates to build * Fixed workflow * Fixed security alert * Update documentation and README
- Loading branch information
1 parent
a81647f
commit 764e59e
Showing
9 changed files
with
235 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Foundation | ||
import PackagePlugin | ||
|
||
@main | ||
struct SwiftLintCommandPlugin: CommandPlugin { | ||
|
||
func performCommand(context: PluginContext, arguments: [String]) throws { | ||
let swiftlintTool = try context.tool(named: "swiftlint") | ||
let swiftlintExecutableURL = URL(fileURLWithPath: swiftlintTool.path.string) | ||
var swiftlintArguments = [ | ||
"lint", | ||
"--cache-path", | ||
context.pluginWorkDirectory.string | ||
] | ||
|
||
var argumentExtractor = ArgumentExtractor(arguments) | ||
|
||
if let configFile = argumentExtractor.extractOption(named: "config").first { | ||
swiftlintArguments.append(contentsOf: ["--config", configFile]) | ||
} | ||
|
||
if let reporter = argumentExtractor.extractOption(named: "reporter").first { | ||
swiftlintArguments.append(contentsOf: ["--reporter", reporter]) | ||
} | ||
|
||
if argumentExtractor.extractFlag(named: "strict") > 0 { | ||
swiftlintArguments.append("--strict") | ||
} | ||
|
||
let process = try Process.run(swiftlintExecutableURL, arguments: swiftlintArguments) | ||
process.waitUntilExit() | ||
|
||
if process.terminationStatus != 0 { | ||
Diagnostics.error("'swiftlint' failed") | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Darwin | ||
import Foundation | ||
import PackagePlugin | ||
|
||
extension Path { | ||
|
||
/// Scans the receiver, then all of its parents looking for a configuration file with the name ".swiftlint.yml". | ||
/// | ||
/// - returns: Path to the configuration file, or nil if one cannot be found. | ||
func firstConfigurationFileInParentDirectories() -> Path? { | ||
let defaultConfigurationFileName = ".swiftlint.yml" | ||
let proposedDirectory = sequence( | ||
first: self, | ||
next: { path in | ||
guard path.stem.count > 1 else { | ||
// Check we're not at the root of this filesystem, as `removingLastComponent()` | ||
// will continually return the root from itself. | ||
return nil | ||
} | ||
|
||
return path.removingLastComponent() | ||
} | ||
).first { path in | ||
let potentialConfigurationFile = path.appending(subpath: defaultConfigurationFileName) | ||
return potentialConfigurationFile.isAccessible() | ||
} | ||
return proposedDirectory?.appending(subpath: defaultConfigurationFileName) | ||
} | ||
|
||
/// Safe way to check if the file is accessible from within the current process sandbox. | ||
private func isAccessible() -> Bool { | ||
let result = string.withCString { pointer in | ||
access(pointer, R_OK) | ||
} | ||
|
||
return result == 0 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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) | ||
|
||
// We are not producing output files and this is needed only to not include cache files into bundle | ||
let outputFilesDirectory = workingDirectory.appending("Output") | ||
|
||
return [ | ||
.prebuildCommand( | ||
displayName: "SwiftLint", | ||
executable: tool.path, | ||
arguments: arguments, | ||
outputFilesDirectory: outputFilesDirectory | ||
) | ||
] | ||
} | ||
|
||
} | ||
|
||
#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 |
Oops, something went wrong.