Skip to content

Instantly share code, notes, and snippets.

@danielcarr
Last active May 5, 2016 09:48
Show Gist options
  • Save danielcarr/ac56fa40ef33c0113a7b746a26e794aa to your computer and use it in GitHub Desktop.
Save danielcarr/ac56fa40ef33c0113a7b746a26e794aa to your computer and use it in GitHub Desktop.
Script for incrementing build iOS app build number before building when multiple versions of the same app need to be released for testing
#! /usr/bin/env bash
usage() {
echo Usage:
echo "$0 <info_plist_file> [<build_number_directory>] [-v]"
echo "If a build number directory is given, the saved number will be checked,"
echo " and if it exists and is greater than the build number in the info plist,"
echo " it will be incremented and saved back to the given build number directory."
echo "If it does not exist, or it is smaller than the info plist build number,"
echo " the info plist number will be incremented and saved back."
echo "If the given directory does not exist, it will be created."
echo "If no directory is given, the number will be read from the info.plist"
echo " and not saved outside of it."
echo
echo "If the -v option is used, the build name and number will be printed out upon success"
echo
exit ${1:-0}
}
infoplist="${1}"
if [[ "${2}" != "-v" ]]; then
buildnumberdir="${2}"
else
verbose=1
fi
if [[ "${3}" = "-v" ]]; then
verbose=1
fi
if [ ! -f "${infoplist}" ]; then
usage 1
fi
plistbuddy() {
plistcommand="$@"
/usr/libexec/PlistBuddy -c "${plistcommand}" "${infoplist}"
}
if [[ -n "${buildnumberdir}" ]]; then
product=$(plistbuddy "Print :CFBundleDisplayName")
product=${product// /_}
version=$(plistbuddy "Print :CFBundleShortVersionString")
productdir="${buildnumberdir}/${product}"
if [[ ! -d "${productdir}" ]]; then
mkdir -p "${productdir}"
fi
buildnumberfile="${productdir}/${version}"
fi
bundled_build_number=$(plistbuddy "Print :CFBundleVersion")
if test -s "${buildnumberfile}"; then
saved_build_number=$(head -1 "${buildnumberfile}")
fi
if test ${bundled_build_number} -gt ${saved_build_number:-0}; then
# add 1 if $bundled_build_number is not 1, 0 if it is
test ${bundled_build_number} -eq 1
build_number=$((bundled_build_number + $?))
else
build_number=$((saved_build_number + 1))
fi
plistbuddy "Set :CFBundleVersion ${build_number}"
if [[ -n "${buildnumberfile}" ]]; then
echo ${build_number} > "${buildnumberfile}"
fi
if [[ "${verbose}" -gt 0 ]]; then
echo "Incremented build number: ${product} ${version} (${build_number})"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment