public
Last active

Automatically update info.plist with git build number and hash when building in Xcode

  • Download Gist
xcode_git_update_plist.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#!/usr/bin/env ruby
 
# Giants' Shoulders Trodden Upon
# Based on the xcode auto-versioning script for Subversion by Axel Andersson
# Updated for git by Marcus S. Zarra and Matt Long
# Converted to ruby by Abizer Nasir
 
# Improved? By Jonathan Zhan
 
# Appends the number of commits on the current branch and a short git hash to the version number set in Xcode.
# Writes this information to CFBundleVersion in your Apps info.plist
 
# How to use:
# Xcode > Product > Edit Schemes
# Pick a Scheme (I use Archive, so it only gets updatethis when doing new distribution builds)
# Add a run script to pre-actions for your chosen scheme
# Set your shell to "/usr/bin/env ruby" and get build settings from your target of choice
# You're done!
 
######################################################
 
# These are the common places where git is installed.
# Change this if your path isn't here
 
common_git_paths = %w[/usr/bin/git /usr/local/git/bin/git /opt/local/bin/git]
git_path = ""
 
common_git_paths.each do |p|
if File.exist?(p)
git_path = p
break
end
end
 
if git_path == ""
puts "Path to git not found"
exit
end
 
sourceRoot = ENV['PROJECT_DIR']
 
Dir.chdir(sourceRoot){
command_line = git_path + " rev-parse --short HEAD"
sha = `#{command_line}`.chomp #short git hash
ver = `git describe`
revRaw = `git rev-list --all | wc -l`.chomp #total number of commits
rev = "r" + revRaw.gsub(/\s+/,"") # last git tag
info_file = ENV['SRCROOT'].gsub(/ /){"\\ "} + "/" + ENV['INFOPLIST_FILE'].gsub(/ /){"\\ "}
`/usr/libexec/PlistBuddy -c "Set :CFBundleVersion #{rev+'/'+sha}" #{info_file}`
`/usr/libexec/PlistBuddy -c "Set :CFBundleVersion #{rev+'/'+sha}" #{ENV['INFOPLIST_PATH']}`
}

1eeeb6 updates the info.plist in your application binary as well as the info file in your project.

Updated so that it works if your project directory has a space somewhere. Oops.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.