Xcode 4:使用Git存储库提交版本在每个版本上更新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变量。 它很好用:)     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...