@@ -3,40 +3,51 @@ import PackagePlugin
33
44@main
55struct 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)
1617import XcodeProjectPlugin
1718
1819extension 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