Skip to content

Commit 7fb7bb9

Browse files
author
Noah Gorny
authored
Merge pull request Bash-it#1952 from gaelicWizard/uncle
lib/helpers: new function `_bash-it-find-in-ancestor()`
2 parents 008c2b3 + e8966ea commit 7fb7bb9

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

completion/available/gradle.completion.bash

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,9 @@
2222
# Avoid inaccurate completions for subproject tasks
2323
COMP_WORDBREAKS=$(echo "$COMP_WORDBREAKS" | sed -e 's/://g')
2424

25-
__gradle-set-project-root-dir() {
26-
local dir="${PWD}"
27-
project_root_dir="${PWD}"
28-
while [[ $dir != '/' ]]; do
29-
if [[ -f "$dir/settings.gradle" || -f "$dir/gradlew" ]]; then
30-
project_root_dir=$dir
31-
return 0
32-
fi
33-
dir="$(dirname "$dir")"
34-
done
35-
return 1
25+
function __gradle-set-project-root-dir() {
26+
project_root_dir="$(_bash-it-find-in-ancestor "settings.gradle" "gradlew")"
27+
return "$?"
3628
}
3729

3830
__gradle-init-cache-dir() {

lib/helpers.bash

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,3 +849,30 @@ then
849849
fi
850850
}
851851
fi
852+
853+
# `_bash-it-find-in-ancestor` uses the shell's ability to run a function in
854+
# a subshell to simplify our search to a simple `cd ..` and `[[ -r $1 ]]`
855+
# without any external dependencies. Let the shell do what it's good at.
856+
function _bash-it-find-in-ancestor() (
857+
about 'searches parents of the current directory for any of the specified file names'
858+
group 'helpers'
859+
param '*: names of files or folders to search for'
860+
returns '0: prints path of closest matching ancestor directory to stdout'
861+
returns '1: no match found'
862+
returns '2: improper usage of shell builtin' # uncommon
863+
example '_bash-it-find-in-ancestor .git .hg'
864+
example '_bash-it-find-in-ancestor GNUmakefile Makefile makefile'
865+
866+
local kin
867+
# To keep things simple, we do not search the root dir.
868+
while [[ "${PWD}" != '/' ]]; do
869+
for kin in "$@"; do
870+
if [[ -r "${PWD}/${kin}" ]]; then
871+
printf '%s' "${PWD}"
872+
return "$?"
873+
fi
874+
done
875+
command cd .. || return "$?"
876+
done
877+
return 1
878+
)

plugins/available/gradle.plugin.bash

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,10 @@ about-plugin 'Add a gw command to use gradle wrapper if present, else use system
33

44
function gw() {
55
local file="gradlew"
6-
local curr_path="${PWD}"
7-
local result="gradle"
6+
local result
87

9-
# Search recursively upwards for file.
10-
until [[ "${curr_path}" == "/" ]]; do
11-
if [[ -e "${curr_path}/${file}" ]]; then
12-
result="${curr_path}/${file}"
13-
break
14-
else
15-
curr_path=$(dirname "${curr_path}")
16-
fi
17-
done
8+
result="$(_bash-it-find-in-ancestor "${file}")"
189

1910
# Call gradle
20-
"${result}" $*
11+
"${result:-gradle}" $*
2112
}

0 commit comments

Comments
 (0)