0

I've managed to reproduce a build phase script from this SO question which increments the build number every time I build the app. Can anyone suggest how the script should look if I want the build number to be version.build, i.e. if my app version is 0.2, I'd like my build number to be set to 0.2.001, 0.2.002, etc. Then, when I change my build number to 0.3, I want the builds to restart to 001, so I get 0.3.001, 0.3.002, etc.

Here's my current script:

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

UPDATE

What I'm trying to do is the following:

  • Get the shortVersion from plist file (e.x. 0.2)
  • Get the buildNumber from plist file (e.x. 0.2.007)
  • Separate the buildNumber into 0.2 and 007
  • If 0.2 equals shortVersion, just increment 007 -> 008 and join the two again so the new buildNumber is 0.2.008
  • Else (e.x. shortVersion has been changed to 0.3), reset the build number to 001 and join the two together to get the new buildNumber as 0.3.001

Unfortunately, I know nothing about bash scripting, so I'd be grateful if someone can provide the script.

artooras
  • 6,315
  • 9
  • 45
  • 78

1 Answers1

-1
#!/bin/bash

# get the configuration
conf=${CONFIGURATION}

# only change build number if we are building for Release or AdHoc!
if [ $conf == Release ] || [ $conf == AdHoc ]; then

# get the build number
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
echo "Old Build number $buildNumber"

# split it by dots
arrIN=(${buildNumber//\./ })

# get the last element position
lastElementPosition=$((${#arrIN[@]} - 1))
# get the minor version from the last array element
minorVersion=${arrIN[${lastElementPosition}]}
# Increase it by 1
minorVersion=$((minorVersion+1))
# Format it as 3 digit
minorVersion=`printf "%03d" $minorVersion`
echo "New minor version $minorVersion"
# Update it on the array
arrIN[$lastElementPosition]=$minorVersion

# construct the build number now by joining the array
buildNumber=$(IFS=. ; echo "${arrIN[*]}")

echo "New Build number $buildNumber"

#update it in plist
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

fi

This is the script you want. Everything is explained on the code, so you should see what I'm doing

References for bash functions used in the above script:

Splitting string from: https://stackoverflow.com/a/5257398/312312

Padding number from: https://stackoverflow.com/a/8789779/312312

Joining array from: https://stackoverflow.com/a/9429887/312312

Community
  • 1
  • 1
Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • Hi. I've tried your script, but what it does is produce builds like 437.001, 438.001, etc. While the version number (just above the build number in Xcode) remains 0.2 – artooras Mar 29 '15 at 06:24
  • All the steps you need are explained in my code. When you change the build number to 0.3, you'll have to reset the build to 001 your self, as the bash script doesn't know the old major version. As for the version, you'll need to use the `CFBundleShortVersionString` key – Lefteris Mar 29 '15 at 10:31
  • But if you get the old build number, which contains 0.2, and compare it to the `CFBundleShortVersionString`, which is 0.3, then the counter could be reset to 001 by code, couldn't it? – artooras Mar 29 '15 at 15:35