Merge pull request #1952 from gaelicWizard/uncle

lib/helpers: new function `_bash-it-find-in-ancestor()`
pull/1966/head
Noah Gorny 2021-09-28 16:51:30 +03:00 committed by GitHub
commit 7fb7bb9cb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 23 deletions

View File

@ -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() {

View File

@ -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
)

View File

@ -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}" $*
}