I have a Swift PM target, here called XYZ
, which contains a single Swift file. Through a BuildToolPlugin
I would like to modify the original Swift source file and include the generated Swift source output in the build: original.swift -> (BuildTooldPlugin) -> modified.swift
.
The issue I'm running into right now is that the definitions of the original and the generated Swift file get included such that the build fails on: Invalid redeclaration of ..
.
How can I exclude the original unmodified source file from the build to prevent the duplicate def issue?
Target making use of the plugin:
.target(
name: "XYZ",
dependencies: [],
path: ".",
sources: ["XYZ"],
plugins: [.plugin(name: "XYZPlugin")]
),
The plugin:
@main
struct XYZPlugin: BuildToolPlugin {
func createBuildCommands(
context: PackagePlugin.PluginContext,
target: PackagePlugin.Target
) async throws -> [PackagePlugin.Command] {
guard let target = target as? SourceModuleTarget else { exit(1) }
let sources: [Path] = target.sourceFiles.filter { $0.type == .source }.map(\.path)
return sources.map { input in
let output = context.pluginWorkDirectory.appending(["\(input.stem)Modified.swift"])
return .buildCommand(
displayName: "XYZ: \(input)",
executable: Path("\(target.directory)/Plugins/XYZ.py"),
arguments: [input, output],
inputFiles: [input],
outputFiles: [output]
)
}
}
}