Automatic Build Number in XCode with Python

I've been using a sed script lately, used a Perl script to embed the svn number into the Get Info while at UGOBE, but never used Python even though it's the language I am much more comfortable with.

While searching for versioning best practices I found this blog by Shiny Frog.  It uses a Python script to get the current svn version and use it for the build number.  Even though I don't like using the svn number for a build number (see Dave Durbin's excellent article for details) it motivated me to switch to Python.  

BTW, here's my preferred versioning scheme:

CFBundleShortVersionString - is the marketing version of the application (e.g., 1.0.0, 1.0.1, 1.1.0, 2.0.0, etc.)

CFBundleVersion - is the build number for the marketing version of the application.  This number increases sequentially for each build of the project.

Here's the Python script that does the work:

#!/usr/bin/python

from AppKit import NSMutableDictionary

import os

# reading info.plist file

projectPlist = NSMutableDictionary.dictionaryWithContentsOfFile_('Info.plist')

# setting the svn version for CFBundleVersion key

version = projectPlist['CFBundleVersion']

incrementVersion = long(version) + 1

projectPlist['CFBundleVersion'] = incrementVersion

projectPlist.writeToFile_atomically_('Info.plist', True)


Shiny Frog's Note on How to use the script in Xcode:

  1. Add a new build phase at the end of your target’s build phases: right click on your Target , Add -> New build Phase -> New Run Script Build Phase.
  2. Double click on the new "Run Script" item and copy/paste the script in the Script field.
  3. Make sure that the Shell field is set to: /usr/bin/python.

When you build your application, the Svn version number will be applied for the CFBundleVersion key.


Copyright © 2008-2012 Irongeek Software, LLC.  support@irongeeksoftware.com