Get current Scheme Name from Run Script Phase

后端 未结 3 1997
渐次进展
渐次进展 2021-02-05 04:41

Is there a way of grabbing the current scheme from a run script phase?

I\'ve tried $(SCHEME_NAME) but it doesn\'t exist.

相关标签:
3条回答
  • 2021-02-05 05:13

    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}
    

    Build Pre-action

    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).

    0 讨论(0)
  • 2021-02-05 05:14

    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")
    
    
    0 讨论(0)
  • 2021-02-05 05:28

    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"
    
    0 讨论(0)
提交回复
热议问题