I have a Swift Package Build Tool Plugin to generate localizations and design tokens, which looks like this currently.
// Copyright © 2023 MEGA Limited. All rights reserved.
import PackagePlugin
@main
struct GenerateTokenBuildTool: BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
guard let target = target as? SourceModuleTarget else { return [] }
return try target.sourceFiles(withSuffix: "xcassets").map { assetCatalog in
let base = assetCatalog.path.stem
let input = assetCatalog.path
let output = context.pluginWorkDirectory.appending(["\(base).swift"])
// Use this when archiving for MacCatalyst
let tool = try context.tool(named: "AssetTokenGenerator").path
let toolName = tool.lastComponent
let path = tool.removingLastComponent()
let macPath = path.removingLastComponent().appending(subpath: path.lastComponent + "-maccatalyst")
let executable = macPath.appending(subpath: toolName)
// // Use this when archiving for iOS/iPadOS
// let executable = try context.tool(named: "AssetTokenGenerator").path
return .buildCommand(
displayName: "Generating tokens for \(base).xcassets",
executable: executable,
arguments: [input.string, output.string],
inputFiles: [input],
outputFiles: [output]
)
}
}
}
If you notice, I need to do a bit of manual bug fixing when determining the path of my executable.
When building for Mac Catalyst, context.tool(named:).path is pointing to an incorrect folder, thus failing the build for my project with this error:
Command PhaseScriptExecution failed with a nonzero exit code
sandbox-exec: execvp() of '//Users/mega-jfe/Library/Developer/Xcode/DerivedData/MEGAVPN-efdcwfcaosxfvneqjuvhrvdmbkax/Build/Products/Debug/AssetTokenGenerator' failed: No such file or directory
It's suppposed to be using the Debug-maccatalyst folder, but Xcode is pointing the path to Debug.
I feel like this is a bug, or else please let me know how I can handle this so that I can build for both without manually changing the code when building for Catalyst.
Thanks!