Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
I turned this into a full-blown project: http://www.gdub.rocks/
#!/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}" "${@}"
@raindev

This comment has been minimized.

Copy link

raindev commented Feb 23, 2014

👍 Thank you, @dougborg! Your script will save me plenty of time, I'm sure.

@dougborg

This comment has been minimized.

Copy link
Owner Author

dougborg commented Nov 6, 2014

@raindev: I just saw your comment. I now keep this updated @ http://www.gdub.rocks/

@Opalo

This comment has been minimized.

Copy link

Opalo commented Jul 18, 2016

@dougborg, have you thought about looking not only for build.gradle but for settings.gradle as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.