Skip to content

Instantly share code, notes, and snippets.

@christophermark
Last active January 9, 2024 13:13
Show Gist options
  • Save christophermark/ccc1d6f460959787da9ebaf9e5a8feb0 to your computer and use it in GitHub Desktop.
Save christophermark/ccc1d6f460959787da9ebaf9e5a8feb0 to your computer and use it in GitHub Desktop.
Shell script to set the iOS version of a project
# Prints out the iOS version number
printIosVersion() {
sed -n 'N;s/.*CFBundleShortVersionString.*>\([0-9]*\.[0-9]*\.[0-9]*\).*/\1/p' ios/Compass/Info.plist
}
# Sets the iOS version number
#
# {$1} The new version number to set
###### Documentation ######
# sed -i '' ' # -i '' will modify the file and save it
# /key\>$/{ # Make sure we match the right 2 lines (not the line above)
# N; # allows us to pattern match over 2 lines instead of 1
# s/ # initiates a find-and-replace
# \(.*CFBundleShortVersionString.*>\) # First regex capture group, before the version number
# [0-9]*\.[0-9]*\.[0-9]* # The version number, to be replaced
# \(.*\) # Second regex capture group, after the version number
# /\1foo\2/' # Keep the capture groups, substitute version for `foo`
# }
# ios/Compass/Info.plist # The file to modify
setIosVersion() {
if [ $# -eq 0 ]
then
echo "Error: No version provided!\nPlease pass a version number parameter to this script."
exit 1
else
echo "Setting the iOS version to: $1"
fi
sed -i "" "/key\>$/{
N
s/\(.*CFBundleShortVersionString.*>\)[0-9]*\.[0-9]*\.[0-9]*\(.*\)/\1$1\2/
}" ios/Compass/Info.plist
}
# Increments the iOS build number
incrementIosBuildNumber() {
# Get the build number
#
# Ensure we start on the CFBundleVersion line, not the line above it.
# Since we're looking at lines 2 at a time, we need to make sure we
# match the CFBundleVersion line and the line below (with the version).
buildNumber=`sed -n "/key\>$/{
N
s/\(.*CFBundleVersion[^0-9]*\)\([0-9]*\)\(.*\)/\2/p
}" ios/Compass/Info.plist`
newBuild=$(($buildNumber + 1))
echo "Incrementing the build number: $newBuild"
sed -i "" "/key\>$/{
N
s/\(.*key\>CFBundleVersion[^0-9]*\)[0-9]*\(.*\)/\1$newBuild\2/
}" ios/Compass/Info.plist
}
#### Main script starts here ####
# This shell script must be passed a version number as the first argument
newVersion="$1"
if [ $# -eq 0 ]
then
echo "Error: No version provided!\nPlease pass a version number parameter to this script."
exit 1
elif [[ ! $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] # Accepts versions like: 2.10.1
then
echo "Error: Unsupported version number format!\nPlease pass a version number in the format: 2.10.1"
exit 1
else
echo "Upgrading the project's version number..."
fi
setIosVersion $newVersion
incrementIosBuildNumber
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment