Skip to content

Instantly share code, notes, and snippets.

@hborders
Created September 14, 2016 18:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hborders/c4a283ae374ee169188c1028d646086f to your computer and use it in GitHub Desktop.
Save hborders/c4a283ae374ee169188c1028d646086f to your computer and use it in GitHub Desktop.
A script run within Xcode's `Run Script` build phase that outputs version info into `versions.h`, which we prepend onto our Info.plist template.
#!/bin/bash -euo pipefail
# When we increment TW_BUNDLE_SHORT_VERSION_STRING
# also update TW_BUNDLE_SHORT_VERSION_DATE to the current date/time
# we don't have to be very exact, but it should be updated at least
# once every 18 months because iTunes requires that a CFBundleVersion
# be at most 18 characters long, and DECIMALIZED_GIT_HASH will be
# at most 10 characters long. Thus, MINUTES_SINCE_DATE needs to be
# at most 7 characters long so we can use the format:
# ${MINUTES_SINCE_DATE}.${DECIMALIZED_GIT_HASH}
#
TW_BUNDLE_SHORT_VERSION_DATE="August 3 2016 00:00:00 GMT"
TW_BUNDLE_SHORT_VERSION_STRING=3.10
BASH_SOURCE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
MINUTES_SINCE_DATE="$( cd "${BASH_SOURCE_DIR}" && ./minutes_since_date.bash "${TW_BUNDLE_SHORT_VERSION_DATE}" )"
# decimalized git hash is guaranteed to be 10 characters or fewer because
# the biggest short=7 git hash we can get is FFFFFFF and
# $ ./decimalize_git_hash.bash FFFFFFF | wc -c
# > 10
DECIMALIZED_GIT_HASH="$( cd "${BASH_SOURCE_DIR}"; ./decimalize_git_hash.bash $( git rev-parse --short=7 HEAD ) )"
echo "Decimalized: \"${DECIMALIZED_GIT_HASH}\""
TW_BUNDLE_VERSION="${MINUTES_SINCE_DATE}"."${DECIMALIZED_GIT_HASH}"
cat <<EOF > "${SRCROOT}"/Versions/versions.h
#define TWBundleShortVersionString ${TW_BUNDLE_SHORT_VERSION_STRING}
#define TWBundleVersion ${TW_BUNDLE_VERSION}
EOF
@hborders
Copy link
Author

@rogerluan
Copy link

After 3+ years using this awesome set of scripts, I decided to clean it up a little bit 🤗

I solved the problem with the manually updated date, and extracted the app version to a separate file that contains just the app version (easier to script with it), e.g.:

1.0.0

I also got rid of the minutes_since_date.sh as I didn't see much value in keeping it around.

Here is the result:

#!/bin/bash

# dirname (containing directory) of the current bash script
BASH_SOURCE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Path of the file that contains the app version
APP_VERSION_FILEPATH="$BASH_SOURCE_DIR/../../app_version" # Customize this based on where your file is at

APP_VERSION_STRING="$(cat $APP_VERSION_FILEPATH)"
echo "App Version: \"${APP_VERSION_STRING}\""

APP_VERSION_LAST_MODIFIED_DATE=$(date -r $APP_VERSION_FILEPATH +%s)
MINUTES_SINCE_APP_VERSION_WAS_LAST_MODIFIED="$((($(date +%s) - $APP_VERSION_LAST_MODIFIED_DATE)/60))"
echo "Minutes since last modification: \"${MINUTES_SINCE_APP_VERSION_WAS_LAST_MODIFIED}\""

# Take the short version (7 char) of the current git commit hash and decimalize it
DECIMALIZED_GIT_HASH="$( cd "${BASH_SOURCE_DIR}"; ./decimalize_git_hash.sh $( git rev-parse --short=7 HEAD ) )"
echo "Decimalized Hash: \"${DECIMALIZED_GIT_HASH}\""

BUILD_NUMBER="${MINUTES_SINCE_APP_VERSION_WAS_LAST_MODIFIED}"."${DECIMALIZED_GIT_HASH}"
echo "Build Number: \"${BUILD_NUMBER}\""

cat <<EOF > "${SRCROOT}"/scripts/versions.h
#define AppVersion ${APP_VERSION_STRING}
#define BuildNumber ${BUILD_NUMBER}
EOF

Thank you so much for open sourcing these scripts @hborders! 🎉

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