Skip to content

Instantly share code, notes, and snippets.

@tpinto
Last active March 28, 2022 11:22
Show Gist options
  • Save tpinto/8cd18670502979e8110c to your computer and use it in GitHub Desktop.
Save tpinto/8cd18670502979e8110c to your computer and use it in GitHub Desktop.
Xcode: Automatic version and build number for iOS apps on Info.plist and Settings.bundle - http://www.tiagopinto.pt/blog/2014/11/10/xcode-version-build-number-ios-info-plist-settings-bundle/
# Tips from: http://xcodehelp.blogspot.ie/2012/05/add-version-number-to-settings-screen.html
# Reference from: https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html
# Get the correct path for the git binary
git=`sh /etc/profile; which git`
# Save paths for the project and target Info.plist
projectPlistPath="${PROJECT_DIR}/${INFOPLIST_FILE}"
targetPlistPath="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
# Get the base build number from project Info.plist (previous build, e.g. 171)
baseBuildNumber=$(echo -n `/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$projectPlistPath"`)
# Increment the base build number by one
newBuildNumber=$(expr $baseBuildNumber + 1)
# Update raw build number for both Info.plists (project and target)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $newBuildNumber" "$projectPlistPath"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $newBuildNumber" "$targetPlistPath"
# Get latest git commit SHA1
gitBuildNumber=$(echo -n `"$git" reflog --oneline | head -1 | awk '{print $1}'`)
# Get git branch name
gitBranchName=`"$git" rev-parse --abbrev-ref HEAD`
# Making sure we don't have any \n in there
gitBuildWithBranch=$(echo -n "$gitBuildNumber-$gitBranchName")
# Getting the base version from the project Info.plist (e.g. 1.3.5)
baseVersion=$(echo -n `/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$projectPlistPath"`)
# Join them all together e.g. 1.3.5-172 (7ca1d87-new-feature)
fullVersion="$baseVersion-$newBuildNumber ($gitBuildWithBranch)"
# This is where the Settings.bundle's plist is located. YMMV
targetSettingsBundlePath="${TARGET_BUILD_DIR}/${CONTENTS_FOLDER_PATH}/Settings.bundle/Root.plist"
# Save the full version on the Settings.bundle for debug purposes
/usr/libexec/PlistBuddy -c "Set :PreferenceSpecifiers:9:DefaultValue $fullVersion" "$targetSettingsBundlePath"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment