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

标签 xcode git

我将 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/

相关文章:

android - 我如何使用 Android App 从/向 GitHub 接收/发送信息

windows - 为什么Windows中安装的git bash在后台运行脚本?

ios - 自动布局约束未正确放置容器 View

swift - 在 Swift 中设置 UITextView 文本

iOS 应用程序 - 精简版或付费版

swift - 在 UITableView 行中保存按钮的选择状态 (Swift)

git - ( git add -A 后跟 git commit )和 git commit -a 之间的区别?

Git 恢复远程分支

git - 使用 git grep 时如何查看提交 sha?

ios - 减小 IOS 中的应用程序大小