utils/ruby.sh: remove dependency for which command

This commit is contained in:
XuehaiPan 2021-09-11 01:15:13 +08:00
parent 0f0c7102f0
commit 5c6d2b154f

View File

@ -1,9 +1,31 @@
export HOMEBREW_REQUIRED_RUBY_VERSION=2.6.3 export HOMEBREW_REQUIRED_RUBY_VERSION=2.6.3
# Search given executable in all PATH entries (remove dependency for `which` command)
which_all() {
if [[ $# -ne 1 ]]
then
return 1
fi
local executable entries entry retcode=1
IFS=':' read -r -a entries <<< "${PATH}" # `readarray -d ':' -t` seems not applicable on WSL Bash
for entry in "${entries[@]}"
do
executable="${entry}/$1"
if [[ -x "${executable}" ]]
then
echo "${executable}"
retcode=0 # present
fi
done
return "${retcode}"
}
# HOMEBREW_LIBRARY is from the user environment # HOMEBREW_LIBRARY is from the user environment
# shellcheck disable=SC2154 # shellcheck disable=SC2154
test_ruby() { test_ruby() {
if [[ ! -x $1 ]] if [[ ! -x "$1" ]]
then then
return 1 return 1
fi fi
@ -15,22 +37,23 @@ test_ruby() {
# HOMEBREW_MACOS is set by brew.sh # HOMEBREW_MACOS is set by brew.sh
# HOMEBREW_PATH is set by global.rb # HOMEBREW_PATH is set by global.rb
# SC2230 falsely flags `which -a` # shellcheck disable=SC2154
# shellcheck disable=SC2154,SC2230
find_ruby() { find_ruby() {
if [[ -n "${HOMEBREW_MACOS}" ]] if [[ -n "${HOMEBREW_MACOS}" ]]
then then
echo "/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby" echo "/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby"
else else
IFS=$'\n' # Do word splitting on new lines only local ruby_exec
for ruby_exec in $(which -a ruby 2>/dev/null) $(PATH=${HOMEBREW_PATH} which -a ruby 2>/dev/null) while read -r ruby_exec
do do
if test_ruby "${ruby_exec}"; then if test_ruby "${ruby_exec}"; then
echo "${ruby_exec}" echo "${ruby_exec}"
break break
fi fi
done done < <(
IFS=$' \t\n' # Restore IFS to its default value which_all ruby
PATH="${HOMEBREW_PATH}" which_all ruby
)
fi fi
} }