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

前端 未结 3 1553
悲哀的现实
悲哀的现实 2020-12-28 10:40

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 updat

3条回答
  •  不思量自难忘°
    2020-12-28 11:23

    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 ]+CFBundleVersion<\/key>\n[\t ]+).*?(<\/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 ]+CFBundleShortVersionString<\/key>\n[\t ]+).*?(<\/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}'"
    

提交回复
热议问题