Skip to content

Instantly share code, notes, and snippets.

@MacLemon
Created December 9, 2012 23:22
Show Gist options
  • Save MacLemon/4247471 to your computer and use it in GitHub Desktop.
Save MacLemon/4247471 to your computer and use it in GitHub Desktop.
Autoincreasing Build numbers, get version numbers from git tag, put it into Info.plist
#!/bin/bash
# by Andreas Monitzer (@anlumo1) and Pepi Zawodsky (@MacLemon)
#
# This script published under WTF license
# http://en.wikipedia.org/wiki/WTFPL
# Improvements to this script are welcome though.
# Augments the Info.plist with a lot of nice stuff.
# It's suggested to call this script from a "run script" build phase, not copy the script's contents there.
# All Values are available from Objective-C like this:
# Example
# NSDictionary *infoPList = [[NSBundle mainBundle] infoDictionary];
# NSLog(@"CFBundleShortVersionString: %@", [infoPList objectForKey:@"CFBundleShortVersionString"]);
echo "Checking for git repo in ${PROJECT_DIR}/.git"
# When using git SCM
if [ -e "${PROJECT_DIR}/.git" ]
then
echo Found this to be a git repo.
INFOPLISTPATH="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
echo Info.plist PATH = $INFOPLISTPATH
# Get a svn-like revision number that keeps increasing with every commit.
REV=$(git log --pretty=format:'' | wc -l | sed 's/[ \t]//g')
echo "git rev: $REV"
# Getting the current branch
GITBRANCH=$(git branch | grep "*" | sed -e 's/^* //')
echo "Git Branch: $GITBRANCH"
# To prevent local builds from displaying incorrent branches we need to delete and readd.
/usr/libexec/PlistBuddy -c "Delete :GitBranch string" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Add :GitBranch string $GITBRANCH" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
# full build hash
GITHASH=$(git rev-parse HEAD)
echo "Git Hash: $GITHASH"
/usr/libexec/PlistBuddy -c "Delete :GitHash string" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Add :GitHash string $GITHASH" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
# commonly used short hash
GITHASHSHORT=$(git rev-parse --short HEAD)
echo "Git Hash Short: $GITHASHSHORT"
/usr/libexec/PlistBuddy -c "Delete :GitShortHash string" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Add :GitShortHash string $GITHASHSHORT" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
# parsing tags to build the CFBundleVersion in the form of <MAJOR>.<MINOR>.<PATCH>.<REV>
# Parts of the version number that are missing are substituted with zeros.
NEAREST=$(git describe --abbrev=0 --match "release_[0-9]*")
echo "Nearest release Tag: \"$NEAREST\""
MAJOR="0"
MINOR="0"
PATCH="0"
if [ "$NEAREST" == "" ]
then
echo "No release tag found!"
else
MAJOR=$(echo $NEAREST | cut -d "_" -f 2)
if [ $MAJOR == "" ]
then
MAJOR="0"
else
MINOR=$(echo $NEAREST | cut -d "_" -f 3)
if [ $MINOR == "" ]
then
MINOR="0"
else
PATCH=$(echo $NEAREST | cut -d "_" -f 4)
if [ $PATCH == "" ]
then
PATCH="0"
fi
fi
fi
fi
echo "Version String: $MAJOR.$MINOR.$PATCH.$REV"
# Setting in the Info.plist file
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $MAJOR.$MINOR.$PATCH.$REV" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $MAJOR.$MINOR.$PATCH.$REV ($GITHASHSHORT)" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
# Setting the same version number in the .dSYM file needed for symbolication in the Hockeyapp.net webinterface
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $MAJOR.$MINOR.$PATCH.$REV" "${DWARF_DSYM_FOLDER_PATH}/${DWARF_DSYM_FILE_NAME}/Contents/Info.plist"
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $MAJOR.$MINOR.$PATCH.$REV ($GITHASHSHORT)" "${DWARF_DSYM_FOLDER_PATH}/${DWARF_DSYM_FILE_NAME}/Contents/Info.plist"
elif [ -e "${PROJECT_DIR}/../.svn" ]
then
echo Found this to be an svn repo.
# Support for SVN would be happy if you'd actually implement it!
REV=$(svnversion -nc "${PROJECT_DIR}" | sed -e 's/^[^:]*://;s/[A-Za-z]//')
echo -n "SVN rev: $REV"
fi
if [ "$BUILD_NUMBER" == "" ]
then
/usr/libexec/PlistBuddy -c "Delete :JenkinsBuild string" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Add :JenkinsBuild string $BUILD_NUMBER" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
else
/usr/libexec/PlistBuddy -c "Delete :JenkinsBuild string" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Add :JenkinsBuild string Local build (not via Jenkins)" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
fi
#EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment