Skip to content

Instantly share code, notes, and snippets.

@thirtified
Last active December 11, 2015 02:18
Show Gist options
  • Save thirtified/4529141 to your computer and use it in GitHub Desktop.
Save thirtified/4529141 to your computer and use it in GitHub Desktop.
Automatically set version and short version string of an Xcode project using parameters of the Git repository containing your project.
# This script automatically sets the version and short version string of
# an Xcode project from parameters of the Git repository containing your project.
#
# - Uses version from latest git tag for CFBundleShortVersionString
# (e.g. 1.2.3 from "v1.2.3")
# - Uses combined string <commit number>.<short commit hash> for CFBundleVersion
# (e.g. 18.9d75e30)
# - Use combined string <commit number> for CFBundleVersion (e.g. 189) for "Release" config
# (Apple requires this value to be a monotonically increasing integer)
#
# To use this script in Xcode, add the contents to a "Run Script" build
# phase for your application target.
#
# https://gist.github.com/4529141
# Based on https://gist.github.com/966838
#
# @author Julian Dreissig (julian@thirtified.com)
set -o errexit
set -o nounset
# Use the latest version tag for CFBundleShortVersionString.
# (Releases have to be tagged in Git using the format v0.0.0.)
RELEASE_VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" describe --abbrev=0 | tail -n 1 | sed -e 's/^v//')
if [ -z "$RELEASE_VERSION"]
then
RELEASE_VERSION="0.0.0"
echo "*** WARN: no version tag found in repository, using '0.0.0' as version info. A a tag containing your release version number (e.g. 'v1.2.3') to your release commit. ***"
fi
# Retrieve Git commit hash as version info.
COMMIT_HASH=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" rev-parse --short HEAD)
# Use the number of commits + "." + the commit hash unless we are building the
# "Release" configuration, in which case we use the commit number only since Apple wants the CFBundleVersion
# value to be a monotonically increasing integer.
NUMBER_OF_COMMITS=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" rev-list HEAD | wc -l | sed 's/ //g')
if [ "$CONFIGURATION" = "Release" ]
then BUILD_VERSION="${NUMBER_OF_COMMITS}" # must be numeric for AppStore releases
else BUILD_VERSION="${NUMBER_OF_COMMITS}.${COMMIT_HASH}"
fi
echo "CFBundleShortVersionString (version number): '$RELEASE_VERSION'"
echo "CFBundleVersion (build number): '$BUILD_VERSION'"
# Actually write the version info into the build:
INFO_PLIST="${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/Info"
defaults write "$INFO_PLIST" CFBundleShortVersionString "$RELEASE_VERSION"
defaults write "$INFO_PLIST" CFBundleVersion "$BUILD_VERSION"
echo "Wrote Version info to $INFO_PLIST"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment