Skip to content

Commit

Permalink
rbenv init: modify shell config files instead of printing instructions (
Browse files Browse the repository at this point in the history
#1568)

When running `rbenv init`, typically during rbenv setup, users expected
their shell environment to be modified permanently. Instead, what the
command would do is print the instructions to the user and expect them
to edit their shell initialization files accordingly. This proved to
be unintuitive.

Now, running `rbenv init <shells>...` will modify the shell initialization
files of the following shells:

- bash: `~/.bash_profile` or `~/.bashrc` if the latter exists but the former does not
- zsh: `~/.zprofile` or `~/.zshrc` if the latter exists and mentions "rbenv"
- fish: `~/.config/fish/config.fish`

If no shells were specified on the command line, rbenv will try to detect
the current shell.

It should be safe to run `rbenv init` multiple times, as the command will
avoid modifying any shell startup file that already mentions "rbenv init".
  • Loading branch information
mislav committed May 3, 2024
1 parent a3b98a4 commit c3ba994
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 82 deletions.
33 changes: 7 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ On systems with Homebrew package manager, the “Using Package Managers” metho
rbenv and you can install it from the AUR using the instructions from this
[wiki page](https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_and_upgrading_packages).

2. Learn how to load rbenv in your shell.
2. Set up your shell to load rbenv.

```sh
# run this and follow the printed instructions:
rbenv init
```

Expand All @@ -75,29 +74,11 @@ This will get you going with the latest version of rbenv without needing a syste
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
```

2. Configure your shell to load rbenv:

* For **bash**:

_Ubuntu Desktop_ users should configure `~/.bashrc`:
```bash
echo 'eval "$(~/.rbenv/bin/rbenv init - bash)"' >> ~/.bashrc
```

On _other platforms_, bash is usually configured via `~/.bash_profile`:
```bash
echo 'eval "$(~/.rbenv/bin/rbenv init - bash)"' >> ~/.bash_profile
```
* For **Zsh**:
```zsh
echo 'eval "$(~/.rbenv/bin/rbenv init - zsh)"' >> ~/.zshrc
```

* For **Fish shell**:
```fish
echo 'status --is-interactive; and ~/.rbenv/bin/rbenv init - fish | source' >> ~/.config/fish/config.fish
```
2. Set up your shell to load rbenv.

```sh
~/.rbenv/bin/rbenv init
```

If you are curious, see here to [understand what `init` does](#how-rbenv-hooks-into-your-shell).

Expand Down Expand Up @@ -298,7 +279,7 @@ name | default | description

### How rbenv hooks into your shell

`rbenv init` is a helper command to bootstrap rbenv into a shell. This helper is part of the recommended installation instructions, but optional, as an advanced user can set up the following tasks manually. Here is what the command does when its output is `eval`'d:
`rbenv init` is a helper command to bootstrap rbenv into a shell. This helper is part of the recommended installation instructions, but optional, as an advanced user can set up the following tasks manually. Here is what the command does when its output is `eval`'d by a shell during its startup:

0. Adds `rbenv` executable to PATH if necessary.

Expand Down
144 changes: 92 additions & 52 deletions libexec/rbenv-init
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
#!/usr/bin/env bash
# Summary: Configure the shell environment for rbenv
# Usage: eval "$(rbenv init - [--no-rehash] [<shell>])"
# Usage: rbenv init [--no-rehash] [<shells>...]
# rbenv init - [--no-rehash] [<shell>]
#
# Modifies shell initialization files to bootstrap rbenv functionality.
# Typically, this will add a line that eval's the output of `rbenv init -`.
# If no shells are named by arguments, the current shell will be detected
# by inspecting the parent process. If a shell is already configured for
# rbenv, the init command does nothing and exits with zero status.
#
# In the `rbenv init -` mode, this outputs a script to be eval'd in the
# current shell. Most importantly, that script prepends the rbenv shims
# directory to the PATH environment variable. To aid interactive shells,
# the script also installs the magic `rbenv()` shell function and loads
# shell completions for rbenv commands.

set -e
[ -n "$RBENV_DEBUG" ] && set -x
Expand All @@ -18,27 +31,29 @@ fi

print=""
no_rehash=""
for args in "$@"
do
if [ "$args" = "-" ]; then
shells=()
while [ $# -gt 0 ]; do
case "$1" in
"-" )
print=1
shift
fi

if [ "$args" = "--no-rehash" ]; then
;;
"--no-rehash" )
no_rehash=1
shift
fi
;;
* )
shells+=("$1")
;;
esac
shift
done

shell="$1"
if [ -z "$shell" ]; then
if [ "${#shells[@]}" -eq 0 ]; then
shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)"
shell="${shell%% *}"
shell="${shell##-}"
shell="${shell:-$SHELL}"
shell="${shell##*/}"
shell="${shell%%-*}"
shells=("${shell%%-*}")
fi

root="${BASH_SOURCE:-$0}"
Expand All @@ -51,62 +66,87 @@ if [ -n "$RBENV_ORIG_PATH" ]; then
fi

if [ -z "$print" ]; then
case "$shell" in
bash )
if [ -f "${HOME}/.bashrc" ] && [ ! -f "${HOME}/.bash_profile" ]; then
profile='~/.bashrc'
display_path() {
if [ "${1/#$HOME\/}" != "$1" ]; then
# shellcheck disable=SC2088
printf '~/%s' "${1/#$HOME\/}"
else
profile='~/.bash_profile'
printf '%s' "$1"
fi
;;
zsh )
profile='~/.zshrc'
;;
ksh | ksh93 | mksh )
# There are two implementations of Korn shell: AT&T (ksh93) and Mir (mksh).
# Systems may have them installed under those names, or as ksh, so those
# are recognized here. The obsolete ksh88 (subsumed by ksh93) and pdksh
# (subsumed by mksh) are not included, since they are unlikely to still
# be in use as interactive shells anywhere.
profile='~/.profile'
;;
fish )
profile='~/.config/fish/config.fish'
;;
* )
profile='your profile'
;;
esac
}

rbenv_command=rbenv
if [ -z "$rbenv_in_path" ]; then
rbenv_command="$root/bin/rbenv"
rbenv_command="${rbenv_command/$HOME\//~/}"
rbenv_command="$(display_path "$root/bin/rbenv")"
fi

color_start=""
color_end=""
if [ -t 1 ]; then
color_start=$'\e[33;1m'
color_end=$'\e[m'
fi

{ echo "# Please add the following line to your \`${profile}' file,"
echo "# then restart your terminal."
echo
[ -t 2 ] && printf '\e[33;1m'
write_config() {
if grep -q "rbenv init" "$1" 2>/dev/null; then
printf 'skipping %s%s%s: already configured for rbenv.\n' "$color_start" "$(display_path "$1")" "$color_end"
return 0
fi
mkdir -p "${1%/*}"
# shellcheck disable=SC2016
printf '\n# Added by `rbenv init` on %s\n%s\n' "$(date)" "$2" >> "$1"
printf 'writing %s%s%s: now configured for rbenv.\n' "$color_start" "$(display_path "$1")" "$color_end"
}

status=0
for shell in "${shells[@]}"; do
case "$shell" in
bash )
if [ -f ~/.bashrc ] && [ ! -f ~/.bash_profile ]; then
profile="$HOME/.bashrc"
else
# shellcheck disable=SC2012
profile="$(ls ~/.bash_profile ~/.bash_login ~/.profile 2>/dev/null | head -n1)"
[ -n "$profile" ] || profile="$HOME/.bash_profile"
fi
write_config "$profile" \
"eval \"\$($rbenv_command init -${no_rehash:+ --no-rehash} bash)\""
;;
zsh )
# check zshrc for backward compatibility with older rbenv init
if grep -q rbenv "${ZDOTDIR:-$HOME}/.zshrc" 2>/dev/null; then
profile="${ZDOTDIR:-$HOME}/.zshrc"
else
profile="${ZDOTDIR:-$HOME}/.zprofile"
fi
write_config "$profile" \
"eval \"\$($rbenv_command init -${no_rehash:+ --no-rehash} zsh)\""
;;
ksh | ksh93 | mksh )
# There are two implementations of Korn shell: AT&T (ksh93) and Mir (mksh).
# Systems may have them installed under those names, or as ksh, so those
# are recognized here. The obsolete ksh88 (subsumed by ksh93) and pdksh
# (subsumed by mksh) are not included, since they are unlikely to still
# be in use as interactive shells anywhere.
write_config "$HOME/.profile" \
"eval \"\$($rbenv_command init - ksh)\""
;;
fish )
printf 'status --is-interactive; and %s init - fish | source' "$rbenv_command"
write_config "${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish" \
"status --is-interactive; and $rbenv_command init -${no_rehash:+ --no-rehash} fish | source"
;;
* )
# shellcheck disable=SC2016
printf 'eval "$(%s init - %s)"' "$rbenv_command" "$shell"
printf 'unsupported shell: "%s"\n' "$shell" >&2
status=1
;;
esac
[ -t 2 ] && printf '\e[m'
echo
echo
} >&2

exit 1
done
exit $status
fi

mkdir -p "${RBENV_ROOT}/"{shims,versions}

shell="${shells[0]}"
case "$shell" in
fish )
[ -n "$rbenv_in_path" ] || printf "set -gx PATH '%s/bin' \$PATH\n" "$root"
Expand Down
57 changes: 53 additions & 4 deletions test/init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,67 @@ OUT
[ -z "$line" ] || flunk "did not expect line: $line"
}

@test "posix shell instructions" {
@test "set up bash" {
assert [ ! -e ~/.bash_profile ]
run rbenv-init bash
assert [ "$status" -eq 1 ]
assert_success "writing ~/.bash_profile: now configured for rbenv."
run cat ~/.bash_profile
# shellcheck disable=SC2016
assert_line 'eval "$(rbenv init - bash)"'
}

@test "fish instructions" {
@test "set up bash (bashrc)" {
mkdir -p "$HOME"
touch ~/.bashrc
assert [ ! -e ~/.bash_profile ]
run rbenv-init bash
assert_success "writing ~/.bashrc: now configured for rbenv."
run cat ~/.bashrc
# shellcheck disable=SC2016
assert_line 'eval "$(rbenv init - bash)"'
}

@test "set up zsh" {
unset ZDOTDIR
assert [ ! -e ~/.zprofile ]
run rbenv-init zsh
assert_success "writing ~/.zprofile: now configured for rbenv."
run cat ~/.zprofile
# shellcheck disable=SC2016
assert_line 'eval "$(rbenv init - zsh)"'
}

@test "set up zsh (zshrc)" {
unset ZDOTDIR
mkdir -p "$HOME"
cat > ~/.zshrc <<<"# rbenv"
run rbenv-init zsh
assert_success "writing ~/.zshrc: now configured for rbenv."
run cat ~/.zshrc
# shellcheck disable=SC2016
assert_line 'eval "$(rbenv init - zsh)"'
}

@test "set up fish" {
unset XDG_CONFIG_HOME
run rbenv-init fish
assert [ "$status" -eq 1 ]
assert_success "writing ~/.config/fish/config.fish: now configured for rbenv."
run cat ~/.config/fish/config.fish
assert_line 'status --is-interactive; and rbenv init - fish | source'
}

@test "set up multiple shells at once" {
unset ZDOTDIR
unset XDG_CONFIG_HOME
run rbenv-init bash zsh fish
assert_success
assert_output <<OUT
writing ~/.bash_profile: now configured for rbenv.
writing ~/.zprofile: now configured for rbenv.
writing ~/.config/fish/config.fish: now configured for rbenv.
OUT
}

@test "option to skip rehash" {
run rbenv-init - --no-rehash
assert_success
Expand Down

0 comments on commit c3ba994

Please sign in to comment.