From 115784c37112640cb702b8f6d89f32f59f63d437 Mon Sep 17 00:00:00 2001 From: Anders Bertelrud Date: Tue, 28 Mar 2023 16:16:26 -0700 Subject: [PATCH] Add more helpful contents to the newly generated build tool plugin --- Sources/Workspace/InitPackage.swift | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Sources/Workspace/InitPackage.swift b/Sources/Workspace/InitPackage.swift index 0ead81ea9bd..f45f3a222d2 100644 --- a/Sources/Workspace/InitPackage.swift +++ b/Sources/Workspace/InitPackage.swift @@ -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] + ) + } } }