Skip to content

Commit 965b2c2

Browse files
Change to prebuild command
1 parent 5cfaea1 commit 965b2c2

File tree

2 files changed

+54
-49
lines changed

2 files changed

+54
-49
lines changed

Package.swift

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,22 @@
22
import PackageDescription
33

44
let package = Package(
5-
name: "swiftlint",
6-
platforms: [
7-
.macOS(.v10_15),
8-
],
9-
products: [
10-
.plugin(
11-
name: "swiftlint",
12-
targets: ["swiftlint"]),
13-
],
14-
dependencies: [
15-
],
16-
targets: [
17-
.binaryTarget(
18-
name: "SwiftLintBinary",
19-
url: "https://github.com/realm/SwiftLint/releases/download/0.50.0-rc.2/SwiftLintBinary-macos.artifactbundle.zip",
20-
checksum: "c100354e0f6ea2531806df3199157e7e8a1eccb920d4da8e9a46f5f498aa4a6a"
21-
),
22-
23-
.plugin(
24-
name: "swiftlint",
25-
capability: .buildTool(),
26-
dependencies: ["SwiftLintBinary"],
27-
path: "."),
28-
]
5+
name: "SwiftLintPlugin",
6+
products: [
7+
.plugin(name: "SwiftLintPlugin", targets: ["SwiftLintPlugin"]),
8+
],
9+
dependencies: [],
10+
targets: [
11+
.plugin(
12+
name: "SwiftLintPlugin",
13+
capability: .buildTool(),
14+
dependencies: ["SwiftLintBinary"],
15+
path: "."
16+
),
17+
.binaryTarget(
18+
name: "SwiftLintBinary",
19+
url: "https://github.com/realm/SwiftLint/releases/download/0.52.4/SwiftLintBinary-macos.artifactbundle.zip",
20+
checksum: "8a8095e6235a07d00f34a9e500e7568b359f6f66a249f36d12cd846017a8c6f5"
21+
)
22+
]
2923
)

SwiftLintPlugin.swift

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,51 @@ import PackagePlugin
33

44
@main
55
struct SwiftLintPlugin: BuildToolPlugin {
6-
func createBuildCommands(
7-
context: PackagePlugin.PluginContext,
8-
target: PackagePlugin.Target
9-
) async throws -> [PackagePlugin.Command] {
10-
[try buildCommand(tool: try context.tool(named: "swiftlint"), workingDirectory: context.pluginWorkDirectory,
11-
targetFiles: [target.directory])]
6+
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
7+
// get the config on the root if the target directory
8+
return [try .SwiftLint(
9+
tool: try context.tool(named: "swiftlint"),
10+
outputDir: context.pluginWorkDirectory,
11+
targetFiles: [target.directory.string])]
1212
}
1313
}
1414

15+
1516
#if canImport(XcodeProjectPlugin)
1617
import XcodeProjectPlugin
1718

1819
extension SwiftLintPlugin: XcodeBuildToolPlugin {
19-
func createBuildCommands(
20-
context: XcodePluginContext,
21-
target: XcodeTarget
22-
) throws -> [Command] {
23-
[try buildCommand(tool: try context.tool(named: "swiftlint"), workingDirectory: context.pluginWorkDirectory,
24-
targetFiles: target.inputFiles.map(\.path))]
20+
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
21+
return [try .SwiftLint(
22+
tool: try context.tool(named: "swiftlint"),
23+
outputDir: context.pluginWorkDirectory,
24+
targetFiles: target.inputFiles.map(\.path.string).filter { $0.hasSuffix(".swift") })]
2525
}
2626
}
2727
#endif
2828

29-
private func buildCommand(tool: PackagePlugin.PluginContext.Tool,
30-
workingDirectory: PackagePlugin.Path,
31-
targetFiles: [PackagePlugin.Path]) throws -> PackagePlugin.Command {
32-
// create config in the working directory
33-
let configFile = workingDirectory.appending("swiftlist.yml").string
34-
try config.write(toFile: configFile, atomically: true, encoding: .utf8)
35-
// create build command
36-
var arguments = ["lint"]
37-
arguments.append(contentsOf: ["--cache-path", "\(workingDirectory)"])
38-
arguments.append(contentsOf: ["--config", "\(configFile)"])
39-
arguments.append(contentsOf: targetFiles.map(\.string).filter {$0.hasSuffix(".swift")})
40-
41-
return .buildCommand(displayName: "SwiftLint", executable: tool.path, arguments: arguments)
29+
private extension Command {
30+
static func SwiftLint(tool: PackagePlugin.PluginContext.Tool, outputDir: PackagePlugin.Path,
31+
targetFiles: [String]) throws-> Command {
32+
// generate configuration
33+
let toolDir = outputDir.removingLastComponent().appending("SwiftLintPlugin-config")
34+
try FileManager.default.createDirectory(atPath: toolDir.string, withIntermediateDirectories: true)
35+
let configPath = toolDir.appending("swiftlist.yml").string
36+
try config.write(toFile: configPath, atomically: true, encoding: .utf8)
37+
38+
var arguments = [
39+
"lint",
40+
"--cache-path", "\(outputDir)",
41+
"--config", "\(configPath)",
42+
]
43+
arguments.append(contentsOf: targetFiles)
44+
45+
return .prebuildCommand(
46+
displayName: "swiftLint \(targetFiles)",
47+
executable: tool.path,
48+
arguments: arguments,
49+
// We are not producing output files and this is needed only to not include cache files into bundle
50+
outputFilesDirectory: outputDir.appending("Output")
51+
)
52+
}
4253
}

0 commit comments

Comments
 (0)