1
1
import PackagePlugin
2
+ import Foundation
2
3
3
4
@main
4
5
struct SimpleBuildToolPlugin : BuildToolPlugin {
@@ -9,28 +10,35 @@ struct SimpleBuildToolPlugin: BuildToolPlugin {
9
10
10
11
// Construct a build command for each source file with a particular suffix.
11
12
return sourceFiles. map ( \. path) . compactMap {
12
- createBuildCommand ( for: $0, in: context. pluginWorkDirectory, with: generatorTool. path)
13
+ createBuildCommand (
14
+ for: $0,
15
+ in: context. pluginWorkDirectory,
16
+ with: generatorTool. path
17
+ )
13
18
}
14
19
}
15
20
16
21
/// Calls a build tool that transforms JSON files into Swift files.
17
22
func createBuildCommand( for inputPath: Path , in outputDirectoryPath: Path , with generatorToolPath: Path ) -> Command ? {
23
+ let inputURL = URL ( fileURLWithPath: inputPath. string)
24
+ let outputDirectoryURL = URL ( fileURLWithPath: outputDirectoryPath. string)
25
+
18
26
// Skip any file that doesn't have the extension we're looking for (replace this with the actual one).
19
- guard inputPath . extension == " json " else { return . none }
27
+ guard inputURL . pathExtension == " json " else { return . none }
20
28
21
29
// Produces .swift files in the same directory structure as the input JSON files appear in the target.
22
- let components = inputPath . string . split ( separator: " LibraryTarget " , omittingEmptySubsequences: false ) . map ( String . init)
23
- let inputName = inputPath . lastComponent
24
- let outputDir = outputDirectoryPath . appending ( components [ 1 ] ) . removingLastComponent ( )
25
- let outputName = inputPath . stem + " .swift "
26
- let outputPath = outputDir. appending ( outputName)
30
+ let components = inputURL . absoluteString . split ( separator: " LibraryTarget " , omittingEmptySubsequences: false ) . map ( String . init)
31
+ let inputName = inputURL . lastPathComponent
32
+ let outputDir = outputDirectoryURL . appending ( path : components [ 1 ] ) . deletingLastPathComponent ( )
33
+ let outputName = inputURL . deletingPathExtension ( ) . lastPathComponent + " .swift "
34
+ let outputURL = outputDir. appending ( path : outputName)
27
35
28
36
return . buildCommand(
29
37
displayName: " Generating \( outputName) from \( inputName) " ,
30
38
executable: generatorToolPath,
31
- arguments: [ " \( inputPath) " , " \( outputPath ) " ] ,
39
+ arguments: [ " \( inputPath) " , " \( outputURL . path ) " ] ,
32
40
inputFiles: [ inputPath] ,
33
- outputFiles: [ outputPath ]
41
+ outputFiles: [ Path ( outputURL . path ) ] ,
34
42
)
35
43
}
36
44
}
0 commit comments