Search code examples
xcode10

Xcode 10: Running Set Build Number script fails with "No such file or directory"


When I input my set build number script in the Shell text field in the Run Script section of Build Phases it works perfectly. When I put the script in a file and try to run it I get a "no such directory or file" error.

The script:

#!/bin/bash
git=`sh /etc/profile; which git`
appBuild=`"$git" rev-list HEAD --count`

if [ $CONFIGURATION = "Debug" ]; then
branchName=`"$git" rev-parse --abbrev-ref HEAD`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $appBuild-$branchName" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $appBuild-$branchName" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}.dSYM/Contents/Info.plist"
else
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $appBuild" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $appBuild" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}.dSYM/Contents/Info.plist"
fi

echo "Incremented the build number ${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"

done

I put that script in ./scripts/set-build-number.sh (doing chmod 755 on the folder and file) and put $(SRCROOT)/scripts/set-build-number in the shell text field. That's when I get the build error. If I put that path in the Input Files section below the Shell text field then nothing happens. What's wrong?

Build Messages

Normal

Set: Entry, ":CFBundleVersion", Does Not Exist
File Doesn't Exist, Will Create: /Users/XXXX/Library/Developer/Xcode/DerivedData/YYYY-czbuuppxwtizikbcswqcwzqtvnri/Build/Products/Debug-iphonesimulator/YYYY.app.dSYM/Contents/Info.plist
Incremented the build number /Users/XXXX/Library/Developer/Xcode/DerivedData/YYYY-czbuuppxwtizikbcswqcwzqtvnri/Build/Products/Debug-iphonesimulator/YYYY.app/Info.plist

With $(SRCROOT)/scripts/set-build-number in script text field.

/Users/XXXX/Library/Developer/Xcode/DerivedData/YYYY-czbuuppxwtizikbcswqcwzqtvnri/Build/Intermediates.noindex/YYYY.build/Debug-iphonesimulator/YYYY.build/Script-652071B32183E6C100A92117.sh: line 25: SRCROOT: command not found
/Users/XXXX/Library/Developer/Xcode/DerivedData/YYYY-czbuuppxwtizikbcswqcwzqtvnri/Build/Intermediates.noindex/YYYY.build/Debug-iphonesimulator/YYYY.build/Script-652071B32183E6C100A92117.sh: line 25: /scripts/set-build-number: No such file or directory
Command PhaseScriptExecution failed with a nonzero exit code

With $(SRCROOT)/scripts/set-build-number in Input Files.

Run custom shell script 'Run Script' 0.1 second

Solution

    1. $(SRCROOT) doesn't play well with spaces. I removed all spaces from project folder name.
    2. I rewrote the script.
    
        #!/bin/bash
        # Set the build number to the current git commit count.
        # If we're using the Dev scheme then we'll suffix the build number with the
        # current branch name to make collisions far less likely across feature branches.
    
        git=$(sh /etc/profile; which git)
        appBuild=$("$git" rev-list HEAD --count)
        branchName=$("$git" rev-parse --abbrev-ref HEAD)
        dsym_plist="$DWARF_DSYM_FOLDER_PATH/$DWARF_DSYM_FILE_NAME/Contents/Info.plist"
        target_plist="$TARGET_BUILD_DIR/$INFOPLIST_PATH"
    
        for plist in "$target_plist" "$dsym_plist"; do
         if [ -f "$plist" ]; then
          if [ $CONFIGURATION = "Debug" ]; then
    
           /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $appBuild-$branchName" "$plist"
           echo "Build number set to $appBuild-$branchName in ${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
    
          else
    
           /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $appBuild" "$plist"
           echo "Build number set to $appBuild in ${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
    
          fi
         fi
    
        done
    
    
    1. Put $SRCROOT/BuildScripts/set-build-number.sh in the shell text box.