Skip to content

Instantly share code, notes, and snippets.

@aschober
Created March 27, 2013 19:36

Revisions

  1. aschober created this gist Mar 27, 2013.
    38 changes: 38 additions & 0 deletions gitversioning
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    #!/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}/Info"

    # Get git tag and hash in the FulLVersion
    FULL_VERSION=$(git --work-tree="${PROJECT_DIR}/" describe --dirty | sed -e 's/^v//' -e 's/g//')

    # Use the latest tag for short version (You'll have to make sure that all your tags are of the format 0.0,
    # this is to satisfy Apple's rule that short version be three integers separated by dots)
    SHORT_VERSION=$(git --work-tree="${PROJECT_DIR}/" describe | awk '{split($0,a,"-"); print a[1]}')
    VERSION=$(git --work-tree="${PROJECT_DIR}/" describe | awk '{split($0,a,"-"); print a[1] "." a[2]}')

    # Check to see if the tag starts with with "ver", and remove if so.
    if [[ "${VERSION}" == ver* ]]; then
    SHORT_VERSION="${SHORT_VERSION#*ver}"
    VERSION="${VERSION#*ver}"

    defaults write $INFO_PLIST CFBundleShortVersionString $SHORT_VERSION
    defaults write $INFO_PLIST CFBundleVersion $VERSION
    defaults write $INFO_PLIST FullVersion $FULL_VERSION

    echo "CFBundleVersion: ${VERSION}"
    echo "CFBundleShortVersionString: ${SHORT_VERSION}"
    echo "FullVersion: ${FULL_VERSION}"
    else
    echo "ERROR: DID NOT FIND 'ver*' in Git Tag! Make sure tag exists! e.g. ver0.1"
    exit 2
    fi