Skip to content

Instantly share code, notes, and snippets.

@chml
Last active January 18, 2022 00:06
Show Gist options
  • Save chml/6e68402b1052eb66573f to your computer and use it in GitHub Desktop.
Save chml/6e68402b1052eb66573f to your computer and use it in GitHub Desktop.
Using git commit count as Xcode build number
#
# https://gist.github.com/johankool/c33cffc0727b13f22f25
# Set the build number to the current git commit count.
# If we're using the Dev scheme, then we'll suffix the build
# number with the current branch name, to make collisions
# far less likely across feature branches.
# Based on:
# http://w3facility.info/question/how-do-i-force-xcode-to-rebuild-the-info-plist-file-in-my-project-every-time-i-build-the-project/
# http://blog.jaredsinclair.com/post/97193356620/the-best-of-all-possible-xcode-automated-build#fnref:p97193356620-1
#
git=`sh /etc/profile; which git`
branchName=`"$git" rev-parse --abbrev-ref HEAD`
buildNumber=$(expr $(git rev-list $branchName --count) - $(git rev-list HEAD..$branchName --count))
if [ $CONFIGURATION = "Debug" ] || [ $branchName != "master" ];then
build=$buildNumber-$branchName
else
build=$buildNumber
fi
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $build" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
echo "Updated ${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
echo "Updated build number to $build using branch '$branchName'"
# 把版本号显示在 Setting Bundle
version=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"`
versionBuild="$version\($build\)"
/usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:0:DefaultValue $versionBuild" "${TARGET_BUILD_DIR}/${FULL_PRODUCT_NAME}/Settings.bundle/Root.plist"
echo "Updated Settings.bundle"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment