Skip to content

Instantly share code, notes, and snippets.

@muncman
Last active July 19, 2018 06:48
Show Gist options
  • Save muncman/6762380 to your computer and use it in GitHub Desktop.
Save muncman/6762380 to your computer and use it in GitHub Desktop.
Pair of scripts for automatically updating the build number for Xcode projects. The second one restores the number so you have no outgoing version control changes.
#!/bin/sh
# Set in Product | Scheme | Edit Scheme | Build | Post-actions as a new Run Script item.
# echo Restoring build number
# ${PROJECT_DIR}/Scripts/restore_build_number.sh
# Select your target in the "Provide build settings from" dropdown.
set -e
if [ -z "$SRCROOT" ]
then
echo "Run this from the project root."
export SRCROOT=`pwd`
echo "Setting SRCROOT to $SRCROOT"
fi
if [ -z "$INFOPLIST_FILE" ]
then
export INFOPLIST_FILE=YOUR_PROJECT/YOUR_PROJECT-Info.plist
echo "Setting INFOPLIST_FILE to $INFOPLIST_FILE"
fi
if [ -f "$SRCROOT/Scripts/previous_build_number.txt" ]
then
CFBundleVersion=`cat $SRCROOT/Scripts/previous_build_number.txt`
else
exit 0
fi
echo Restoring CFBundleVersion to $CFBundleVersion
[ -f "$SRCROOT/Scripts/previous_build_number.txt" ] && rm -f "$SRCROOT/Scripts/previous_build_number.txt"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $CFBundleVersion" "$SRCROOT/$INFOPLIST_FILE"
#!/bin/sh
# Set in Product | Scheme | Edit Scheme | Build | Pre-actions as a new Run Script item.
# echo Updating build number
# ${PROJECT_DIR}/Scripts/update_build_number.sh
# Select your target in the "Provide build settings from" dropdown.
# NOTE: You need to change YOUR_PROJECT with your actual project name.
set -e
if [ -z "$SRCROOT" ]
then
echo "Run this from the project root."
export SRCROOT=`pwd`
echo "Setting SRCROOT to $SRCROOT"
fi
if [ -z "$INFOPLIST_FILE" ]
then
export INFOPLIST_FILE=YOUR_PROJECT/YOUR_PROJECT-Info.plist
echo "Setting INFOPLIST_FILE to $INFOPLIST_FILE"
fi
# Clean slate, if needed, to start.
# Sometimes Xcode is lame and doesn't complete the Post-action reset.
$SRCROOT/Scripts/restore_build_number.sh
CFBundleVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$SRCROOT/$INFOPLIST_FILE")
echo CFBundleVersion was $CFBundleVersion
# Save for restoration; to avoid outgoing VCS change.
[ -f "$SRCROOT/Scripts/previous_build_number.txt" ] && rm -f "$SRCROOT/Scripts/previous_build_number.txt"
echo $CFBundleVersion > $SRCROOT/Scripts/previous_build_number.txt
DotTimestamp=$(date '+.%m%d%H%M')
echo Appending timestamp of $DotTimestamp
CFBundleVersion=$CFBundleVersion$DotTimestamp
echo Updating CFBundleVersion to $CFBundleVersion
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $CFBundleVersion" "$SRCROOT/$INFOPLIST_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment