Skip to content

Instantly share code, notes, and snippets.

@m-irfan
Created February 3, 2022 14:14
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save m-irfan/760a3d9ede6c76b38f5e5fd748f3e315 to your computer and use it in GitHub Desktop.
Xcode Shell Script
#!/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 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.
#
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. Relace `yourProjectName` 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
@m-irfan
Copy link
Author

m-irfan commented May 31, 2022

Please make sure to make BACKUP of your source code before attempting to use above scripts.
This can corrupt your .pbxproj file, if XCode make changes in settings or if the project name have special characters etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment