Skip to content

Instantly share code, notes, and snippets.

@plechev
Last active October 25, 2017 09:40
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 plechev/3fcff40f8a5322aaf9cc7e85122a6f56 to your computer and use it in GitHub Desktop.
Save plechev/3fcff40f8a5322aaf9cc7e85122a6f56 to your computer and use it in GitHub Desktop.
Gradle shell command aliasing (Bash)
#!/usr/local/bin/bash
#
# Saves typing by allowing commands like `g cb` to be translated to `gradlew clean build`
# Mapping lists can be customised for dev environment
#
declare -A grt=(["c"]="clean"
["b"]="build"
["a"]="assemble"
["t"]="tasks"
["j"]="jar"
["m"]="model"
["r"]="tomcatRun"
["d"]="dockerImage"
["s"]="cassandraRunDaemon"
["w"]="wiremockRunDaemon"
["i"]="dependencyInsight --dependency"
["f"]="functionalTest"
["q"]="codenarcAll checkstyleAll"
["@"]="buildHtmlDocs"
["§"]="buildSwaggerUiDocs"
)
declare -A gro=(["u"]="--gui" # start gradle gui
["z"]="-t" # continuous build
["I"]="--info"
["X"]="--debug"
["S"]="--stacktrace"
["O"]="--offline"
["D"]="--daemon"
["W"]="--no-daemon"
)
declare -A grp=(
["Q"]="-PENVIRONMENT_NAME=stage"
["P"]="-PENVIRONMENT_NAME=prod"
["L"]="-DENVIRONMENT_NAME=devlocal"
)
declare -A grd=(
["n"]="-x test" # skip tasks
["h"]="-x checkstyleAll -x checkstyleTest -x codenarcAll -x codenarcTest"
)
declare -A gre=(
["/"]="&> /tmp/gradle-build.log"
)
function gd() {
__exec "$@"
}
function g() {
s="$1"
cmd=()
if [ "$s" == "?" ]; then
echo "Tasks: "
gparray "$(declare -p grt)"
echo "Options: "
gparray "$(declare -p gro)"
echo "Properties: "
gparray "$(declare -p grp)"
echo "JVM Options: "
gparray "$(declare -p grd)"
echo "Extras: "
gparray "$(declare -p gre)"
else
ro=""
declare -a a
# do goals first
for ((i=0; i<${#s}; i++)); do
cmd+=(${grt[${s:$i:1}]});
done
# options
for ((i=0; i<${#s}; i++)); do
cmd+=(${gro[${s:$i:1}]});
done
# properties
for ((i=0; i<${#s}; i++)); do
cmd+=(${grp[${s:$i:1}]});
done
# -D
for ((i=0; i<${#s}; i++)); do
cmd+=(${grd[${s:$i:1}]});
done
# extras
for ((i=0; i<${#s}; i++)); do
if [ ${s:$i:1} == ">" ]; then
ro=${gre[${s:$i:1}]}
else
cmd+=(${gre[${s:$i:1}]});
fi
done
cmd="${cmd[@]} $2 $ro"
fi
__exec "$cmd"
}
function __exec() {
cmd="$@"
if [ "$cmd" != "" ]; then
gradle=("./gradlew")
cmd="${gradle[@]} $cmd"
printf '_%.0s' {1..100}
printf "\n\tExecuting: %s\n" "$cmd"
printf '=%.0s' {1..100}
printf "\n\n"
eval "$cmd"
fi
}
function gparray {
eval "declare -A arg_array=${1#*=}"
for i in "${!arg_array[@]}"; do
printf " %s\t%s\n" "$i => ${arg_array[$i]}"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment