Skip to content

Instantly share code, notes, and snippets.

@dsibilly
Created June 8, 2011 19:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dsibilly/1015179 to your computer and use it in GitHub Desktop.
Save dsibilly/1015179 to your computer and use it in GitHub Desktop.
Unique Build Numbers for XCode 4
# XCode 4 auto-versioning script for Git
# Inspired by the work of Axel Andersson, Marcus S. Zarra and Matt Long
# http://valthonis.net/u/19
"""
NOTE: Due to its use of build environment variables, this
script will only work from inside XCode's build process!
"""
import os
import csv
from subprocess import Popen, PIPE
from Foundation import NSMutableDictionary
cmd = "/usr/local/bin/git rev-parse --short HEAD" # get the short commit hash from git
build_number = Popen(cmd, shell=True, stdout=PIPE).stdout.read()
info_plist = os.environ['BUILT_PRODUCTS_DIR'] + "/" + os.environ['WRAPPER_NAME'] + "/Info.plist"
# Open the plist and write the short commit hash as the bundle version
plist = NSMutableDictionary.dictionaryWithContentsOfFile_(info_plist)
core_version = csv.reader([plist['CFBundleVersion'].rstrip()], delimiter=" ").next()[0]
full_version = ''.join([core_version, ' build ', build_number])
plist['CFBundleVersion'] = full_version
plist.writeToFile_atomically_(info_plist, 1)
@mave99a
Copy link

mave99a commented Nov 19, 2011

A simpler solution use shell script (not necessary need python and the library)

!/bin/bash

buildNumber=git rev-parse --short HEAD
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" PROJECT/PROJECT-Info.plist

@jmoody
Copy link

jmoody commented Dec 7, 2011

This script is the same as mave99a's above, but formatted for clarity.

#!/bin/sh
buildNumber=`/opt/local/bin/git rev-parse --short HEAD`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$PROJECT_NAME"/"$PROJECT_NAME"-Info.plist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment