Archive for the ‘xCode’ Category

Tracking a build count with xCode

Tuesday, April 22nd, 2008

This article applies to people working on a solo project who want to track how many times they have gone through the infamous edit, compile, run and test cycle. Once you have written one or two simple applications you may have come to the obvious conclusion that building good software takes time.

Executive summary

Your time is not infinite and it is valuable to know where you are spending it. What I’m going to show you is how to increment a counter by adding a Run Script Phase to your project. This script generates a .xcconfig file which we then use to put the build number directly in the about box.

Some data from my projects

I first started using this technique with Laughing Man but not until five versions had been released.

SunFlower was started after Laughing Man so I started collecting data from the beginning of the project. Below is the data for builds 0.1 through to 0.9.

I find this information very useful because I can correlate it with the difference in lines of code and what changes were made in that version. This can give valuable perspective on whether that “easy new feature” or change was really that easy. It also helps to roughly gauge whether your standard for quality of work has gone up.

You will notice that for SunFlower, there is a major spike for version 0.5 and 0.8. In both cases I know why those versions took so many builds. 0.8, for instance is when I introduced the iTunes style scrollbar which took much work to refine.

The incrementing shell script

The first thing we need is something to increment our build number. The shell script listing below does just that. It requires one parameter, which is the name of the file used to store the current build number. This allows us to use this script for multiple projects at the same time.

#!/bin/bash
#
# buildIncrementer  
#  -- a simple script to increment the 
#      build number through Xcode
#
if [ -e "$1" ]
then
  echo " do nothing" > /dev/null
else
  # creat an empty file
  touch $1
fi
 
#increment the build number
read number  < $1
let number++
echo $number
echo $number > $1

I have this script saved in the bin directory of my home directory as buildIncrementer. Copy the script and place it where you like on your machine.

Adding the run script phase to the project

To use the build incrementer we need to add a run script phase to the project. Find the target you would like to apply this to and right click on it. Choose Add –> New Build Phase –> New Run Script Build Phrase.

Now right click and choose “Get Info” on the newly created build phase. Modify the script text so that it is like the image below.

Now run your application through xCode and it will run your script. This will cause the file version.xcconfig to be created in your project directory. You can look at version.xcconfig in a text editor it will contain “CURRENT_PROJECT_VERSION = 1″.

Adding the build number to the about box

Now that we’ve got a count of how many builds have occurred it would be nice to put that build number in the about box. The first thing we need to do is add the file version.xcconfig to the project.

Once you’ve added the dynamically generated .xcconfig file you need to modify the build settings of the project so that the project is based on that file. Choose “Get Info” on the project and select the Build tab. Set configuration to “All Configurations” and then choose version in the Based On combo box at the bottom of the window.

Lastly, edit the info.plist file so that CFBundleVersion uses ${CURRENT_PROJECT_VERSION} and add a CFBundleShortVersionString.

  <key>CFBundleShortVersionString</key>
  <string>0.1</string>
  <key>CFBundleVersion</key>
  <string>${CURRENT_PROJECT_VERSION}</string>

It is important to note that you must do a clean for the latest project build number to appear in the about box. This is because the info.plist file is only regenerated by xCode if it needs to be.

If you’re too lazy to set this up you can download a self-contained xCode project that has all these steps setup.

Happy Koding,