Skip to content

Instantly share code, notes, and snippets.

@drodriguez
Created April 2, 2009 09:19
Show Gist options
  • Save drodriguez/89109 to your computer and use it in GitHub Desktop.
Save drodriguez/89109 to your computer and use it in GitHub Desktop.
This script and build target will take the revision number from a Subversion repository (or a Git
repository using git-svn) and substitute the last dotted component of the CFBundleVersion in your
Info.plist file (you can put something like "1.0.0.xx" for your first run).
Instructions:
- Save update_build_number.rb into ${PROJECT_DIR}/Scripts (or wherever you want, but remember to
change the paths accordigly in the build phase).
- Add a new target to your project (I named mines as "Update FooBar Build Number"). In new target
dialog choose from "Other" category "Shell Script Target".
- In the "Run Script" build phase include the contents shown above.
- Make your main target depend on "Update FooBar Build Number".
# Script to paste in the Run Script phase
update_build_number() {
# This is where my git binaries are
PATH=$PATH:/opt/local/bin
/usr/bin/env ruby ${PROJECT_DIR}/Scripts/update_build_number.rb ${PROJECT_DIR} ${PROJECT_DIR}/Path/To/Your/Info.plist
}
case "$CONFIGURATION" in
Beta|Distribution)
update_build_number
;;
*)
esac
require 'osx/cocoa'
class InvalidRepository < Exception; end
class GitInfo
def initialize(path)
unless File.exist?(File.join(path, '.git'))
raise InvalidRepository.new(path)
end
@path = File.join(path, '.git')
end
def build_number
result = `git --git-dir=#{@path} svn info`
if $?.success?
result = result.split("\n").inject({}) do |memo, item|
pair = item.split(':', 2)
memo[pair.first] = pair.last.strip
memo
end
result = result["Revision"]
return nil if result.nil?
begin
Integer(result)
rescue ArgumentError => e
result = result[0..-2]
if result.length > 0
retry
else
nil
end
end
else
nil
end
end
end
class SVNInfo
def initialize(path)
unless File.exist?(File.join(path, '.svn'))
raise InvalidRepository.new(path)
end
@path = path
end
def build_number
result = `svnversion #{@path}`
if $?.success?
result = result.split(':').last
begin
Integer(result)
rescue ArgumentError => e
result = result[0..-2]
if result.length > 0
retry
else
nil
end
end
else
nil
end
end
end
def main
begin
info = SVNInfo.new(ARGV[0])
rescue InvalidRepository => e
begin
info = GitInfo.new(ARGV[0])
rescue InvalidRepository => e
return
end
end
build_number = info.build_number
plist = OSX::NSMutableDictionary.dictionaryWithContentsOfFile_(ARGV[1])
main_version = plist['CFBundleVersion'].split('.')[0..-2]
plist['CFBundleVersion'] = (main_version << build_number).join('.')
plist.writeToFile_atomically_(ARGV[1], true)
end
if __FILE__ == $0
main
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment