Skip to content

Instantly share code, notes, and snippets.

@bgreenlee
Created January 22, 2011 05:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bgreenlee/790905 to your computer and use it in GitHub Desktop.
Save bgreenlee/790905 to your computer and use it in GitHub Desktop.
Xcode Auto-Versioning: Updates your Info.plist's CFBundleVersion with the current git tag and/or sha. #xcode #git
# Xcode Auto-Versioning
#
# Updates your Info.plist's CFBundleVersion with the current git tag and/or sha.
#
# based on https://github.com/elliottcable/xcode-git-versioner
#
# Usage:
# 1. Right-click the target you want to add the versioning phase to (usually the target that builds your app)
# 2. Select: Add -> New Build Phase -> New Run Script Build Phase
# 3. Specify /usr/bin/env ruby as the shell for the script
# 4. Paste the script body into the Script text area
# 5. Ensure that the build phase is at the end of the target's list of build phases
plist_path = File.join(ENV['BUILT_PRODUCTS_DIR'], ENV['INFOPLIST_PATH'])
raise "Info.plist missing, or path error" unless File.file?(plist_path)
# get our version info from git
git = `which git`.chomp
tag, age, sha = `#{git} describe --tags --always --long`.chomp.match(/(?:(.*)-(\d+)-)?([0-9a-g]+)/i)[1..3]
branch = `#{git} name-rev HEAD --name-only --always`.chomp
# "v1.0", "v1.0b2 (g3f22c9f)", "(g3f22c9f)"
version = tag ? (age.to_i.zero? ? "#{tag}" : "#{tag}b#{age} (#{sha})") : "(#{sha})"
version += " (on #{branch})" if branch and branch != 'master'
# do the insert the version into the plist and write it out
plist = File.read(plist_path)
plist.sub!(/(<key>CFBundleVersion<\/key>.*?<string>).*?<\/string>/m, "\\1#{version}</string>")
File.open(plist_path, 'w') {|f| f.write(plist)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment