Skip to content

Commit

Permalink
Add more helpful contents to the newly generated build tool plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
abertelrud committed Mar 28, 2023
1 parent 6340897 commit 115784c
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions Sources/Workspace/InitPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,26 @@ public final class InitPackage {
content += """
struct \(typeName): BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
print("Hello, World!")
return []
// The plugin can choose what parts of the package to process.
guard let target = target as? SourceModuleTarget else { return [] }
// Find the code generator tool to run (replace this with the actual one).
let generatorTool = try context.tool(named: "my-code-generator")
// Construct a build command for each source file with a particular suffix.
return target.sourceFiles.map(\\.path).compactMap { inputPath in
guard inputPath.extension == "my-input-suffix" else { return .none }
let inputName = inputPath.lastComponent
let outputName = inputPath.stem + ".swift"
let outputPath = context.pluginWorkDirectory.appending(outputName)
return .buildCommand(
displayName: "Generating \\(outputName) from \\(inputName)",
executable: generatorTool.path,
arguments: ["\\(inputPath)", "-o", "\\(outputPath)"],
inputFiles: [inputPath],
outputFiles: [outputPath]
)
}
}
}
Expand Down

0 comments on commit 115784c

Please sign in to comment.