Discarding inputs of a Swift Build Tool Plugin in the final product

Hi! I'm trying to build a Swift Build Tool Plugin that works both with Swift Packages and Xcode, the general idea is that it will get as input some JavaScript files and validate, mangle and merge them in a unified output file. This all works well, but I need to make sure the input files are not added to the final product. Is this possible to do in with the plugin?

My idea was to replace an existing Custom Build Script with this plugin but I don't see a way to do this only with the plugin currently.

Hi, I'm pretty new to plugins, but I think I just did something similar as a test.

I have a folder with a bunch of files that is excluded in the package that is then used by a plugin to create a single code file. None of the .txt files go into the build.

I followed along with WWDC 2022 Create Swift Package plugins, but I also recommend doing a search for " func createBuildCommands(" and other plugin related commands on github to see what other folks are doing.

from: PluginExplorer/Package.swift at main · carlynorama/PluginExplorer · GitHub

            dependencies: [
                .product(name: "ArgumentParser", package: "swift-argument-parser"),
            exclude: ["Data"],
            plugins: ["MyInBuildPlugin", "MyPreBuildPlugin"]

from: PluginExplorer/Plugins/MyInBuildPlugin/plugin.swift at main · carlynorama/PluginExplorer · GitHub

struct MyInBuildPlugin:BuildToolPlugin {
    func createBuildCommands(context: PackagePlugin.PluginContext, target: PackagePlugin.Target) async throws -> [PackagePlugin.Command] {
        //if target doesn't have source files, don't run the tool. 
        guard let target = target as? SourceModuleTarget else {
            return []
        let dataDirectory = target.directory.appending(["Data"]) 
        let dataContents = try FileManager.default.contentsOfDirectory(atPath: dataDirectory.string).map { fileName in

        let outputFileName = "FruitStore.swift"
        let outputFiles:[Path] = [context.pluginWorkDirectory.appending([outputFileName])]
        return [.buildCommand(displayName: "Build the FruitStore",
                                         executable: try context.tool(named: "MyInBuildPluginTool").path,
                                         arguments: [dataDirectory.string, context.pluginWorkDirectory.string, outputFileName],
                                         inputFiles: dataContents,
                                         outputFiles: outputFiles)]

Unfortunately this is not related to my issue I think. I want to remove the source files from the output target.

I'm still not sure I'm clear on the layout of your project and what folders do and do not need changing, but in general if the Source folder of the Target is going to be changed, use a command plugin instead of a build plugin unless you are in a position to turn off the sandbox.

This is what I know about command plugins: What if instead of a CLI, Plugins? Part 1, Command plugins

Hope that helps!