Skip to content

Instantly share code, notes, and snippets.

@markbattistella
Created December 27, 2023 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markbattistella/40bee562273652247e4df26c6e29d5f4 to your computer and use it in GitHub Desktop.
Save markbattistella/40bee562273652247e4df26c6e29d5f4 to your computer and use it in GitHub Desktop.
Auto-increment Xcode project number
#!/bin/sh
#
# auto-increment.sh
# Author: Mark Battistella
# Website: https://markbattistella.com
#
# get the path for the config file
CONFIG_FILE="${PROJECT_DIR}/Shared/Data/Config.xcconfig"
# get the git commit count
GIT_COMMIT_COUNT=`git rev-list --all --count`
# get the git commit hash
NEW_GIT_COMMIT_HASH=`git log -1 --pretty="%h"`
# read the old values
CURRENT_PROJECT_VERSION=`source "$CONFIG_FILE"; echo $CURRENT_PROJECT_VERSION`
CURRENT_MARKETING_VERSION=`source "$CONFIG_FILE"; echo $MARKETING_VERSION`
CURRENT_GIT_COMMIT_HASH=`source "$CONFIG_FILE"; echo $GIT_COMMIT_HASH`
# splits a two-decimal version string, such as "4.1.9"
MAJORVERSION=`echo "$CURRENT_MARKETING_VERSION" | awk -F "." '{print $1}'`
MINORVERSION=`echo "$CURRENT_MARKETING_VERSION" | awk -F "." '{print $2}'`
PATCHVERSION=`echo "$CURRENT_MARKETING_VERSION" | awk -F "." '{print $3}'`
# increment the patch version
PATCHVERSION=$(($PATCHVERSION + 1))
# only do this on git commits
# -- if the old one is the same as the current
# -- then exit
if [ "$NEW_GIT_COMMIT_HASH" == "$CURRENT_GIT_COMMIT_HASH" ]; then
echo "Info: no changes in git commits"
exit 0
# -- otherwise there are changes
# -- so lets update the file
else
# check if there is a build number to bump
if [ -z "$CURRENT_PROJECT_VERSION" ]; then
echo "Error: Empty build number"
exit 0
fi
# set the build number to commits
CURRENT_PROJECT_VERSION=$GIT_COMMIT_COUNT
# check the version values
# -- if patch is 10, reset to 0
# -- bump minor version +1
if [ "$PATCHVERSION" -gt 9 ]; then
PATCHVERSION=$(($PATCHVERSION % 10))
MINORVERSION=$(($MINORVERSION + 1))
fi
# -- if minor is 10, reset to 0
# -- bump major version +1
if [ "$MINORVERSION" -gt 9 ]; then
MINORVERSION=$(($MINORVERSION % 10))
MAJORVERSION=$(($MAJORVERSION + 1))
fi
# set new version number
NEW_VERSION_STRING=`echo $VERSIONNUM | awk -F "." '{ print '$MAJORVERSION' "." '$MINORVERSION' ".'$PATCHVERSION'" }'`
# -- delete the old lines
echo "`sed /^CURRENT_PROJECT_VERSION/d "$CONFIG_FILE"`" > $CONFIG_FILE
echo "`sed /^MARKETING_VERSION/d "$CONFIG_FILE"`" > $CONFIG_FILE
echo "`sed /^GIT_COMMIT_HASH/d "$CONFIG_FILE"`" > $CONFIG_FILE
# -- add in the new values
echo "CURRENT_PROJECT_VERSION=$CURRENT_PROJECT_VERSION" >> $CONFIG_FILE
echo "MARKETING_VERSION=$NEW_VERSION_STRING" >> $CONFIG_FILE
echo "GIT_COMMIT_HASH=$NEW_GIT_COMMIT_HASH" >> $CONFIG_FILE
fi
@markbattistella
Copy link
Author

Not sure if 100% working, but using the git commit number as the build number:

#!/bin/sh

#
# auto-increment.sh
# Author: Mark Battistella
# Website: https://markbattistella.com
#

# get the path for the config file
CONFIG_FILE="${PROJECT_DIR}/Shared/Data/Config.xcconfig"

# get the git commit count
GIT_COMMIT_COUNT=`git rev-list --all --count`

# get the git commit hash
NEW_GIT_COMMIT_HASH=`git log -1 --pretty="%h"`

# read the old values
CURRENT_PROJECT_VERSION=`source "$CONFIG_FILE"; echo $CURRENT_PROJECT_VERSION`
CURRENT_MARKETING_VERSION=`source "$CONFIG_FILE"; echo $MARKETING_VERSION`
CURRENT_GIT_COMMIT_HASH=`source "$CONFIG_FILE"; echo $GIT_COMMIT_HASH`

# splits a two-decimal version string, such as "4.1.9"
MAJORVERSION=`echo "$CURRENT_MARKETING_VERSION" | awk -F "." '{print $1}'`
MINORVERSION=`echo "$CURRENT_MARKETING_VERSION" | awk -F "." '{print $2}'`
PATCHVERSION=`echo "$CURRENT_MARKETING_VERSION" | awk -F "." '{print $3}'`

# increment the patch version
PATCHVERSION=$(($PATCHVERSION + 1))

# only do this on git commits
# -- if the old one is the same as the current
# -- then exit
if [ "$NEW_GIT_COMMIT_HASH" == "$CURRENT_GIT_COMMIT_HASH" ]; then
    echo "Info: no changes in git commits"
    exit 0
else
    # set the build number to the total number of commits
    CURRENT_PROJECT_VERSION=$GIT_COMMIT_COUNT

    # check the version values
    # -- if patch is 10, reset to 0
    # -- bump minor version +1
    if [ "$PATCHVERSION" -gt 9 ]; then
        PATCHVERSION=$(($PATCHVERSION % 10))
        MINORVERSION=$(($MINORVERSION + 1))
    fi

    # -- if minor is 10, reset to 0
    # -- bump major version +1
    if [ "$MINORVERSION" -gt 9 ]; then
        MINORVERSION=$(($MINORVERSION % 10))
        MAJORVERSION=$(($MAJORVERSION + 1))
    fi

    # set new version number
    NEW_VERSION_STRING="$MAJORVERSION.$MINORVERSION.$PATCHVERSION"

    # write the new values
    sed -i '' "/CURRENT_PROJECT_VERSION/d" "$CONFIG_FILE"
    sed -i '' "/MARKETING_VERSION/d" "$CONFIG_FILE"
    sed -i '' "/GIT_COMMIT_HASH/d" "$CONFIG_FILE"

    echo "CURRENT_PROJECT_VERSION=$CURRENT_PROJECT_VERSION" >> $CONFIG_FILE
    echo "MARKETING_VERSION=$NEW_VERSION_STRING" >> $CONFIG_FILE
    echo "GIT_COMMIT_HASH=$NEW_GIT_COMMIT_HASH" >> $CONFIG_FILE
fi

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