Skip to content

Instantly share code, notes, and snippets.

@baz
Created December 29, 2013 01:30
Show Gist options
  • Save baz/8166453 to your computer and use it in GitHub Desktop.
Save baz/8166453 to your computer and use it in GitHub Desktop.
Generate header for version information
#!/bin/bash
# This script automatically sets the version and short version string of
# an Xcode project from the Git repository containing the project.
#
# To use this script in Xcode 4, add the contents to a "Run Script" build
# phase for your application target.
set -o errexit
set -o nounset
INFO_PLIST="${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/Contents/Info"
INFO_PLIST_SRC="${SRCROOT}/src/Info.plist"
# Use the latest tag for short version (You'll have to make sure that all your tags are of the format 0.0.0,
# this is to satisfy Apple's rule that short version be three integers separated by dots)
SHORT_VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}/" describe --abbrev=0)
# I'd like to use the Git commit hash for CFBundleVersion.
# VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" rev-parse --short HEAD)
# But Apple wants this value to be a monotonically increasing integer, so
# instead use the number of commits on the master branch. If you like to
# play fast and loose with your Git history, this may cause you problems.
# Thanks to @amrox for pointing out the issue and fix.
VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}/" rev-list master | wc -l)
defaults write "${INFO_PLIST}" CFBundleShortVersionString $SHORT_VERSION
defaults write "${INFO_PLIST}" CFBundleVersion $VERSION
# If we're building a release version write our new version information,
# to the actual file so our dSYM files reflect the same version we're releasing.
if [ "${CONFIGURATION}" = "Release" ]; then
defaults write "${INFO_PLIST_SRC}" CFBundleShortVersionString $SHORT_VERSION
defaults write "${INFO_PLIST_SRC}" CFBundleVersion $VERSION
fi
echo "VERSION: ${VERSION}"
echo "SHORT VERSION: ${SHORT_VERSION}"
BUNDLE_IDENTIFIER=$(defaults read "${INFO_PLIST}" CFBundleIdentifier)
# Output header file
HEADER="${SRCROOT}/config/BuildConfig.h"
cd "${SRCROOT}/config"
echo "// Do not edit" > $HEADER
echo "#define SHORT_VERSION_STRING @\"${SHORT_VERSION}\"" >> $HEADER
echo "#define BUNDLE_IDENTIFIER @\"${BUNDLE_IDENTIFIER}\"" >> $HEADER
echo "// Updated on "`date` >> $HEADER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment