Skip to content

Instantly share code, notes, and snippets.

@MateusGabi
Created May 1, 2018 18:08
Show Gist options
  • Save MateusGabi/9380cb01c31908344b4cc5c497756ec0 to your computer and use it in GitHub Desktop.
Save MateusGabi/9380cb01c31908344b4cc5c497756ec0 to your computer and use it in GitHub Desktop.
Build Number 4 Gradlew Apps
#!/bin/bash
setProperty(){
awk -v pat="^$1=" -v value="$1=$2" '{ if ($0 ~ pat) print value; else print $0; }' $3 > $3.tmp
mv $3.tmp $3
}
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if ["$1" == ""]
then
echo "Use: bash buildnumber.sh to/app/path"
fi
echo "Checking for git repo in $DIR/$1/.git"
if [ -e "$1/.git" ]
then
echo Found this to be a git repo.
cd $DIR/$1
# Get a svn-like revision number that keeps increasing with every commit.
REV=$(git log --pretty=format:'' | wc -l | sed 's/[ \t]//g')
echo "git rev: $REV"
# Getting the current branch
GITBRANCH=$(git branch | grep "*" | sed -e 's/^* //')
echo "Git Branch: $GITBRANCH"
# full build hash
GITHASH=$(git rev-parse HEAD)
echo "Git Hash: $GITHASH"
# commonly used short hash
GITHASHSHORT=$(git rev-parse --short HEAD)
echo "Git Hash Short: $GITHASHSHORT"
NEAREST=$(git describe --abbrev=0 --match "v[0-9]*")
echo "Nearest release Tag: \"$NEAREST\""
MAJOR="0"
MINOR="0"
PATCH="0"
if [ "$NEAREST" == "" ]
then
echo "No release tag found!"
else
MAJOR=$(echo $NEAREST | cut -d "." -f 2)
if [ $MAJOR == "" ]
then
MAJOR="0"
else
MINOR=$(echo $NEAREST | cut -d "." -f 3)
if [ $MINOR == "" ]
then
MINOR="0"
else
PATCH=$(echo $NEAREST | cut -d "." -f 4)
if [ $PATCH == "" ]
then
PATCH="0"
fi
fi
fi
fi
echo "Getting Date"
DATE=$(date +%Y%m%d)
echo "Version String: $MAJOR.$MINOR.$PATCH [$REV] $DATE"
echo "Writing on gradle.propeties"
FILE="gradle.properties"
setProperty "VERSION" $MAJOR.$MINOR.$PATCH $FILE
setProperty "BUILD" $REV $FILE
setProperty "DATE" $DATE $FILE
echo "Thanks!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment