From 9572edf4bb2a796bb4e3852dbdd93570b3d09c8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Sat, 24 Sep 2022 23:59:13 +0200 Subject: [PATCH] Init script automatically adds rbenv to PATH if necessary (#1432) --- libexec/rbenv-init | 24 ++++++++++++++++++------ test/init.bats | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/libexec/rbenv-init b/libexec/rbenv-init index ae2da55..cabea21 100755 --- a/libexec/rbenv-init +++ b/libexec/rbenv-init @@ -41,7 +41,9 @@ if [ -z "$shell" ]; then shell="${shell%%-*}" fi -root="${0%/*}/.." +root="${BASH_SOURCE:-$0}" +root="${root%/*}" +root="${root%/*}" if [ -z "$print" ]; then case "$shell" in @@ -74,6 +76,7 @@ if [ -z "$print" ]; then echo 'status --is-interactive; and rbenv init - fish | source' ;; * ) + # shellcheck disable=SC2016 printf 'eval "$(rbenv init - %s)"\n' "$shell" ;; esac @@ -85,18 +88,27 @@ fi 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 fish ) - echo "set -gx PATH '${RBENV_ROOT}/shims' \$PATH" - echo "set -gx RBENV_SHELL $shell" + [ -n "$rbenv_in_path" ] || printf "set -gx PATH '%s/bin' \$PATH\n" "$root" + 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}"' - echo "export RBENV_SHELL=$shell" + # shellcheck disable=SC2016 + [ -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}" if [ -r "$completion" ]; then - echo "source '$completion'" + printf "source '%s'\n" "$completion" fi ;; esac diff --git a/test/init.bats b/test/init.bats index 63fc8c6..da48163 100755 --- a/test/init.bats +++ b/test/init.bats @@ -21,7 +21,7 @@ load test_helper root="$(cd $BATS_TEST_DIRNAME/.. && pwd)" run rbenv-init - bash assert_success - assert_line "source '${root}/test/../libexec/../completions/rbenv.bash'" + assert_line "source '${root}/test/../completions/rbenv.bash'" } @test "detect parent shell" {