Skip to content

Instantly share code, notes, and snippets.

@guillaumerose
Created March 11, 2019 14:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save guillaumerose/b6110d8424b7a0796cd3389217f4e832 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Wrapper function for Maven's mvn command.
mvn-color()
{
# Formatting constants
BOLD=`tput bold`
UNDERLINE_ON=`tput smul`
UNDERLINE_OFF=`tput rmul`
TEXT_BLACK=`tput setaf 0`
TEXT_RED=`tput setaf 1`
TEXT_GREEN=`tput setaf 2`
TEXT_YELLOW=`tput setaf 3`
TEXT_BLUE=`tput setaf 4`
TEXT_MAGENTA=`tput setaf 5`
TEXT_CYAN=`tput setaf 6`
TEXT_WHITE=`tput setaf 7`
BACKGROUND_BLACK=`tput setab 0`
BACKGROUND_RED=`tput setab 1`
BACKGROUND_GREEN=`tput setab 2`
BACKGROUND_YELLOW=`tput setab 3`
BACKGROUND_BLUE=`tput setab 4`
BACKGROUND_MAGENTA=`tput setab 5`
BACKGROUND_CYAN=`tput setab 6`
BACKGROUND_WHITE=`tput setab 7`
RESET_FORMATTING=`tput sgr0`
# Filter mvn output using sed
mvn $@ | sed -e "s/\(\[INFO\]\ \-.*\)/${TEXT_BLUE}${BOLD}\1/g" \
-e "s/\(\[INFO\]\ \[.*\)/${RESET_FORMATTING}${BOLD}\1${RESET_FORMATTING}/g" \
-e "s/\(\[INFO\]\ BUILD SUCCESSFUL\)/${BOLD}${TEXT_GREEN}\1${RESET_FORMATTING}/g" \
-e "s/\(\[WARNING\].*\)/${BOLD}${TEXT_YELLOW}\1${RESET_FORMATTING}/g" \
-e "s/\(\[ERROR\].*\)/${BOLD}${TEXT_RED}\1${RESET_FORMATTING}/g" \
-e "s/Tests run: \([^,]*\), Failures: \([^,]*\), Errors: \([^,]*\), Skipped: \([^,]*\)/${BOLD}${TEXT_GREEN}Tests run: \1${RESET_FORMATTING}, Failures: ${BOLD}${TEXT_RED}\2${RESET_FORMATTING}, Errors: ${BOLD}${TEXT_RED}\3${RESET_FORMATTING}, Skipped: ${BOLD}${TEXT_YELLOW}\4${RESET_FORMATTING}/g"
# Store the exit status of the first command before the pipe
BUILD_RESULT=${PIPESTATUS[0]}
# Make sure formatting is reset
echo -ne ${RESET_FORMATTING}
}
# -Djava.awt.headless=true prevents Java from opening a Launcher app under Mac OS X
export MAVEN_OPTS="-Xmx2g -XX:+TieredCompilation -XX:TieredStopAtLevel=1 -Djava.awt.headless=true"
function alert_user {
echo "${1}"
if [ ! -z `which notify-send` ]; then
notify-send "${1}"
fi
# For Mac OS X: install from https://github.com/alloy/terminal-notifier
if [ -f /Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier ]
then
/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier -message "${1}"
fi
}
function exit_ko {
alert_user "${1}"
exit 1
}
function exit_ok {
alert_user "${1}"
exit 0
}
PROJECT_DIR=$(pwd)
BRANCH=$(git branch --no-color | awk '$1=="*" {print $2}')
ORIGIN=$(git remote -v | awk '$1=="origin" && $3=="(push)" {print $2}')
if [ -z != $1 ]; then
export BUILD_DIR=${BUILD_DIR:-"$1"}
else
export BUILD_DIR=${BUILD_DIR:-../privatebuild}
fi
echo "Using build directory $BUILD_DIR"
git fetch
git add -A; git ls-files --deleted -z | xargs -0 -I {} git rm {}; git commit -m "wip"
git rebase origin/${BRANCH}
if [ "$?" -ne 0 ]
then
git rebase --abort
git log -n 1 | grep -q -c "wip" && git reset HEAD~1
exit_ko "Unable to rebase. Please pull or rebase and fix conflicts manually."
fi
git log -n 1 | grep -q -c "wip" && git reset HEAD~1
rm -Rf $BUILD_DIR
git clone -slb "${BRANCH}" . $BUILD_DIR
cd $BUILD_DIR
MVNCI="mvn-color -T 4 clean install"
if [ "$2" == "fast" ]; then
$MVNCI -T 1C
else
$MVNCI
fi
if [ $BUILD_RESULT -ne 0 ]; then
exit_ko "Unable to build"
fi
if [ "$2" == "no-push" ]; then
exit_ok "Not publishing, as per the no-push option"
fi
git push $ORIGIN $BRANCH
if [ $? -ne 0 ]; then
exit_ko "Unable to push"
fi
cd ${PROJECT_DIR} && git fetch
exit_ok "Yet another successful push!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment