diff --git a/completion/available/gradle.completion.bash b/completion/available/gradle.completion.bash index 786450a6..35971d50 100644 --- a/completion/available/gradle.completion.bash +++ b/completion/available/gradle.completion.bash @@ -22,17 +22,9 @@ # Avoid inaccurate completions for subproject tasks COMP_WORDBREAKS=$(echo "$COMP_WORDBREAKS" | sed -e 's/://g') -__gradle-set-project-root-dir() { - local dir="${PWD}" - project_root_dir="${PWD}" - while [[ $dir != '/' ]]; do - if [[ -f "$dir/settings.gradle" || -f "$dir/gradlew" ]]; then - project_root_dir=$dir - return 0 - fi - dir="$(dirname "$dir")" - done - return 1 +function __gradle-set-project-root-dir() { + project_root_dir="$(_bash-it-find-in-ancestor "settings.gradle" "gradlew")" + return "$?" } __gradle-init-cache-dir() { diff --git a/lib/helpers.bash b/lib/helpers.bash index fbc6aa88..94df885d 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -849,3 +849,30 @@ then fi } fi + +# `_bash-it-find-in-ancestor` uses the shell's ability to run a function in +# a subshell to simplify our search to a simple `cd ..` and `[[ -r $1 ]]` +# without any external dependencies. Let the shell do what it's good at. +function _bash-it-find-in-ancestor() ( + about 'searches parents of the current directory for any of the specified file names' + group 'helpers' + param '*: names of files or folders to search for' + returns '0: prints path of closest matching ancestor directory to stdout' + returns '1: no match found' + returns '2: improper usage of shell builtin' # uncommon + example '_bash-it-find-in-ancestor .git .hg' + example '_bash-it-find-in-ancestor GNUmakefile Makefile makefile' + + local kin + # To keep things simple, we do not search the root dir. + while [[ "${PWD}" != '/' ]]; do + for kin in "$@"; do + if [[ -r "${PWD}/${kin}" ]]; then + printf '%s' "${PWD}" + return "$?" + fi + done + command cd .. || return "$?" + done + return 1 +) diff --git a/plugins/available/gradle.plugin.bash b/plugins/available/gradle.plugin.bash index 6267bd84..8dec1313 100644 --- a/plugins/available/gradle.plugin.bash +++ b/plugins/available/gradle.plugin.bash @@ -3,19 +3,10 @@ about-plugin 'Add a gw command to use gradle wrapper if present, else use system function gw() { local file="gradlew" - local curr_path="${PWD}" - local result="gradle" + local result - # Search recursively upwards for file. - until [[ "${curr_path}" == "/" ]]; do - if [[ -e "${curr_path}/${file}" ]]; then - result="${curr_path}/${file}" - break - else - curr_path=$(dirname "${curr_path}") - fi - done + result="$(_bash-it-find-in-ancestor "${file}")" # Call gradle - "${result}" $* + "${result:-gradle}" $* }