将 Xcode 包版本设置为 git hash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8192921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:28:49  来源:igfitidea点击:

Set Xcode bundle version to git hash

xcodegit

提问by forthrin

In Xcode, I want to set the project bundle version to the git hash, to create a unique link between a revision and a build.

在 Xcode 中,我想将项目包版本设置为 git 哈希,以在修订和构建之间创建唯一链接。

(I have seen several postings about this that increase the build number for each build, but to me this seems excessive, and I would like to have a build number that testers can refer to when sender feedback).

(我已经看到一些关于此的帖子,这些帖子增加了每个构建的构建号,但对我来说这似乎过多,我希望有一个构建号,测试人员可以在发件人反馈时参考)。

I added the following as a Run Script in Build Phases:

我在构建阶段添加了以下作为运行脚本:

/usr/libexec/PlistBuddy -c "Set :CFBundleVersion `git rev-parse --short HEAD`" *.plist

And then I read out the bundle version in the code with:

然后我读出了代码中的捆绑版本:

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

However, then the new revision number does not appear in the code until the nextbuild. I assume this is because the plist file is already read by the compiler when my script is run.

但是,直到下一次构建时,新的修订号才会出现在代码中。我认为这是因为在我的脚本运行时编译器已经读取了 plist 文件。

Also, since the plist is changed with the revision string aftercommitting to git, this causes an annoying diff in the project, so maybe I am not so smart after all.

另外,由于提交到 gitplist 随修订字符串更改,这会导致项目中出现烦人的差异,所以也许我毕竟不是那么聪明。

An alternative would be to write the revision number to a text file (build.txt) which is ignored by git. But I would still like the project build number to follow git as well.

另一种方法是将修订号写入一个被 git 忽略的文本文件 (build.txt)。但我仍然希望项目构建号也遵循 git。

Anyone know a brilliant way to do this?

有人知道这样做的绝妙方法吗?

采纳答案by user1532390

https://gist.github.com/966838is a short bash script that meets Apple's requirement that CFBundleVersion should be a monotonically increased string.

https://gist.github.com/966838是一个简短的 bash 脚本,它满足 Apple 的要求,即CFBundleVersion 应该是单调递增的 string

回答by user1532390

You will need to install PlistBuddyto use this method.

您需要安装PlistBuddy才能使用此方法。

The method that I use is to set the script as the last build phase, and make the changes on the target build directory. In other words:

我使用的方法是将脚本设置为最后一个构建阶段,并在目标构建目录上进行更改。换句话说:

BUILD_NUMBER=`git rev-parse --short HEAD`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_NUMBER" "${TARGET_BUILD_DIR}"/"${INFOPLIST_PATH}"

So in the repo the value should be a dummy value like 0, and it will get overwritten everytime. This does mean that the current build number will only be available after you build.

所以在 repo 中,该值应该是一个像 0 这样的虚拟值,并且每次都会被覆盖。这确实意味着当前版本号仅在您构建后可用。

回答by Abizern

There have been quite a few ways to do this.

有很多方法可以做到这一点。

I used to run a post build script to inject the sha into the bundle:

我曾经运行一个后期构建脚本来将 sha 注入到包中:

https://gist.github.com/208825

https://gist.github.com/208825

But a more modern method is on the Cocoa is my Girlfriend blog http://www.cimgf.com/2011/02/20/revisiting-git-tags-and-building/

但更现代的方法是可可是我的女朋友博客http://www.cimgf.com/2011/02/20/revisiting-git-tags-and-building/

回答by a2n

I wrote a much shorter scriptfor this. Bash can accomplish the task; no need for other interpreters.

我为此编写了一个更短的脚本。Bash 可以完成任务;不需要其他口译员。