Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Update current Xcode target's build number with the number of commits on a specified branch. http://tgoode.com/2014/06/05/sensible-way-increment-bundle-version-cfbundleversion-xcode/
#!/bin/bash
# update_build_number.sh
# Usage: `update_build_number.sh [branch]`
# Run this script after the 'Copy Bundle Resources' build phase
# Ref: http://tgoode.com/2014/06/05/sensible-way-increment-bundle-version-cfbundleversion-xcode/
branch=${1:-'master'}
buildNumber=$(expr $(git rev-list $branch --count) - $(git rev-list HEAD..$branch --count))
echo "Updating build number to $buildNumber using branch '$branch'."
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
if [ -f "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}.dSYM/Contents/Info.plist" ]; then
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}.dSYM/Contents/Info.plist"
fi
@airdrummingfool

This comment has been minimized.

Copy link
Owner Author

@airdrummingfool airdrummingfool commented Jun 4, 2014

To use: add this as a 'Run Script' after the 'Copy Bundle Resources' build script in Xcode. If no argument is provided (to specify which branch to compare against), master is used by default.

@airdrummingfool

This comment has been minimized.

Copy link
Owner Author

@airdrummingfool airdrummingfool commented Mar 29, 2015

Update: now updating the dSYM Info.plist as well. Thanks to this comment by @dismory.

@airdrummingfool

This comment has been minimized.

Copy link
Owner Author

@airdrummingfool airdrummingfool commented Feb 6, 2016

Update: make sure the dSYM Info.plist file exists before trying to update it. Thanks to this comment by Motti Shneor.

@airdrummingfool

This comment has been minimized.

Copy link
Owner Author

@airdrummingfool airdrummingfool commented Feb 21, 2016

If you don't want to anchor the build number to a branch, you can use this to calculate it:
buildNumber=$(git rev-list HEAD --count)

@Coeur

This comment has been minimized.

Copy link

@Coeur Coeur commented Sep 30, 2018

@airdrummingfool, with Xcode 10, ${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}.dSYM seems to be always missing. Good thing there is an if -f in the script, but I wonder if we need to change something for Xcode 10?

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