Build numbers from Bazaar [UPDATED x 3]

Posted on the 30/06/2008 at 04:31 PM

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. bazaarbuildno1
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



Comments (1)




Page 1 of 1 pages



Copyright © 2006-2014 M Cubed Software