-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftLintPlugin.swift
53 lines (46 loc) · 1.98 KB
/
SwiftLintPlugin.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import Foundation
import PackagePlugin
@main
struct SwiftLintPlugin: BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
// get the config on the root if the target directory
return [try .SwiftLint(
tool: try context.tool(named: "swiftlint"),
outputDir: context.pluginWorkDirectory,
targetFiles: [target.directory.string])]
}
}
#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin
extension SwiftLintPlugin: XcodeBuildToolPlugin {
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
return [try .SwiftLint(
tool: try context.tool(named: "swiftlint"),
outputDir: context.pluginWorkDirectory,
targetFiles: target.inputFiles.map(\.path.string).filter { $0.hasSuffix(".swift") })]
}
}
#endif
private extension Command {
static func SwiftLint(tool: PackagePlugin.PluginContext.Tool, outputDir: PackagePlugin.Path,
targetFiles: [String]) throws-> Command {
// generate configuration
let toolDir = outputDir.removingLastComponent().appending("SwiftLintPlugin-config")
try FileManager.default.createDirectory(atPath: toolDir.string, withIntermediateDirectories: true)
let configPath = toolDir.appending("swiftlist.yml").string
try config.write(toFile: configPath, atomically: true, encoding: .utf8)
var arguments = [
"lint",
"--cache-path", "\(outputDir)",
"--config", "\(configPath)",
]
arguments.append(contentsOf: targetFiles)
return .prebuildCommand(
displayName: "swiftLint \(targetFiles)",
executable: tool.path,
arguments: arguments,
// We are not producing output files and this is needed only to not include cache files into bundle
outputFilesDirectory: outputDir.appending("Output")
)
}
}