Skip to content

Instantly share code, notes, and snippets.

@mbinna
Last active November 12, 2021 20:59
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbinna/4068616 to your computer and use it in GitHub Desktop.
Save mbinna/4068616 to your computer and use it in GitHub Desktop.
Include Version Info from Git into Info.plist at build time
Add the script include-version-info.sh into a new run script build phase of your application target. The build phase
should be located after the build phase "Copy Bundle Resources".
# Use Xcode's copy of the Git binary
GIT=`xcrun -find git`
# Use the commit count as CFBundleVersion
GIT_COMMIT_COUNT=`${GIT} rev-list --count HEAD`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${GIT_COMMIT_COUNT}" "${BUILT_PRODUCTS_DIR}/${INFOPLIST_PATH}"
## Use the last annotated tag as CFBundleShortVersionString
GIT_TAG=`${GIT} describe --tags --always --dirty`
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString ${GIT_TAG}" "${BUILT_PRODUCTS_DIR}/${INFOPLIST_PATH}"
@totalgee
Copy link

Thanks for this. I'm doing something similar to the above to get git info into the application info plist, but it doesn't always work for me.

In the Build Messages, after Run custom shell script, I see Process /path/to/Info.plist being run, where it calls ProcessInfoPlistFile (this is just prior to Signing the application, after which Touch and Register are the last two build messages in the log). And in the end, the Info.plist (in the "product" application bundle) does not include my changes...I think it must get overwritten by that plist "processing" step.

This seems to depend from project to project -- I have had it work -- or there is something else I'm missing to ensure proper ordering. But I do indeed have my Run Script build phase set last in the list of "Build Phases", after Copy Bundle Resources.

Have you run into something similar?

@totalgee
Copy link

I even tried something like this, adding a delay and backgrounding the plist setting:

(sleep 5 && /usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${MY_CUSTOM_STR}" "${BUILT_PRODUCTS_DIR}/${INFOPLIST_PATH}") &

Even then, Xcode seems to wait for all child processes of the custom script to exit before continuing. However, the strange thing is this does work properly for me after a clean build (the plist file is properly modified because the custom script completes after the Process /path/to/Info.plist command has run. But when trivially rebuilding (e.g. hitting Cmd-B without modifying any source files, or even just Cmd-R to run), the bundle plist file gets restored/overwritten, back to its unmodified state.

@totalgee
Copy link

totalgee commented Oct 16, 2018

One last update with a solution... the problems happened after I updated to Xcode 10 (with Xcode 9 it worked for me). It turns out that to get it to work reliably (with Xcode 10) I needed to add the file "${BUILT_PRODUCTS_DIR}/${INFOPLIST_PATH}" as an input dependency for my custom script. Then the build system rearranges things to ensure it processes Info.plist before calling my custom script that modifies it (no further hacks like sleep 5 are needed ;-).

See the following comment in the Xcode 10 Release notes:

In the new build system, shell scripts can't rely on the state of build artifacts not listed in other build phases (for example, the Info.plist file or .dSYM files.) Add files the script build phase depends on as explicit input dependencies to the shell script build phase. (40852184)

@kisielk
Copy link

kisielk commented Dec 26, 2018

@totalgee Adding the plist to the dependencies also fixed it for me, thanks for the advice.

@mlynch
Copy link

mlynch commented Aug 2, 2019

Unfortunately it seems like my Info.plist gets overwritten every time the app is finally run in XCode 11 beta. I tried variations of BUILT_PRODUCTS_DIR and TARGET_BUILD_DIR (and adding them as input files) and I can get the plist value to change on build, but as soon as the app is run it is cleared. I've got the run script as the last task as well.

Will be following this if anyone figures out how to make this work in 11 beta (w new build system).

@billburgess
Copy link

I had to remove this script temporarily as Xcode 11 GM Seed 2 (and everything before that) was clearing out the CFBundleVersion. I wasn't able to run my build on Jenkins as a result. It kept complaining about a missing bundle version even though it has a default value set that gets updated on the fly from this script. If anyone finds a workaround, I'd love to put this back.

@simonmcl
Copy link

Anyone find a fix for this? using xcrun agvtool next-version -all was working for me in a build phase run script, but then when I added a Xcode server Bot everything just breaks for some reason.

@alexcohn
Copy link

@billburgess I guess that the updates for the default value on the fly actually interferes with the (strange) logic of Xcode. I, too, wanted to have the version params to be saved in a text file (I tried xcconfig), but this didn't work reliably.

In the end, this script works for me when I put in Info.plist placeholders, like

<key>CFBundleShortVersionString</key>
<string>--</string>
<key>CFBundleVersion</key>
<string>--</string>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment