Skip to content

Instantly share code, notes, and snippets.

@jobearrr
Last active February 2, 2021 10:46
Show Gist options
  • Save jobearrr/c95bbf85e0fcfc921d45c146dece43ba to your computer and use it in GitHub Desktop.
Save jobearrr/c95bbf85e0fcfc921d45c146dece43ba to your computer and use it in GitHub Desktop.
Increment version numbers on Xcode
# This came out of an answer I gave on stackoverflow:
# https://stackoverflow.com/questions/66006333/auto-increment-version-number-in-ios-application-using-azure-devops/66007332#66007332
# This script will increment the version numbers each time it's executed:
# 0.0.1 -> 0.0.2 -> ... -> 0.0.99 -> 0.1.0 -> 0.1.1 -> 0.1.2 -> ... -> 0.1.99 -> 0.2.0 -> ... -> 0.99.99 -> 1.0.0
#
# It can be easily adapted to follow other rules.
# And it can also be integrated as a build a build phase in a Xcode project.
buildPlist="Info.plist" # Enter the path to your plist file here
maxSubversionNumber=99 # Set what will be the maximum number to increment each sub version to
versionNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$buildPlist")
majorVersion=`echo $versionNumber | awk -F "." '{print ($1 == "" ? 0 : $1)}'`
minorVersion=`echo $versionNumber | awk -F "." '{print ($2 == "" ? 0 : $2)}'`
patchVersion=`echo $versionNumber | awk -F "." '{print ($3 == "" ? 0 : $3)}'`
if [[ $patchVersion -ge $maxSubversionNumber ]]
then
patchVersion=0
if [[ $minorVersion -ge $maxSubversionNumber ]]
then
minorVersion=0
majorVersion=$(($majorVersion + 1))
else
minorVersion=$(($minorVersion + 1))
fi
else
patchVersion=$(($patchVersion + 1))
fi
newVersionNumber=`echo $versionNumber | awk -F "." '{print '$majorVersion' "." '$minorVersion' ".'$patchVersion'" }'`
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $newVersionNumber" "$buildPlist"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment