I am trying to get SwiftGen working at a buildCommand Build Tool Plugin. I forked Anders' example repository:
and I got it working in this example repository. To see it in action, just do the following:
- Open "SPMBuildToolPlugins.xcworkspace" in Xcode
- Choose the "ExampleApp" scheme
- Build and run.
You can see in the Build ouput that Xcode is writing a script file and then running it:
However, when I implemented this inside of my own project, I noticed that that the generated script looks like this:
#!/bin/bash
ASSETS_OUTPUT_FILE=/Users/ehyche/Library/Developer/Xcode/DerivedData/Noom-durciaqeajcieegapeqmlkivmlux/SourcePackages/coach-ios/NoomOnboardingUI/SwiftGenPlugin/SwiftGenOutputs/Media.swift
TARGET_NAME=NoomOnboardingUI
STRINGS_OUTPUT_FILE=/Users/ehyche/Library/Developer/Xcode/DerivedData/Noom-durciaqeajcieegapeqmlkivmlux/SourcePackages/coach-ios/NoomOnboardingUI/SwiftGenPlugin/SwiftGenOutputs/Strings.swift
PACKAGE_DIR=/Users/ehyche/src/github/noom/coach-ios
STRINGS_INPUT_DIR=/Users/ehyche/src/github/noom/coach-ios/Sources/NoomOnboardingUI/Resources/en.lproj
/usr/bin/sandbox-exec -p "(version 1)
(deny default)
(import \"system.sb\")
(allow file-read*)
(allow process*)
(allow file-write*
(subpath \"/private/tmp\")
(subpath \"/private/var/folders/lr/bztrcn1x42l5q8fwwyh2422c0000gp/T\")
)
(deny file-write*
(subpath \"/Users/ehyche/src/github/noom/coach-ios\")
)
(allow file-write*
(subpath \"/Users/ehyche/Library/Developer/Xcode/DerivedData/Noom-durciaqeajcieegapeqmlkivmlux/SourcePackages/coach-ios/NoomOnboardingUI/SwiftGenPlugin\")
)
" /Users/ehyche/src/github/noom/coach-ios/Binaries/SwiftGenBinary.artifactbundle/swiftgen-6.5.1/bin/swiftgen config run --verbose --config /Users/ehyche/src/github/noom/coach-ios/Sources/NoomOnboardingUI/Resources/swiftgen_spm_plugin.yml
But when swiftgen runs, it does not see the environment variables like ASSETS_OUTPUT_FILE, STRINGS_OUTPUT_FILE, etc. So it does not do any substitution and the resulting command looks like:
swiftgen strings --templateName structured-swift5 --output ${STRINGS_OUTPUT_FILE} --filter .+\.strings$ ${STRINGS_INPUT_DIR}
which, of course, fails.
I noticed that if I manually changed the generated script to include export
in front of each environment variable:
#!/bin/bash
export ASSETS_OUTPUT_FILE=/Users/ehyche/Library/Developer/Xcode/DerivedData/Noom-durciaqeajcieegapeqmlkivmlux/SourcePackages/coach-ios/NoomOnboardingUI/SwiftGenPlugin/SwiftGenOutputs/Media.swift
export TARGET_NAME=NoomOnboardingUI
export STRINGS_OUTPUT_FILE=/Users/ehyche/Library/Developer/Xcode/DerivedData/Noom-durciaqeajcieegapeqmlkivmlux/SourcePackages/coach-ios/NoomOnboardingUI/SwiftGenPlugin/SwiftGenOutputs/Strings.swift
export PACKAGE_DIR=/Users/ehyche/src/github/noom/coach-ios
export STRINGS_INPUT_DIR=/Users/ehyche/src/github/noom/coach-ios/Sources/NoomOnboardingUI/Resources/en.lproj
then it works just fine.
It seems like my alternatives are:
- Don't use the swiftgen.yml file at all, and just produce separate
.buildCommand
s for strings and assets. Those commands would have everything that is needed inside the arguments which I control. - Figure out why the generated script doesn't work.
So my questions are:
- Is the script generation an SPM thing or an Xcode thing?
- Is there anyway to influence how that script is generated?
Thanks!
Eric