Skip to content

Instantly share code, notes, and snippets.

@alexclst
Last active August 3, 2017 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexclst/675e02c457b02b8a88868809a84ac8b7 to your computer and use it in GitHub Desktop.
Save alexclst/675e02c457b02b8a88868809a84ac8b7 to your computer and use it in GitHub Desktop.
Git Commit Data to InfoPlist iOS (code discussed in this Tenseg blog post: http://www.tenseg.net/blog/2017/08/03/git-commit-info-in-your-app/)
#!/bin/bash
# Add this as a new Run Script build phase for your target.
# Created by Alexander Celeste on 8/1/17.
# Based on http://kelan.io/2008/xcode-run-script-build-phase-tip/
script_file="git-commit-num-to-build-num.sh"
echo "Running a custom build phase script: $script_file"
sh ${PROJECT_DIR}/scripts/${script_file}
scriptExitStatus=$?
echo "DONE with script: ${script_file} (exitStatus=${scriptExitStatus})"
exit "${scriptExitStatus}"
#!/bin/bash
# git-commit-num-to-build-num.sh
# Created by Alexander Celeste on 8/1/17.
# Instructions:
# Run this script after the 'Copy Bundle Resources' build phase
# It will compute git commit information and write it to the target's built Info.plist
# Based on http://tgoode.com/2014/06/05/sensible-way-increment-bundle-version-cfbundleversion-xcode/#code
# declare # uncomment for a list of all env variables
# get data
BRANCH=$(cat $PROJECT_DIR/.git/HEAD | rev | cut -d "/" -f 1 | rev)
echo "You are on the $BRANCH branch."
BUILDNUM=$(expr $(git rev-list $BRANCH --count) - $(git rev-list HEAD..$BRANCH --count))
echo "This is build $BUILDNUM."
COMMIT=$(git rev-list $BRANCH | head -1 | cut -c1-7)
DIFF=$(git diff | wc -l)
echo "This is commit $COMMIT with $DIFF diffs compared to HEAD."
# write data to the products, both the app itself and the dSYM bundle
# build number, this key should already exist as it is standard in Apple-ecosystem apps
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILDNUM" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILDNUM" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}.dSYM/Contents/Info.plist"
# commit information, we always delete the key first to ensure we add the new information each time
/usr/libexec/PlistBuddy -c "Delete :TSCommit" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Add :TSCommit string \"$BRANCH $COMMIT $DIFF\"" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Delete :TSCommit" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}.dSYM/Contents/Info.plist"
/usr/libexec/PlistBuddy -c "Add :TSCommit string \"$BRANCH $COMMIT $DIFF\"" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}.dSYM/Contents/Info.plist"
echo "Completed writing the above data to the products."
// version-string.swift
// Created by Alexander Celeste on 8/1/17.
var versionString = ""
if let shortVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as? String {
versionString += "Version \(shortVersion)"
}
if let bundleVersion = Bundle.main.infoDictionary!["CFBundleVersion"] as? String {
versionString += " (build \(bundleVersion))"
}
if let commitString = Bundle.main.infoDictionary!["TSCommit"] as? String {
versionString += " (\(commitString))"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment