Swift package buildtool plugin cannot accept arguments nor environment variables

I have a library that contains code that is brand specific. When I build a buildtool plugin it should accept a parameter during the build to produce the correct source files for that brand in the build output directory. I can do this by adding the parameter for the brand to the arguments of the buildtool plugin.

import PackagePlugin
import Foundation

@main
struct BrandPicker: BuildToolPlugin {
    func createBuildCommands(
        context: PackagePlugin.PluginContext,
        target: PackagePlugin.Target
    ) async throws -> [PackagePlugin.Command] {
        // We only care about the source module targets
        guard let target = target as? SourceModuleTarget else {
            return []
        }
        let input = target.directory.appending("Common.swift")
        let output = context.pluginWorkDirectory.appending(["CommonOutput.swift"])
      
        return [
            .buildCommand(
            displayName: "Add branded files",
            executable: try context.tool(named: "<#main#>").path,
            arguments: [output.string] + ["<#brand#>"],
            inputFiles: [input],
            outputFiles: [output]
        )
        ]
    }
}

I seam to only be able to harcode the <#brand#> parameter into the build process as during the build arguments or environment variables I add to the scheme that builds this target get ignored. My maybe naive mind was expecting things I put into the scheme to build the library to get passed to the ProcessInfo().arguments, but they do not apparently. I guess this is the intended way of working? Or could this be a feature request?

3 Likes

Any workaround? I was also trying to see if there was a way to get access to the Xcode target build settings but this also appears to not be possible.

Yeah the workaround is to have a file in sources that contains the settings. For now it is a json file. This we read. This is not rely a solution for a swift package that you deliver to a client and would like to make configurable as the json files in source are read only.

1 Like