Skip to content

Instantly share code, notes, and snippets.

@danielgehr
Last active October 9, 2017 08:58
Show Gist options
  • Save danielgehr/abf34e25909d42b158b77b72776f710b to your computer and use it in GitHub Desktop.
Save danielgehr/abf34e25909d42b158b77b72776f710b to your computer and use it in GitHub Desktop.
Automatically increment and set build number of an Xcode project by counting git commits of the current branch
# This build phase script updates the Info.plist file of your archived Xcode project with the current build number
# The build number is calculated by counting the amount of git commits on the current branch
# Updating the plist file in the archive has the benefits that this won't result in a code change which then would be visible in git as uncommited change
# Just set the build number in Xcode to something like "AUTO_GENERATED" in order to make clear that this value comes from somewhere else
# You need to add the script after the "Copy Bundle Ressource" phase, otherwise, there wont be an archive
# Ideally, just place it at the end of your build phases
# If your app uses a Settings.bundle and the settings should show the app version, you can use the second part of the script to update the settings bundle
# Otherwise, you just need the first part
# *** Update Info.plist file in archive with build number ***
# Calculate build number by counting git commits and set the build number in the Info.plist of the .app file
BUILD_NUMBER=`git rev-list --count HEAD`
INFO_PLIST="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/Info.plist"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${BUILD_NUMBER}" "${INFO_PLIST}"
# ******
# *** Update Root.plist file in Settings.bundle with version and build number ***
# Create version string
VERSION_NUMBER=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" ${SRCROOT}/${INFOPLIST_FILE})
VERSION_AND_BUILD_NUMBER="$VERSION_NUMBER ($BUILD_NUMBER)"
# Iterate through plist file in settings bundle and search for the bundle version key
# If found, set the value to the new version string
DEFAULTS_VERSION_KEY="CFBundleVersion"
SETTINGS_ROOTPLIST="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/Settings.bundle/Root.plist"
for i in {0..50}; do
CURRENT_KEY=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:${i}:Key" "${SETTINGS_ROOTPLIST}"`
if [ "${CURRENT_KEY}" == "${DEFAULTS_VERSION_KEY}" ]; then
/usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:${i}:DefaultValue ${VERSION_AND_BUILD_NUMBER}" "${SETTINGS_ROOTPLIST}"
break
fi
done
# ******
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment