Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 4: Update CFBundleVersion on each build using Git repo commit version

Tags:

git

xcode

I'm using Xcode 4 in combination with Git and would like to increment the CFBundleVersion in Info.plist on each build. The value for the key CFBundleVersion should be updated to the number of the last commit I made to the Git repository.

I found that python script which works well, but unfortunately doesn't update the Info.plist in my Xcode project - it just updates the Info.plist in the "BUILT_PRODUCTS_DIR".

Does anyone has an idea how to get Xcode 4 to fetch the version of the latest commit and put that information into the project's Info.plist?

Thanks!

like image 375
Patrick Avatar asked Jun 11 '11 16:06

Patrick


1 Answers

The version string needs to be of the format [xx].[yy].[zz] where x, y, z are numbers.

I deal with this by using git tag to give specific commits meaningful tag numbers for x and y (eg 0.4), and then via a script build phase, z gets the number of commits since the last tag, as returned by git describe.

Here's the script, which I adapted from this one. It can be added straight to the target as a build phase (shell is /usr/bin/env ruby):

# add git tag + version number to Info.plist
version = `/usr/bin/env git describe`.chomp

puts "raw version "+version
version_fancy_re = /(\d*\.\d*)-?(\d*)-?/
version =~ version_fancy_re
commit_num = $2
if ( $2.empty? )
commit_num = "0"
end
fancy_version = ""+$1+"."+commit_num
puts "compatible: "+fancy_version

# backup
source_plist_path = File.join(ENV['PROJECT_DIR'], ENV['INFOPLIST_FILE'])
orig_plist = File.open( source_plist_path, "r").read;
File.open( source_plist_path+".bak", "w") { |file| file.write(orig_plist) }

# put in CFBundleVersion key
version_re = /([\t ]+<key>CFBundleVersion<\/key>\n[\t ]+<string>).*?(<\/string>)/
orig_plist =~ version_re
bundle_version_string = $1 + fancy_version + $2
orig_plist.gsub!(version_re, bundle_version_string)

# put in CFBundleShortVersionString key
version_re = /([\t ]+<key>CFBundleShortVersionString<\/key>\n[\t ]+<string>).*?(<\/string>)/
orig_plist =~ version_re
bundle_version_string = $1 + fancy_version + $2
orig_plist.gsub!(version_re, bundle_version_string)

# write
File.open(source_plist_path, "w") { |file| file.write(orig_plist) }
puts "Set version string to '#{fancy_version}'"
like image 85
damian Avatar answered Sep 17 '22 23:09

damian