Skip to content

Commit 32501f6

Browse files
committed
Add more helpful contents to the newly generated build tool plugin
1 parent 6340897 commit 32501f6

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

Sources/Workspace/InitPackage.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,26 @@ public final class InitPackage {
407407
content += """
408408
struct \(typeName): BuildToolPlugin {
409409
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
410-
print("Hello, World!")
411-
return []
410+
// The plugin can choose what parts of the package to process.
411+
guard let sourceFiles = target.sourceModule?.sourceFiles else { return [] }
412+
413+
// Find the code generator tool to run (replace this with the actual one).
414+
let generatorTool = try context.tool(named: "my-code-generator")
415+
416+
// Construct a build command for each source file with a particular suffix.
417+
return sourceFiles.map(\\.path).compactMap { inputPath in
418+
guard inputPath.extension == "my-input-suffix" else { return .none }
419+
let inputName = inputPath.lastComponent
420+
let outputName = inputPath.stem + ".swift"
421+
let outputPath = context.pluginWorkDirectory.appending(outputName)
422+
return .buildCommand(
423+
displayName: "Generating \\(outputName) from \\(inputName)",
424+
executable: generatorTool.path,
425+
arguments: ["\\(inputPath)", "-o", "\\(outputPath)"],
426+
inputFiles: [inputPath],
427+
outputFiles: [outputPath]
428+
)
429+
}
412430
}
413431
}
414432

0 commit comments

Comments
 (0)