Init script automatically adds rbenv to PATH if necessary (#1432)

This commit is contained in:
Mislav Marohnić 2022-09-24 23:59:13 +02:00 committed by GitHub
parent 117a381575
commit 9572edf4bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 7 deletions

View file

@ -41,7 +41,9 @@ if [ -z "$shell" ]; then
shell="${shell%%-*}" shell="${shell%%-*}"
fi fi
root="${0%/*}/.." root="${BASH_SOURCE:-$0}"
root="${root%/*}"
root="${root%/*}"
if [ -z "$print" ]; then if [ -z "$print" ]; then
case "$shell" in case "$shell" in
@ -74,6 +76,7 @@ if [ -z "$print" ]; then
echo 'status --is-interactive; and rbenv init - fish | source' echo 'status --is-interactive; and rbenv init - fish | source'
;; ;;
* ) * )
# shellcheck disable=SC2016
printf 'eval "$(rbenv init - %s)"\n' "$shell" printf 'eval "$(rbenv init - %s)"\n' "$shell"
;; ;;
esac esac
@ -85,18 +88,27 @@ fi
mkdir -p "${RBENV_ROOT}/"{shims,versions} mkdir -p "${RBENV_ROOT}/"{shims,versions}
rbenv_in_path=true
if [ -n "$RBENV_ORIG_PATH" ]; then
PATH="$RBENV_ORIG_PATH" command -v rbenv >/dev/null || rbenv_in_path=""
fi
case "$shell" in case "$shell" in
fish ) fish )
echo "set -gx PATH '${RBENV_ROOT}/shims' \$PATH" [ -n "$rbenv_in_path" ] || printf "set -gx PATH '%s/bin' \$PATH\n" "$root"
echo "set -gx RBENV_SHELL $shell" printf "set -gx PATH '%s/shims' \$PATH\n" "$RBENV_ROOT"
printf 'set -gx RBENV_SHELL %s\n' "$shell"
;; ;;
* ) * )
echo 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"' # shellcheck disable=SC2016
echo "export RBENV_SHELL=$shell" [ -n "$rbenv_in_path" ] || printf 'export PATH="%s/bin:${PATH}"\n' "$root"
# shellcheck disable=SC2016
printf 'export PATH="%s/shims:${PATH}"\n' "$RBENV_ROOT"
printf 'export RBENV_SHELL=%s\n' "$shell"
completion="${root}/completions/rbenv.${shell}" completion="${root}/completions/rbenv.${shell}"
if [ -r "$completion" ]; then if [ -r "$completion" ]; then
echo "source '$completion'" printf "source '%s'\n" "$completion"
fi fi
;; ;;
esac esac

View file

@ -21,7 +21,7 @@ load test_helper
root="$(cd $BATS_TEST_DIRNAME/.. && pwd)" root="$(cd $BATS_TEST_DIRNAME/.. && pwd)"
run rbenv-init - bash run rbenv-init - bash
assert_success assert_success
assert_line "source '${root}/test/../libexec/../completions/rbenv.bash'" assert_line "source '${root}/test/../completions/rbenv.bash'"
} }
@test "detect parent shell" { @test "detect parent shell" {