Skip to content

Instantly share code, notes, and snippets.

@MSch
Forked from bgreenlee/xcode_auto_versioning.rb
Created May 24, 2011 19:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MSch/989548 to your computer and use it in GitHub Desktop.
Save MSch/989548 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 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.0.20"
version = tag ? (age.to_i.zero? ? "#{tag}" : "#{tag}.#{age}")
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)}
@StevenAppJob
Copy link

Hi I tried the script but gives error on build: Build/Revision.rb:22: syntax error, unexpected tIDENTIFIER, expecting ':'
version += " (on #{branch})" if bran...
^~~~~~~
Command PhaseScriptExecution failed with a nonzero exit code

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