Skip to content

Instantly share code, notes, and snippets.

@idevsoftware
Created July 13, 2016 02:46
Show Gist options
  • Save idevsoftware/33244b9e783a3577802400d126b75741 to your computer and use it in GitHub Desktop.
Save idevsoftware/33244b9e783a3577802400d126b75741 to your computer and use it in GitHub Desktop.
Run Script Build Phase for Xcode to automatically increase CFBundleVersion
#!/bin/bash
# Run Script Build Phase for Xcode to automatically increase the CFBundleVersion value
# on your target's Info.plist using a 'yymmddHHMMSS' style timestamp.
# Also, sets a custom key with the current git commit hash (GIT_COMMIT_HASH)
#
# This script works by modifying the target copy of your Info.plist file, so that it won't
# change your original Info.plist which is under version control. It does, however, use
# the original file to read the initial CFBundleVersion value to which the timestamp
# for the current date/time is appended.
#
# Inspired by the work of Tommy Goode (@airdrummingfool)
# http://tgoode.com/2014/06/05/sensible-way-increment-bundle-version-cfbundleversion-xcode/
# Usage:
#
# Under your project properties choose the adequate target, select Build Phases and click
# the '+' button, then 'New Run Script Phase'. Paste the contents below and make sure
# it's set AFTER the 'Copy Bundle Resources' phase (you can reorder by dragging).
PLISTBUDDY="/usr/libexec/PlistBuddy"
GIT="$(which git)"
PROJECT_INFO_PLIST="${PROJECT_DIR}/${INFOPLIST_FILE}"
TARGET_INFO_PLIST="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
if [ ! -x ${PLISTBUDDY} -o \
-z "${GIT}" -o \
! -x "${GIT}" -o \
! -f ${PROJECT_INFO_PLIST} \
! -f ${TARGET_INFO_PLIST} ]
then
exit
fi
DSYM_INFO_PLIST="${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}.dSYM/Contents/Info.plist"
TIMESTAMP="$(date -u +%y%m%d%H%M%S)"
COMMIT="$(${GIT} rev-parse --short HEAD)"
BUNDLE_VERSION=$(${PLISTBUDDY} -c "Print CFBundleVersion" "${PROJECT_INFO_PLIST}")
TARGET_BUNDLE_VERSION="${BUNDLE_VERSION}.${TIMESTAMP}"
echo "Updating CFBundleVersion and setting GIT_COMMIT_HASH using ${TARGET_INFO_PLIST}"
${PLISTBUDDY} -c "Add :GIT_COMMIT_HASH string ${COMMIT}" "${TARGET_INFO_PLIST}"
${PLISTBUDDY} -c "Set :CFBundleVersion ${TARGET_BUNDLE_VERSION}" "${TARGET_INFO_PLIST}"
if [ -f ${DSYM_INFO_PLIST} ]
then
echo "Updating CFBundleVersion and setting GIT_COMMIT_HASH using ${DSYM_INFO_PLIST}"
${PLISTBUDDY} -c "Add :GIT_COMMIT_HASH string ${COMMIT}" "${DSYM_INFO_PLIST}"
${PLISTBUDDY} -c "Set :CFBundleVersion ${TARGET_BUNDLE_VERSION}" "${DSYM_INFO_PLIST}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment