我将 Xcode 4 与 Git 结合使用,并希望在每次构建时增加 Info.plist 中的 CFBundleVersion。键 CFBundleVersion 的值应更新为我对 Git 存储库所做的最后一次提交的编号。
我找到了 that运行良好的 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
返回。
这是我改编自 this one 的脚本.它可以作为构建阶段直接添加到目标中(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}'"
关于Xcode 4 : Update CFBundleVersion on each build using Git repo commit version,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6317181/