Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save m-irfan/5eff8629ff6fda57326b5f103291553d to your computer and use it in GitHub Desktop.
Save m-irfan/5eff8629ff6fda57326b5f103291553d to your computer and use it in GitHub Desktop.
Xcode Shell Script to auto increase app version number without changing $(MARKETING_VERSION) from Info.plist file, only increases if archive succeeds.
#!/bin/sh
#
# Irfan Anwar : Software Engineer @ the ENTERTAINER : iamirfan@live.com
#
# Auto increase app version in archive - Pre-action Script
# NOTE: Add Post-action Script given below with this Pre-action Script to make it functional.
# It increases first part of desimal version number e.g 1.0 -> 2.0, ofcourse you can modify the logic, like add more desimal number or increase tiny part or minor part, like 1.0.3 -> 1.0.4 or whatever if you are a smart developer :)
#
# USAGE:
# 1. Open Edit Scheme Window in Xcode
# 2. Expand Build tab from left panel
# 3. Select Pre-actions tab.
# 4. Click on + button on bottom left side of detail panel.
# 5. Choose `New Run Script Action`
# 6. Choose your target from `Provide build settings` from menu.
# 7. Copy paste following script.
#
# MOST IMPORTANT - First make a backup of your project (compress/duplicate) before running below scripts-
if [ $CONFIGURATION == Release ]; then # comment this line if you want to increase on every build, this line make sure to increase app version only while Archiving for Release.
versionNumber="$MARKETING_VERSION"
$(/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $versionNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}")
Version=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${PROJECT_DIR}/${INFOPLIST_FILE}")
NEWSUBVERSION=$(echo $Version | awk -F "." '{print $1}')
NEWSUBVERSION=$(($NEWSUBVERSION + 1))
NEWVERSIONSTRING=$(echo $Version | awk -F "." '{print "'$NEWSUBVERSION'" "." $2}')
$(/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $NEWVERSIONSTRING" "${PROJECT_DIR}/${INFOPLIST_FILE}")
else
echo $CONFIGURATION " build - Not bumping version number."
fi
############################################################################################################
# POST ACTION #
############################################################################################################
# Auto increase app version in archive - Post-action Script
# NOTE: Add Pre-action Script with this Post-action Script to make it functional.
# replace Al-Futtaim with your project name
#
# USAGE:
# 1. Open Edit Scheme Window in Xcode
# 2. Expand Build tab from left panel
# 3. Select Post-actions tab.
# 4. Click on + button on bottom left side of detail panel.
# 5. Choose `New Run Script Action`
# 6. Choose your target from `Provide build settings` from menu.
# 7. Copy paste following script.
# 8. Replace `yourProjectName` on line 61 with your apps project name check your apps root folder for exact name like SampleApp.xcodeproj
# 9. Archive your project.
#
if [ $CONFIGURATION == Release ]; then # comment this line if you want to increase on every build, this line make sure to increase app version only while Archiving for Release.
oldVersion="$MARKETING_VERSION"
newVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${PROJECT_DIR}/${INFOPLIST_FILE}")
sed -i '' -e "s+MARKETING_VERSION = $oldVersion+MARKETING_VERSION = $newVersion+g" "${PROJECT_DIR}/yourProjectName.xcodeproj/project.pbxproj"
$(/usr/libexec/PlistBuddy -c 'Set :CFBundleShortVersionString $(MARKETING_VERSION)' "${PROJECT_DIR}/${INFOPLIST_FILE}")
else
echo $CONFIGURATION " build - Not bumping version number."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment