About
This is the M Cubed Software weblog. To find out more about us head to our about page.
Search
Feed
Archives
- June 2010
- April 2010
- March 2010
- February 2010
- January 2010
- November 2009
- August 2009
- July 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
Build numbers from Bazaar [UPDATED x 3]
Posted on 30/06/2008 at 04:31 PM in
Things have been pretty quiet on the public front of M Cubed for the past month or two, but rest assured I’m working on some pretty cool stuff that I’ll hopefully get to show off soon. One thing that has changed recently is that I’ve started to use Bazaar as my version control system of choice.
Now I’ve had a pretty tarnished history with version control software, often resorting to use zip archives because they were less hassle. I’ve fallen in love with Bazaar though and have managed to integrate it pretty well into my workflow.
One thing I’ve started to find a use for recently though has been build numbers for releases. There are many ways people use to create build numbers, the one I’ve settled on is using the latest revision number of my Bazaar repository. This is both a very simple way of working out build numbers, but also tells me where in my repository this version came from.
Searching t’interweb I found several useful pages detailing how to automate build numbers in Xcode and integrate them with version control systems. I spent a 15 minutes learning the basics of Perl (which is surprisingly easy to learn) and another 30 minutes playing around and I finally had a build script that takes the latest revision number from a Bazaar repository and puts it in the Info.plist of the current project.
So how do you add this to your Xcode project? Simple:
1. Right-click on your build target and choose Add->New Build Phase->New Run Script Build Phase.
2. Set the Shell to /usr/bin/perl
3. Copy the following script to the script field:
# Created by Martin Pilkington on 29/06/2008.
# Copyright 2008 M Cubed Software. All rights reserved.
#Check if we're versioned under bzr
if (-d ".bzr") {
#get the revno minus the new line
my $revno = `/usr/local/bin/bzr revno`;
substr($revno, -1, 1, "");
#open the info plist
open(my $input, "<", "Info.plist") or die "Can't open Info.plist: $!";
my $outputstring = "";
#loop through each line until we find CFBundleVersion, then substitute the next line
while (<$input>) {
$outputstring .= "$_";
if (/[t ]+<key>CFBundleVersion</key>\n/) {
my $line = <$input>;
$line =~ s/([t ]+<string>)(.*?)(</string>)/$1.($revno).$3/eg;
$outputstring .= $line;
}
}
#write back out to Info.plist
open(my $output, ">", "Info.plist") or die "Can't open Info.plist: $!";
print $output $outputstring;
} else {
print "This project is not versionedn";
}
4. Set your CFBundleVersion setting to 1. 
5. Move the Run Script phase to the top of your target.
There you have it, automated build numbers straight from your Bazaar repository. If you have any improvements or find any bugs then please leave a comment below, this is my first foray into Perl so some things may be wrong but it works for me.
Update: Jeremy Knope has provided a ruby version of the above script which can be found here: http://www.codecollector.net/view/1736/Bazaar_revno_build_phase_script . I’ve also updated my Perl script to check if the project is versioned with bzr. I’ve also fixed the comments which didn’t seem to be working before.
Update 2: Fixed some more bugs
Update 3: Fixed a bug with the regex
(1) Comments
Comments
Excellent stuff, thank you. However, as someone who is still in the early stages of stumbling around with XCode I was wondering why this only works when you do a release build rather than a debug build. Is there a way to have the bundle version update for both build types?