From 6a912bf104a1585978d1f79d39642d042853277f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 12 Jan 2016 11:03:50 -0800 Subject: [PATCH 01/13] add support for rbenv shell - `rbenv shell -` allows you to switch to the previously activated ruby version. Similar to `cd -` or `git checkout -`. This tries to implement `rbenv shell -` as proposed in #854. However, adding support seemed to break the "shell change version" test. I'm not very good at Bash programming, can someone tell me what is wrong with what I'm doing? I'd like to add a bit more functionality to this, but I'm really just cargo cult programming Bash. Thank you! fix tests --- libexec/rbenv-sh-shell | 41 ++++++++++++++++++++++++------ test/shell.bats | 57 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 87 insertions(+), 11 deletions(-) diff --git a/libexec/rbenv-sh-shell b/libexec/rbenv-sh-shell index f4e0098f..9c885e93 100755 --- a/libexec/rbenv-sh-shell +++ b/libexec/rbenv-sh-shell @@ -39,25 +39,50 @@ fi if [ "$version" = "--unset" ]; then case "$shell" in fish ) + echo "set -e OLD_RBENV_VERSION" echo "set -e RBENV_VERSION" ;; * ) + echo "unset OLD_RBENV_VERSION" echo "unset RBENV_VERSION" ;; esac exit fi +if [ "$version" = "-" ]; then + if [ -z "$OLD_RBENV_VERSION" ]; then + echo "rbenv: OLD_RBENV_VERSION not set" >&2 + exit 1; + fi + case "$shell" in + fish ) + rbenv_version=$RBENV_VERSION + echo "set -e OLD_RBENV_VERSION \"$rbenv_version\"" + echo "set -e RBENV_VERSION \"$OLD_RBENV_VERSION\"" + ;; + * ) + rbenv_version=$RBENV_VERSION + echo "export OLD_RBENV_VERSION=\"$rbenv_version\"" + echo "export RBENV_VERSION=\"$OLD_RBENV_VERSION\"" + ;; + esac + exit +fi + # Make sure the specified version is installed. if rbenv-prefix "$version" >/dev/null; then - case "$shell" in - fish ) - echo "setenv RBENV_VERSION \"${version}\"" - ;; - * ) - echo "export RBENV_VERSION=\"${version}\"" - ;; - esac + if [ "$version" != "$RBENV_VERSION" ]; then + case "$shell" in + fish ) + echo "setenv RBENV_VERSION \"${version}\"" + ;; + * ) + echo "export OLD_RBENV_VERSION=\"$RBENV_VERSION\"" + echo "export RBENV_VERSION=\"${version}\"" + ;; + esac + fi else echo "false" exit 1 diff --git a/test/shell.bats b/test/shell.bats index 0f776745..3c7313c1 100644 --- a/test/shell.bats +++ b/test/shell.bats @@ -22,12 +22,18 @@ load test_helper @test "shell unset" { RBENV_SHELL=bash run rbenv-sh-shell --unset - assert_success "unset RBENV_VERSION" + assert_output < Date: Wed, 17 Feb 2016 15:48:00 -0500 Subject: [PATCH 02/13] Fish shell "." is deprecated in favor of "source" Per [the fish documentation for "source"](file:///usr/local/Cellar/fish/2.2.0/share/doc/fish/commands.html#source) - ". (a single period) is an alias for the source command. The use of . is deprecated in favour of source, and . will be removed in a future version of fish." --- libexec/rbenv-init | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/rbenv-init b/libexec/rbenv-init index 7ddae5e5..82df3229 100755 --- a/libexec/rbenv-init +++ b/libexec/rbenv-init @@ -70,7 +70,7 @@ if [ -z "$print" ]; then echo case "$shell" in fish ) - echo 'status --is-interactive; and . (rbenv init -|psub)' + echo 'status --is-interactive; and source (rbenv init -|psub)' ;; * ) echo 'eval "$(rbenv init -)"' From 8b0b51a166d6bcbe83fd467a70ec8919645aabfa Mon Sep 17 00:00:00 2001 From: Donald Merand Date: Sat, 20 Feb 2016 10:27:39 -0500 Subject: [PATCH 03/13] convert references to '.' to 'source' for fish shell --- libexec/rbenv-init | 9 +++------ test/init.bats | 4 ++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/libexec/rbenv-init b/libexec/rbenv-init index 7ddae5e5..ac9d4cf0 100755 --- a/libexec/rbenv-init +++ b/libexec/rbenv-init @@ -70,7 +70,7 @@ if [ -z "$print" ]; then echo case "$shell" in fish ) - echo 'status --is-interactive; and . (rbenv init -|psub)' + echo 'status --is-interactive; and source (rbenv init -|psub)' ;; * ) echo 'eval "$(rbenv init -)"' @@ -97,10 +97,7 @@ esac completion="${root}/completions/rbenv.${shell}" if [ -r "$completion" ]; then - case "$shell" in - fish ) echo ". '$completion'" ;; - * ) echo "source '$completion'" ;; - esac + echo "source '$completion'" fi if [ -z "$no_rehash" ]; then @@ -117,7 +114,7 @@ function rbenv switch "\$command" case ${commands[*]} - . (rbenv "sh-\$command" \$argv|psub) + source (rbenv "sh-\$command" \$argv|psub) case '*' command rbenv "\$command" \$argv end diff --git a/test/init.bats b/test/init.bats index d7005489..95be301c 100644 --- a/test/init.bats +++ b/test/init.bats @@ -47,13 +47,13 @@ OUT root="$(cd $BATS_TEST_DIRNAME/.. && pwd)" run rbenv-init - fish assert_success - assert_line ". '${root}/test/../libexec/../completions/rbenv.fish'" + assert_line "source '${root}/test/../libexec/../completions/rbenv.fish'" } @test "fish instructions" { run rbenv-init fish assert [ "$status" -eq 1 ] - assert_line 'status --is-interactive; and . (rbenv init -|psub)' + assert_line 'status --is-interactive; and source (rbenv init -|psub)' } @test "option to skip rehash" { From 7860ad5268753f005c7ecc121d186fd16370f8d9 Mon Sep 17 00:00:00 2001 From: "Yamashita, Yuu" Date: Fri, 1 Jul 2016 00:08:19 +0000 Subject: [PATCH 04/13] Help message should not be written to stdout for `sh-` commands (yyuu/pyenv#650) --- libexec/rbenv | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libexec/rbenv b/libexec/rbenv index d77df669..b0b0e91b 100755 --- a/libexec/rbenv +++ b/libexec/rbenv @@ -109,7 +109,11 @@ case "$command" in shift 1 if [ "$1" = --help ]; then - exec rbenv-help "$command" + if [[ "$command" == "sh-"* ]]; then + exec rbenv-help "$command" 1>&2 + else + exec rbenv-help "$command" + fi else exec "$command_path" "$@" fi From 26ac59fd1da0a28695697a03bd575810bc962d90 Mon Sep 17 00:00:00 2001 From: "Yamashita, Yuu" Date: Mon, 4 Jul 2016 00:56:08 +0000 Subject: [PATCH 05/13] Write help message to stdout --- libexec/rbenv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/rbenv b/libexec/rbenv index b0b0e91b..07847a66 100755 --- a/libexec/rbenv +++ b/libexec/rbenv @@ -110,7 +110,7 @@ case "$command" in shift 1 if [ "$1" = --help ]; then if [[ "$command" == "sh-"* ]]; then - exec rbenv-help "$command" 1>&2 + echo "rbenv help \"$command\"" else exec rbenv-help "$command" fi From 99342d642f3ef018c2d71e7bf54593263e2c015f Mon Sep 17 00:00:00 2001 From: Jose Luis Duran Date: Mon, 8 Aug 2016 17:24:07 -0300 Subject: [PATCH 06/13] src/configure: Add FreeBSD --- src/configure | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/configure b/src/configure index 86e25ac1..10d0ff57 100755 --- a/src/configure +++ b/src/configure @@ -21,6 +21,9 @@ case "$(uname -s)" in Darwin* ) host_os="darwin$(uname -r)" ;; +FreeBSD* ) + host_os="freebsd$(uname -r)" + ;; OpenBSD* ) host_os="openbsd$(uname -r)" ;; From d0779fc8fb90fb7268d0ee858086e92bcd316787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 9 Sep 2016 10:29:12 +0200 Subject: [PATCH 07/13] Expand literal tilde in PATH The literal tilde in a PATH entry (e.g. `~/.rbenv/shims`) doesn't seem to be supported by system `which` utility, but *does* seem to be supported by `command -v` (used in `rbenv-which`) and `type -p`. Therefore, we must strip away `~/.rbenv/shims` from PATH when looking up executables for system Ruby, lest we risk infinite loop. We do so by substituting any occurence of `~` in PATH with the value of `HOME`. --- libexec/rbenv-which | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/rbenv-which b/libexec/rbenv-which index c7c0301a..78db1c05 100755 --- a/libexec/rbenv-which +++ b/libexec/rbenv-which @@ -18,7 +18,7 @@ fi remove_from_path() { local path_to_remove="$1" local path_before - local result=":$PATH:" + local result=":${PATH//\~/$HOME}:" while [ "$path_before" != "$result" ]; do path_before="$result" result="${result//:$path_to_remove:/:}" From c4d97ad3927c2670d293ac8910ff2bbcd05a06c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 25 Nov 2016 20:24:16 +0100 Subject: [PATCH 08/13] Finalize `rbenv shell -` implementation This ensures that OLD_RBENV_VERSION is never exported. This makes the implementation a little bit more complex, since more logic needs to be pushed down into eval'd code. --- libexec/rbenv-sh-shell | 61 +++++++++++++++++++++++++++---------- test/shell.bats | 69 ++++++++++++++---------------------------- 2 files changed, 68 insertions(+), 62 deletions(-) diff --git a/libexec/rbenv-sh-shell b/libexec/rbenv-sh-shell index 9c885e93..04f81866 100755 --- a/libexec/rbenv-sh-shell +++ b/libexec/rbenv-sh-shell @@ -3,6 +3,7 @@ # Summary: Set or show the shell-specific Ruby version # # Usage: rbenv shell +# rbenv shell - # rbenv shell --unset # # Sets a shell-specific Ruby version by setting the `RBENV_VERSION' @@ -12,6 +13,11 @@ # should be a string matching a Ruby version known to rbenv. # The special version string `system' will use your default system Ruby. # Run `rbenv versions' for a list of available Ruby versions. +# +# When `-` is passed instead of the version string, the previously set +# version will be restored. With `--unset`, the `RBENV_VERSION` +# environment variable gets unset, restoring the environment to the +# state before the first `rbenv shell` call. set -e [ -n "$RBENV_DEBUG" ] && set -x @@ -31,7 +37,7 @@ if [ -z "$version" ]; then echo "rbenv: no shell-specific version configured" >&2 exit 1 else - echo "echo \"\$RBENV_VERSION\"" + echo 'echo "$RBENV_VERSION"' exit fi fi @@ -39,11 +45,11 @@ fi if [ "$version" = "--unset" ]; then case "$shell" in fish ) - echo "set -e OLD_RBENV_VERSION" + echo 'set -gu OLD_RBENV_VERSION "$RBENV_VERSION"' echo "set -e RBENV_VERSION" ;; * ) - echo "unset OLD_RBENV_VERSION" + echo 'OLD_RBENV_VERSION="$RBENV_VERSION"' echo "unset RBENV_VERSION" ;; esac @@ -51,20 +57,42 @@ if [ "$version" = "--unset" ]; then fi if [ "$version" = "-" ]; then - if [ -z "$OLD_RBENV_VERSION" ]; then - echo "rbenv: OLD_RBENV_VERSION not set" >&2 - exit 1; - fi case "$shell" in fish ) - rbenv_version=$RBENV_VERSION - echo "set -e OLD_RBENV_VERSION \"$rbenv_version\"" - echo "set -e RBENV_VERSION \"$OLD_RBENV_VERSION\"" + cat <&2 + false +end +EOS ;; * ) - rbenv_version=$RBENV_VERSION - echo "export OLD_RBENV_VERSION=\"$rbenv_version\"" - echo "export RBENV_VERSION=\"$OLD_RBENV_VERSION\"" + cat <&2 + false +fi +EOS ;; esac exit @@ -75,11 +103,12 @@ if rbenv-prefix "$version" >/dev/null; then if [ "$version" != "$RBENV_VERSION" ]; then case "$shell" in fish ) - echo "setenv RBENV_VERSION \"${version}\"" + echo 'set -gu OLD_RBENV_VERSION "$RBENV_VERSION"' + echo "set -gx RBENV_VERSION \"$version\"" ;; * ) - echo "export OLD_RBENV_VERSION=\"$RBENV_VERSION\"" - echo "export RBENV_VERSION=\"${version}\"" + echo 'OLD_RBENV_VERSION="$RBENV_VERSION"' + echo "export RBENV_VERSION=\"$version\"" ;; esac fi diff --git a/test/shell.bats b/test/shell.bats index 3c7313c1..a3c16b66 100644 --- a/test/shell.bats +++ b/test/shell.bats @@ -20,18 +20,32 @@ load test_helper assert_success 'echo "$RBENV_VERSION"' } +@test "shell revert" { + RBENV_SHELL=bash run rbenv-sh-shell - + assert_success + assert_line 0 'if [ -n "${OLD_RBENV_VERSION+x}" ]; then' +} + +@test "shell revert (fish)" { + RBENV_SHELL=fish run rbenv-sh-shell - + assert_success + assert_line 0 'if set -q OLD_RBENV_VERSION' +} + @test "shell unset" { RBENV_SHELL=bash run rbenv-sh-shell --unset + assert_success assert_output < Date: Fri, 25 Nov 2016 21:11:08 +0100 Subject: [PATCH 09/13] rbenv 1.1.0 --- libexec/rbenv---version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/rbenv---version b/libexec/rbenv---version index b10e9a30..e994f028 100755 --- a/libexec/rbenv---version +++ b/libexec/rbenv---version @@ -12,7 +12,7 @@ set -e [ -n "$RBENV_DEBUG" ] && set -x -version="1.0.0" +version="1.1.0" git_revision="" if cd "${BASH_SOURCE%/*}" 2>/dev/null && git remote -v 2>/dev/null | grep -q rbenv; then From 8eb97549e159d7e30950963324962ef145ab5cf2 Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Mon, 19 Dec 2016 17:21:45 -0500 Subject: [PATCH 10/13] Conforms OLD_RBENV_VERSION to RBENV_* convention Keeping rbenv-controlled variables to RBENV_* "namespace" helps with discoverability (and tools like rbenv-env) but also consistency and a very minor degree of safety/isolation from env impact. --- libexec/rbenv-sh-shell | 40 ++++++++++++++++++++-------------------- test/shell.bats | 12 ++++++------ 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/libexec/rbenv-sh-shell b/libexec/rbenv-sh-shell index 04f81866..8c71185c 100755 --- a/libexec/rbenv-sh-shell +++ b/libexec/rbenv-sh-shell @@ -45,11 +45,11 @@ fi if [ "$version" = "--unset" ]; then case "$shell" in fish ) - echo 'set -gu OLD_RBENV_VERSION "$RBENV_VERSION"' + echo 'set -gu RBENV_VERSION_OLD "$RBENV_VERSION"' echo "set -e RBENV_VERSION" ;; * ) - echo 'OLD_RBENV_VERSION="$RBENV_VERSION"' + echo 'RBENV_VERSION_OLD="$RBENV_VERSION"' echo "unset RBENV_VERSION" ;; esac @@ -60,36 +60,36 @@ if [ "$version" = "-" ]; then case "$shell" in fish ) cat <&2 + echo "rbenv: RBENV_VERSION_OLD is not set" >&2 false end EOS ;; * ) cat <&2 + echo "rbenv: RBENV_VERSION_OLD is not set" >&2 false fi EOS @@ -103,11 +103,11 @@ if rbenv-prefix "$version" >/dev/null; then if [ "$version" != "$RBENV_VERSION" ]; then case "$shell" in fish ) - echo 'set -gu OLD_RBENV_VERSION "$RBENV_VERSION"' + echo 'set -gu RBENV_VERSION_OLD "$RBENV_VERSION"' echo "set -gx RBENV_VERSION \"$version\"" ;; * ) - echo 'OLD_RBENV_VERSION="$RBENV_VERSION"' + echo 'RBENV_VERSION_OLD="$RBENV_VERSION"' echo "export RBENV_VERSION=\"$version\"" ;; esac diff --git a/test/shell.bats b/test/shell.bats index a3c16b66..9d6ae14b 100644 --- a/test/shell.bats +++ b/test/shell.bats @@ -23,20 +23,20 @@ load test_helper @test "shell revert" { RBENV_SHELL=bash run rbenv-sh-shell - assert_success - assert_line 0 'if [ -n "${OLD_RBENV_VERSION+x}" ]; then' + assert_line 0 'if [ -n "${RBENV_VERSION_OLD+x}" ]; then' } @test "shell revert (fish)" { RBENV_SHELL=fish run rbenv-sh-shell - assert_success - assert_line 0 'if set -q OLD_RBENV_VERSION' + assert_line 0 'if set -q RBENV_VERSION_OLD' } @test "shell unset" { RBENV_SHELL=bash run rbenv-sh-shell --unset assert_success assert_output < Date: Thu, 6 Apr 2017 23:55:48 -0700 Subject: [PATCH 11/13] Prefer 'set' over 'setenv' for fish shell The setenv function in fish shell has changed dramatically in https://github.com/fish-shell/fish-shell/commit/75600b6b538f242a6b340e51965f25cfd4473c4a It now conforms to the csh version, which takes at most two arguments. In this init script, the form setenv PATH prepend_something $PATH had been used, which had too many arguments. Since setenv isn't a native command in fish, a suitable replacement is to use the "set -gx" command, which can consume multiple arguments. --- libexec/rbenv-init | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libexec/rbenv-init b/libexec/rbenv-init index ac9d4cf0..e8ae8d5a 100755 --- a/libexec/rbenv-init +++ b/libexec/rbenv-init @@ -86,8 +86,8 @@ mkdir -p "${RBENV_ROOT}/"{shims,versions} case "$shell" in fish ) - echo "setenv PATH '${RBENV_ROOT}/shims' \$PATH" - echo "setenv RBENV_SHELL $shell" + echo 'set -gx PATH '${RBENV_ROOT}'/shims $PATH' + echo "set -gx RBENV_SHELL $shell" ;; * ) echo 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"' From 4e271134943c17ce6627c00a0131375981d911b9 Mon Sep 17 00:00:00 2001 From: jacob-on-github Date: Tue, 16 May 2017 14:29:46 -0500 Subject: [PATCH 12/13] Update README.md I work on a team that has followed the Homebrew installations. More than once we've missed the `rbenv init` instruction and it has caused headaches down the road. This formatting makes it harder to miss. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6c624b69..662d4a46 100644 --- a/README.md +++ b/README.md @@ -230,10 +230,10 @@ manager on Mac OS X: ~~~ $ brew update $ brew install rbenv +$ rbenv init ~~~ -Afterwards you'll still need to run `rbenv init` for instructions -as stated in the caveats. You'll only ever have to do this once. +You'll only ever have to run `rbenv init` once. ### How rbenv hooks into your shell From a81da8d864c46c82b8d4ddc8da778f2cdf4274dd Mon Sep 17 00:00:00 2001 From: Jeff Kowalski Date: Tue, 16 May 2017 13:36:06 -0700 Subject: [PATCH 13/13] Revert quoting change in previous commit; adjust test to match code Revert back to original quoting style used before previous commit. Adjust init.bats to reflect changes for successful tests. --- libexec/rbenv-init | 2 +- test/init.bats | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) mode change 100644 => 100755 test/init.bats diff --git a/libexec/rbenv-init b/libexec/rbenv-init index e8ae8d5a..1661a052 100755 --- a/libexec/rbenv-init +++ b/libexec/rbenv-init @@ -86,7 +86,7 @@ mkdir -p "${RBENV_ROOT}/"{shims,versions} case "$shell" in fish ) - echo 'set -gx PATH '${RBENV_ROOT}'/shims $PATH' + echo "set -gx PATH '${RBENV_ROOT}/shims' \$PATH" echo "set -gx RBENV_SHELL $shell" ;; * ) diff --git a/test/init.bats b/test/init.bats old mode 100644 new mode 100755 index 95be301c..6cf64e8b --- a/test/init.bats +++ b/test/init.bats @@ -73,7 +73,7 @@ OUT export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin:/usr/local/bin" run rbenv-init - fish assert_success - assert_line 0 "setenv PATH '${RBENV_ROOT}/shims' \$PATH" + assert_line 0 "set -gx PATH '${RBENV_ROOT}/shims' \$PATH" } @test "can add shims to PATH more than once" { @@ -87,7 +87,7 @@ OUT export PATH="${RBENV_ROOT}/shims:$PATH" run rbenv-init - fish assert_success - assert_line 0 "setenv PATH '${RBENV_ROOT}/shims' \$PATH" + assert_line 0 "set -gx PATH '${RBENV_ROOT}/shims' \$PATH" } @test "outputs sh-compatible syntax" {