Xcode 4:使用Git repo提交版本在每个构建上更新CFBundleVersion



我将Xcode 4与Git结合使用,并希望在每次构建时增加Info.plist中的CFBundleVersion。密钥CFBundleVersion的值应该更新为我最后一次提交到Git存储库的次数。

我发现python脚本运行良好,但不幸的是,它没有更新Xcode项目中的Info.plist——它只是更新了"BUILT_PRODUCTS_DIR"中的Info_plist。

有人知道如何让Xcode 4获取最新提交的版本,并将这些信息放入项目的Info.plist中吗?

谢谢!

版本字符串的格式需要为[xx]。[yy]。[zz]其中x,y,z是数字。

我通过使用git tag为x和y提供有意义的特定提交标记号(例如0.4)来处理此问题,然后通过脚本构建阶段,z获得自git describe返回的最后一个标记以来的提交数。

这是我根据这个改编的剧本。它可以直接添加到目标作为构建阶段(shell/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}'"

这非常适合我

#!/usr/bin/ruby
require 'rubygems'
    begin
        require 'Plist'
        rescue LoadError => e
        puts "You need to install the 'Plist' gem: [sudo] gem install plist"
    exit 1
end
raise "Must be run from Xcode" unless ENV['XCODE_VERSION_ACTUAL']
GIT = "/usr/bin/env git"
PRODUCT_PLIST = File.join(ENV['BUILT_PRODUCTS_DIR'], ENV['INFOPLIST_PATH'])
HASH = `#{GIT} log -1 --pretty=format:%h`
BUNDLE_VERSION = "CFBundleVersion"
if File.file?(PRODUCT_PLIST) and HASH
    # update product plist
    `/usr/bin/plutil -convert xml1 "#{PRODUCT_PLIST}"`
    info = Plist::parse_xml(PRODUCT_PLIST)
    if info
        info[BUNDLE_VERSION] = HASH
        info["GCGitCommitHash"] = HASH
        info.save_plist(PRODUCT_PLIST)
    end
    `/usr/bin/plutil -convert binary1 "#{PRODUCT_PLIST}"`
    # log
    puts "updated #{BUNDLE_VERSION} to #{HASH}"
    puts "HEAD: #{HASH}"
end

@damian感谢您的脚本,它运行良好。

但在那之后,我遇到了以下问题。每次提交后,当我构建项目时,我都会在git中进行更改。我有忽略plist文件的解决方案,但我不希望这样。

现在,我将您的脚本添加到git中的预提交挂钩中,而不是xcode构建阶段。唯一的问题是我的脚本无法获得PROJECT_DIR和INFOPLIST_FILE,所以我不得不用scipt硬编码它们。我找不到如何从xcode项目中获取env变量。

它运行良好:)

最新更新