Is there a way of grabbing the current scheme from a run script phase?
I\'ve tried $(SCHEME_NAME)
but it doesn\'t exist.
I couldn't find an environment variable to use, so I had to develop a work around: write a scheme name to disk in the Build Pre-action and then read it back out in the Run Script phase.
For each scheme you're interested in go to Edit Scheme and add a script with the following code:
rm -rf ${INTERMEDIATES_OUTPUT_DIR}
mkdir -p ${INTERMEDIATES_OUTPUT_DIR}
echo MY_SCHEME_NAME > ${SCHEME_FILE}
Next, go to your build target's "Build Settings" and add two "User-Defined Settings":
INTERMEDIATES_OUTPUT_DIR=${PROJECT_DIR}/build/intermediates/${CONFIGURATION}/
SCHEME_FILE=${INTERMEDIATES_OUTPUT_DIR}current_scheme.txt
Open up your "Run script" and add this:
SCHEME_NAME=`cat ${SCHEME_FILE}`
Be sure to add the intermediates build directory to your .gitignore file.
Obviously you could simplify this a bit by hardcoding a file name but this is a bit more robust (and we have other stuff that ends up in the intermediates directory, too).
You can write the scheme name to the info.plist
file and read it back in Run Script Phase, in Edit Scheme menu, choose Build -> Pre-actions and add the following script:
/usr/libexec/PlistBuddy -c "Set :SchemeName \"$SCHEME_NAME\"" "$INFOPLIST_FILE"
and then add the key SchemeName
of type string in the info.plist
, and its initial value is empty.
Finally, in the run script phase add the following:
SCHEME_NAME=$(/usr/libexec/PlistBuddy -c "Print SchemeName" "$INFOPLIST_FILE")
INTERMEDIATES_OUTPUT_DIR doesn't seem to work for building swift previews which I wanted customization on as well. I ended up doing something similar using defaults write
which I found works in all cases and doesn't involve the creation of a file.
Prebuild:
defaults write com.apple.dt.Xcode LastBuildScheme "MySchemeName"`
Build Script:
[[ $(defaults read com.apple.dt.Xcode LastBuildScheme) = "MySchemeName" ]] && echo "Scheme" || echo "Not Scheme"