Skip to content

Instantly share code, notes, and snippets.

@twolfson
Last active October 22, 2021 05:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twolfson/213d34fe3fd747fc652911369fc77f76 to your computer and use it in GitHub Desktop.
Save twolfson/213d34fe3fd747fc652911369fc77f76 to your computer and use it in GitHub Desktop.
Increment CFBundleVersion script, generally reusable for incrementing version in bash
#!/usr/bin/env bash
# Exit on first error, unset variable, or pipe failure
set -euo pipefail
# Define our constants
PLIST_FILEPATH="path/to/plist.plist"
# Resolve our version being deployed
# https://gist.github.com/sekati/3172554
# DEV: `plutil` doesn't support extraction easily (could use `-o -` but it's wrapped in XML/binary/JSON)
version="$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$PLIST_FILEPATH")"
# Break up our versions
# https://stackoverflow.com/a/6253883
# DEV: `cut` can return the original string if the delimiter wasn't found, so we compare patch to the version
major="$(echo "$version" | cut -d . -f 1)"
minor="$(echo "$version" | cut -d . -f 2)"
patch="$(echo "$version" | cut -d . -f 3)"
if test "$major" == "" || test "$minor" == "" || test "$patch" == "" || test "$patch" == "$version"; then
echo "Unable to parse CFBundleVersion \"${version}\"" 1>&2
exit 1
fi
# Determine our increment target
increment_target="${1:-}"
if test "$increment_target" == ""; then
increment_target="minor"
fi
# Increment our target version as requested
if test "$increment_target" == "major"; then
# 1.6.3 -> 2.0.0
major="$(($major + 1))"
minor="0"
patch="0"
elif test "$increment_target" == "minor"; then
# 1.6.3 -> 1.7.0
minor="$(($minor + 1))"
patch="0"
elif test "$increment_target" == "patch"; then
# 1.6.3 -> 1.6.4
patch="$(($patch + 1))"
else
echo "Unrecognized increment target \"${increment_target}\". Please specify \"major\", \"minor\", or \"patch\"" 1>&2
echo "Usage: $0 [increment_target]" 1>&2
exit 1
fi
# Set our new version
new_version="${major}.${minor}.${patch}"
plutil -replace CFBundleVersion -string "$new_version" "$PLIST_FILEPATH"
plutil -replace CFBundleShortVersionString -string "$new_version" "$PLIST_FILEPATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment