Automatic Build Numbers In Xcode

As part of the project I’m working on at the moment I wanted a way to automatically update a targets build number in Xcode. My preferred setting for the build number is the current SCM revision, the script below automatically updates the build number in the Info.plist file each time the application is built.

Just add the script as a ‘Run Script’ build phase for your chosen target in Xcode. The script is only designed for Git, SVN & Git-SVN as they’re all I use at the moment.

#!/bin/bash

#
# Automatically sets the build number for a target in Xcode based
# on either the Subversion revision or Git hash depending on the
# version control system being used
#
# Simply add this script as a 'Run Script' build phase for any
# target in your project
#

git status
if [ "$?" -eq "0" ]
then

    git svn info
    if [ "$?" -eq "0" ]
    then
        buildNumber=$(git svn info | grep Revision | awk '{print $2}')
    else
        buildNumber=$(git rev-parse HEAD | cut -c -10)
    fi
elif [ -d ".svn" ]
then
    buildNumber=$(svn info | grep Revision | awk '{print $2}')
else
    buildNumber=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" Source/Application-Info.plist)
fi

/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" Source/Application-Info.plist

Check back soon.

Automatic Build Numbers In Xcode