#!/bin/bash init() { SOURCE_DIR=$1 GIT_DIR=$SOURCE_DIR VERSION_FILE=$SOURCE_DIR/reapi/version/version.h APPVERSION_FILE=$SOURCE_DIR/reapi/version/appversion.h APPVERSION_FILE_INC=$2 GENERATE_INC=0 PREFIX_INC_LOWER=${APPVERSION_FILE_INC,,} PREFIX_INC_UPPER=${APPVERSION_FILE_INC^^} DEFINE_PREFIXINC="#define ${PREFIX_INC_UPPER}_VERSION" if test -z "`git --version`"; then echo "Please install git client" echo "sudo apt-get install git" exit -1 fi # # Read old version from $APPVERSION_FILE_INC, if present # if [ $? -ne 0 -o "$APPVERSION_FILE_INC" != "" ] && [ $? -ne 0 -o "$PREFIX_INC_UPPER" != "" ]; then APPVERSION_FILE_INC=$SOURCE_DIR/reapi/version/${APPVERSION_FILE_INC}_version.inc if [ $? -ne 0 -o "$APPVERSION_FILE_INC" != "" ] && [ $? -ne 0 -o "$PREFIX_INC_UPPER" != "" ]; then if test -f $APPVERSION_FILE_INC ; then OLD_VERSION_INC=$(cat $APPVERSION_FILE_INC | grep -wi "$DEFINE_PREFIXINC" | sed -e "s/$DEFINE_PREFIXINC.*[^0-9]\([0-9][0-9]*\).*/\1/i" -e "s/\r//g") fi GENERATE_INC=1 fi fi # Read old version if [ -e $APPVERSION_FILE ]; then OLD_VERSION=$(cat $APPVERSION_FILE | grep -wi '#define APP_VERSION' | sed -e 's/#define APP_VERSION[ \t\r\n\v\f]\+\(.*\)/\1/i' -e 's/\r//g') if [ $? -ne 0 ]; then OLD_VERSION="" else # Remove quotes OLD_VERSION=$(echo $OLD_VERSION | xargs) fi fi # Get major, minor and maintenance information from gradle.properties MAJOR=$(cat "$VERSION_FILE" | grep -wi 'VERSION_MAJOR' | sed -e 's/.*VERSION_MAJOR.*[^0-9]\([0-9][0-9]*\).*/\1/i' -e 's/\r//g') if [ $? -ne 0 -o "$MAJOR" = "" ]; then MAJOR=0 fi MINOR=$(cat "$VERSION_FILE" | grep -wi 'VERSION_MINOR' | sed -e 's/.*VERSION_MINOR.*[^0-9]\([0-9][0-9]*\).*/\1/i' -e 's/\r//g') if [ $? -ne 0 -o "$MINOR" = "" ]; then MINOR=0 fi MAINTENANCE=$(cat "$VERSION_FILE" | grep -i 'VERSION_MAINTENANCE' | sed -e 's/.*VERSION_MAINTENANCE.*[^0-9]\([0-9][0-9]*\).*/\1/i' -e 's/\r//g') if [ $? -ne 0 -o "$MAINTENANCE" = "" ]; then MAINTENANCE=0 fi BRANCH_NAME=$(git -C "$GIT_DIR/" rev-parse --abbrev-ref HEAD) if [ $? -ne 0 -o "$BRANCH_NAME" = "" ]; then BRANCH_NAME=master fi COMMIT_COUNT=$(git -C "$GIT_DIR/" rev-list --count $BRANCH_NAME) if [ $? -ne 0 -o "$COMMIT_COUNT" = "" ]; then COMMIT_COUNT=0 fi # # Configure remote url repository # # Get remote name by current branch BRANCH_REMOTE=$(git -C "$GIT_DIR/" config branch.$BRANCH_NAME.remote) if [ $? -ne 0 -o "$BRANCH_REMOTE" = "" ]; then BRANCH_REMOTE=origin fi # Get commit id COMMIT_SHA=$(git -C "$GIT_DIR/" rev-parse --verify HEAD) COMMIT_SHA=${COMMIT_SHA:0:7} # Get remote url COMMIT_URL=$(git -C "$GIT_DIR/" config remote.$BRANCH_REMOTE.url) URL_CONSTRUCT=0 if [[ "$COMMIT_URL" == *"git@"* ]]; then URL_CONSTRUCT=1 # Strip prefix 'git@' COMMIT_URL=${COMMIT_URL#git@} # Strip postfix '.git' COMMIT_URL=${COMMIT_URL%.git} # Replace ':' to '/' COMMIT_URL=${COMMIT_URL/:/\/} elif [[ "$COMMIT_URL" == *"https://"* ]]; then URL_CONSTRUCT=1 # Strip prefix 'https://' COMMIT_URL=${COMMIT_URL#https://} # Strip postfix '.git' COMMIT_URL=${COMMIT_URL%.git} fi if test "$URL_CONSTRUCT" -eq 1; then # Append extra string if [[ "$COMMIT_URL" == *"bitbucket.org"* ]]; then COMMIT_URL=$(echo https://$COMMIT_URL/commits/) else COMMIT_URL=$(echo https://$COMMIT_URL/commit/) fi fi # # Detect local modifications # if [ `git -C "$GIT_DIR/" ls-files -m | wc -l` = 0 ]; then MODIFIED= else MODIFIED=+m fi NEW_VERSION_INC="$MAJOR$MINOR$COMMIT_COUNT" NEW_VERSION="$MAJOR.$MINOR.$MAINTENANCE.$COMMIT_COUNT-dev$MODIFIED" # Update appversion.h if version has changed or modifications/mixed revisions detected if [ "$NEW_VERSION" != "$OLD_VERSION" ] || [ "$NEW_VERSION_INC" != "$OLD_VERSION_INC" ]; then update_appversion fi } update_appversion() { if test "$GENERATE_INC" -eq 1; then update_appversion_inc fi day=$(date +%d) year=$(date +%Y) hours=$(date +%H:%M:%S) month=$(LANG=en_us_88591; date +"%b") # Write appversion.h echo Updating appversion.h, new version is '"'$NEW_VERSION'"', the old one was $OLD_VERSION echo -e "#ifndef __APPVERSION_H__\r">$APPVERSION_FILE echo -e "#define __APPVERSION_H__\r">>$APPVERSION_FILE echo -e "\r">>$APPVERSION_FILE echo -e "//\r">>$APPVERSION_FILE echo -e "// This file is generated automatically.\r">>$APPVERSION_FILE echo -e "// Don't edit it.\r">>$APPVERSION_FILE echo -e "//\r">>$APPVERSION_FILE echo -e "\r">>$APPVERSION_FILE echo -e "// Version defines\r">>$APPVERSION_FILE echo -e '#define APP_VERSION "'$NEW_VERSION'"\r'>>$APPVERSION_FILE echo -e "#define APP_VERSION_C $MAJOR,$MINOR,$MAINTENANCE,$COMMIT_COUNT\r">>$APPVERSION_FILE echo -e '#define APP_VERSION_STRD "'$MAJOR.$MINOR.$MAINTENANCE.$COMMIT_COUNT'"\r'>>$APPVERSION_FILE echo -e "#define APP_VERSION_FLAGS 0x0L\r">>$APPVERSION_FILE echo -e "\r">>$APPVERSION_FILE echo -e '#define APP_COMMIT_DATE "'$month $day $year'"\r'>>$APPVERSION_FILE echo -e '#define APP_COMMIT_TIME "'$hours'"\r'>>$APPVERSION_FILE echo -e "\r">>$APPVERSION_FILE echo -e '#define APP_COMMIT_SHA "'$COMMIT_SHA'"\r'>>$APPVERSION_FILE echo -e '#define APP_COMMIT_URL "'$COMMIT_URL'"\r'>>$APPVERSION_FILE echo -e "\r">>$APPVERSION_FILE echo -e "#endif //__APPVERSION_H__\r">>$APPVERSION_FILE } update_appversion_inc() { echo Updating $APPVERSION_FILE_INC, new version is '"'$NEW_VERSION_INC'"', the old one was $OLD_VERSION_INC echo -e "#if defined _${PREFIX_INC_LOWER}_version_included\r">$APPVERSION_FILE_INC echo -e " #endinput\r">>$APPVERSION_FILE_INC echo -e "#endif\r">>$APPVERSION_FILE_INC echo -e "#define _${PREFIX_INC_LOWER}_version_included\r">>$APPVERSION_FILE_INC echo -e "\r">>$APPVERSION_FILE_INC echo -e "// $PREFIX_INC_LOWER version\r">>$APPVERSION_FILE_INC echo -e "#define ${PREFIX_INC_UPPER}_VERSION $NEW_VERSION_INC\r">>$APPVERSION_FILE_INC echo -e "#define ${PREFIX_INC_UPPER}_VERSION_MAJOR $MAJOR\r">>$APPVERSION_FILE_INC echo -e "#define ${PREFIX_INC_UPPER}_VERSION_MINOR $MINOR\r">>$APPVERSION_FILE_INC } # Initialise init $* # Exit normally exit 0