Skip to content

Instantly share code, notes, and snippets.

@stigkj
Forked from dougborg/gw.sh
Created October 1, 2013 20:06
Show Gist options
  • Save stigkj/6784305 to your computer and use it in GitHub Desktop.
Save stigkj/6784305 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Find the system gradle executable to fall back on.
GRADLE=$(which gradle)
# Default names for things
GRADLEW='gradlew'
BUILD_GRADLE='build.gradle'
GRADLEW_FOUND=false
BUILD_GRADLE_FOUND=false
CURR_PATH="${PWD}"
function error_exit() {
echo "${@}" >&2
exit 1
}
# First, search recursively upwards for a build.gradle.
# TODO: Make this smarter by parsing the args and use the provided buildfile if provided
until [[ "${CURR_PATH}" == "/" ]] || ${BUILD_GRADLE_FOUND}; do
if [[ -e "${CURR_PATH}/${BUILD_GRADLE}" ]]; then
BUILD_GRADLE_FOUND=true
BUILD_GRADLE="${CURR_PATH}/${BUILD_GRADLE}"
else
CURR_PATH=$(dirname "${CURR_PATH}")
fi
done
# Fail fast if we don't find a build.gradle.
! ${BUILD_GRADLE_FOUND} && error_exit "Unable to find a ${BUILD_GRADLE} in any parent directory of ${PWD}."
# Search recursively upwards from the first-found build.gradle for a gradlew.
until [[ "${CURR_PATH}" == "/" ]] || ${GRADLEW_FOUND}; do
if [[ -x "${CURR_PATH}/${GRADLEW}" ]]; then
GRADLEW_FOUND=true
# Prefer the gradle wrapper if one exists in this tree.
GRADLE="${CURR_PATH}/${GRADLEW}"
else
CURR_PATH=$(dirname "${CURR_PATH}")
fi
done
# Select the right gradle or error out if no good options exist.
if ${GRADLEW_FOUND}; then
GRADLE_TYPE="wrapper"
elif [[ -x ${GRADLE} ]]; then
GRADLE_TYPE="executable"
echo "There is no ${GRADLEW} set up for this project. You may want to consider setting one up."
echo "See: http://gradle.org/docs/current/userguide/gradle_wrapper.html"
else
error_exit "Unable to find ${GRADLEW} or a gradle executable installed and available on your path."
fi
# Say what we are gonna do, then do it.
echo -e "Using gradle ${GRADLE_TYPE} at '${GRADLE}' to run '${BUILD_GRADLE}':\n"
cd $(dirname ${BUILD_GRADLE}) && "${GRADLE}" "${@}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment