From 5bfec84432e63c46df4724918080abe488ba64b4 Mon Sep 17 00:00:00 2001 From: Yamashita Yuu Date: Thu, 15 Aug 2013 23:01:13 +0900 Subject: [PATCH 1/3] add fish shell support --- completions/rbenv.fish | 22 ++++++++++++++++++++++ libexec/rbenv-init | 37 ++++++++++++++++++++++++++++++++++--- libexec/rbenv-sh-rehash | 12 +++++++++++- libexec/rbenv-sh-shell | 21 ++++++++++++++++++--- test/init.bats | 28 ++++++++++++++++++++++++---- test/shell.bats | 24 ++++++++++++++++++++---- 6 files changed, 129 insertions(+), 15 deletions(-) create mode 100644 completions/rbenv.fish diff --git a/completions/rbenv.fish b/completions/rbenv.fish new file mode 100644 index 00000000..3ca8e945 --- /dev/null +++ b/completions/rbenv.fish @@ -0,0 +1,22 @@ +function __fish_rbenv_needs_command + set cmd (commandline -opc) + if [ (count $cmd) -eq 1 -a $cmd[1] = 'rbenv' ] + return 0 + end + return 1 +end + +function __fish_rbenv_using_command + set cmd (commandline -opc) + if [ (count $cmd) -gt 1 ] + if [ $argv[1] = $cmd[2] ] + return 0 + end + end + return 1 +end + +complete -f -c rbenv -n '__fish_rbenv_needs_command' -a '(rbenv commands)' +for cmd in (rbenv commands) + complete -f -c rbenv -n "__fish_rbenv_using_command $cmd" -a "(rbenv completions $cmd)" +end diff --git a/libexec/rbenv-init b/libexec/rbenv-init index 2dbd67d1..c3a3bb02 100755 --- a/libexec/rbenv-init +++ b/libexec/rbenv-init @@ -62,6 +62,9 @@ if [ -z "$print" ]; then ksh ) profile='~/.profile' ;; + fish ) + profile='~/.config/fish/config.fish' + ;; * ) profile='your profile' ;; @@ -80,7 +83,14 @@ fi mkdir -p "${RBENV_ROOT}/"{shims,versions} if [[ ":${PATH}:" != *:"${RBENV_ROOT}/shims":* ]]; then - echo 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"' + case "$shell" in + fish ) + echo 'setenv PATH "'${RBENV_ROOT}'/shims"' '$PATH' ';' + ;; + * ) + echo 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"' + ;; + esac fi completion="${root}/completions/rbenv.${shell}" @@ -91,8 +101,27 @@ if [ -z "$no_rehash" ]; then fi commands=(`rbenv-commands --sh`) -IFS="|" -cat </dev/null || true" + +case "$shell" in +fish ) + # nothing to do + ;; +* ) + echo "hash -r 2>/dev/null || true" + ;; +esac diff --git a/libexec/rbenv-sh-shell b/libexec/rbenv-sh-shell index 4b89c6c0..d0ffe1b0 100755 --- a/libexec/rbenv-sh-shell +++ b/libexec/rbenv-sh-shell @@ -24,6 +24,7 @@ if [ "$1" = "--complete" ]; then fi version="$1" +shell="$(basename "$SHELL")" if [ -z "$version" ]; then if [ -z "$RBENV_VERSION" ]; then @@ -36,14 +37,28 @@ if [ -z "$version" ]; then fi if [ "$version" = "--unset" ]; then - echo "unset RBENV_VERSION" + case "$shell" in + fish ) + echo "set -e RBENV_VERSION" + ;; + * ) + echo "unset RBENV_VERSION" + ;; + esac exit fi # Make sure the specified version is installed. if rbenv-prefix "$version" >/dev/null; then - echo "export RBENV_VERSION=\"${version}\"" + case "$shell" in + fish ) + echo "setenv RBENV_VERSION \"${version}\"" + ;; + * ) + echo "export RBENV_VERSION=\"${version}\"" + ;; + esac else - echo "return 1" + echo "false" exit 1 fi diff --git a/test/init.bats b/test/init.bats index dc9fe302..a4c63199 100644 --- a/test/init.bats +++ b/test/init.bats @@ -18,13 +18,19 @@ load test_helper } @test "setup shell completions" { - export SHELL=/bin/bash root="$(cd $BATS_TEST_DIRNAME/.. && pwd)" - run rbenv-init - + SHELL=/bin/bash run rbenv-init - assert_success assert_line "source '${root}/libexec/../completions/rbenv.bash'" } +@test "setup shell completions (fish)" { + root="$(cd $BATS_TEST_DIRNAME/.. && pwd)" + SHELL=/usr/bin/fish run rbenv-init - + assert_success + assert_line '. "'${root}'/libexec/../completions/rbenv.fish";' +} + @test "option to skip rehash" { run rbenv-init - --no-rehash assert_success @@ -33,14 +39,28 @@ load test_helper @test "adds shims to PATH" { export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin" - run rbenv-init - + SHELL=/bin/bash run rbenv-init - assert_success assert_line 0 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"' } +@test "adds shims to PATH (fish)" { + export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin" + SHELL=/usr/bin/fish run rbenv-init - + assert_success + assert_line 0 'setenv PATH "'${RBENV_ROOT}'/shims" $PATH ;' +} + @test "doesn't add shims to PATH more than once" { export PATH="${RBENV_ROOT}/shims:$PATH" - run rbenv-init - + SHELL=/bin/bash run rbenv-init - assert_success refute_line 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"' } + +@test "doesn't add shims to PATH more than once (fish)" { + export PATH="${RBENV_ROOT}/shims:$PATH" + SHELL=/usr/bin/fish run rbenv-init - + assert_success + refute_line 'setenv PATH "'${RBENV_ROOT}'/shims" $PATH ;' +} diff --git a/test/shell.bats b/test/shell.bats index 579d31ae..b14d1cfb 100644 --- a/test/shell.bats +++ b/test/shell.bats @@ -11,26 +11,42 @@ load test_helper } @test "shell version" { - RBENV_VERSION="1.2.3" run rbenv-sh-shell + SHELL=/bin/bash RBENV_VERSION="1.2.3" run rbenv-sh-shell + assert_success 'echo "$RBENV_VERSION"' +} + +@test "shell version (fish)" { + SHELL=/usr/bin/fish RBENV_VERSION="1.2.3" run rbenv-sh-shell assert_success 'echo "$RBENV_VERSION"' } @test "shell unset" { - run rbenv-sh-shell --unset + SHELL=/bin/bash run rbenv-sh-shell --unset assert_success "unset RBENV_VERSION" } +@test "shell unset (fish)" { + SHELL=/usr/bin/fish run rbenv-sh-shell --unset + assert_success "set -e RBENV_VERSION" +} + @test "shell change invalid version" { run rbenv-sh-shell 1.2.3 assert_failure assert_output < Date: Sat, 28 Sep 2013 15:04:24 +0200 Subject: [PATCH 2/3] cleanup in fish Use process subtitution syntax: . (rbenv init -|psub) instead of: eval (rbenv init -) because the latter doesn't work well with newlines. --- libexec/rbenv-init | 17 +++++++++++------ libexec/rbenv-sh-rehash | 6 ++++-- test/init.bats | 6 +++--- test/rehash.bats | 14 ++++++++++++++ 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/libexec/rbenv-init b/libexec/rbenv-init index c3a3bb02..c240460d 100755 --- a/libexec/rbenv-init +++ b/libexec/rbenv-init @@ -73,7 +73,14 @@ if [ -z "$print" ]; then { echo "# Load rbenv automatically by adding" echo "# the following to ${profile}:" echo - echo 'eval "$(rbenv init -)"' + case "$shell" in + fish ) + echo '. (rbenv init -|psub)' + ;; + * ) + echo 'eval "$(rbenv init -)"' + ;; + esac echo } >&2 @@ -85,7 +92,7 @@ mkdir -p "${RBENV_ROOT}/"{shims,versions} if [[ ":${PATH}:" != *:"${RBENV_ROOT}/shims":* ]]; then case "$shell" in fish ) - echo 'setenv PATH "'${RBENV_ROOT}'/shims"' '$PATH' ';' + echo "setenv PATH '${RBENV_ROOT}/shims' \$PATH" ;; * ) echo 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"' @@ -94,7 +101,7 @@ if [[ ":${PATH}:" != *:"${RBENV_ROOT}/shims":* ]]; then fi completion="${root}/completions/rbenv.${shell}" -[ -r "$completion" ] && echo "source '$completion'" +[ -r "$completion" ] && echo ". '$completion'" if [ -z "$no_rehash" ]; then echo 'rbenv rehash 2>/dev/null' @@ -106,9 +113,7 @@ fish ) cat </dev/null || true" + or='||' ;; esac + +echo "hash -r 2>/dev/null $or true" diff --git a/test/init.bats b/test/init.bats index a4c63199..d0795eef 100644 --- a/test/init.bats +++ b/test/init.bats @@ -21,14 +21,14 @@ load test_helper root="$(cd $BATS_TEST_DIRNAME/.. && pwd)" SHELL=/bin/bash run rbenv-init - assert_success - assert_line "source '${root}/libexec/../completions/rbenv.bash'" + assert_line ". '${root}/libexec/../completions/rbenv.bash'" } @test "setup shell completions (fish)" { root="$(cd $BATS_TEST_DIRNAME/.. && pwd)" SHELL=/usr/bin/fish run rbenv-init - assert_success - assert_line '. "'${root}'/libexec/../completions/rbenv.fish";' + assert_line ". '${root}/libexec/../completions/rbenv.fish'" } @test "option to skip rehash" { @@ -48,7 +48,7 @@ load test_helper export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin" SHELL=/usr/bin/fish run rbenv-init - assert_success - assert_line 0 'setenv PATH "'${RBENV_ROOT}'/shims" $PATH ;' + assert_line 0 "setenv PATH '${RBENV_ROOT}/shims' \$PATH" } @test "doesn't add shims to PATH more than once" { diff --git a/test/rehash.bats b/test/rehash.bats index 97414410..97f6cd43 100755 --- a/test/rehash.bats +++ b/test/rehash.bats @@ -98,3 +98,17 @@ SH assert_success assert_output "HELLO=:hello:ugly:world:again" } + +@test "sh-rehash in bash" { + create_executable "2.0" "ruby" + SHELL=/bin/bash run rbenv-sh-rehash + assert_success "hash -r 2>/dev/null || true" + assert [ -x "${RBENV_ROOT}/shims/ruby" ] +} + +@test "sh-rehash in fish" { + create_executable "2.0" "ruby" + SHELL=/usr/bin/fish run rbenv-sh-rehash + assert_success "hash -r 2>/dev/null ; or true" + assert [ -x "${RBENV_ROOT}/shims/ruby" ] +} From 5ae2cac088d12fea01054e1f3abbf304b92920b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Sat, 28 Sep 2013 15:58:13 +0200 Subject: [PATCH 3/3] fix `rbenv()` function in ksh and dash ksh syntax becomes: function rbenv { typeset command `typeset` only declares a local variable if there's an explicit `function` declaration; otherwise the variable leaks. Other shells use this syntax: rbenv() { local command This is for dash compatibility, which supports neither `function` nor `typeset`. references #205, fixes #408 --- libexec/rbenv-init | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/libexec/rbenv-init b/libexec/rbenv-init index c240460d..1a2762c7 100755 --- a/libexec/rbenv-init +++ b/libexec/rbenv-init @@ -122,13 +122,25 @@ function rbenv command rbenv "\$command" \$argv end end +EOS + exit 0 + ;; +ksh ) + cat <