2011-08-12 11:33:45 +02:00
|
|
|
#!/usr/bin/env bash
|
2012-12-30 05:05:04 +01:00
|
|
|
# Summary: Configure the shell environment for rbenv
|
2024-07-05 14:44:17 +02:00
|
|
|
# Usage: rbenv init [<shells>...]
|
2024-05-03 16:59:39 +02:00
|
|
|
# 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.
|
2012-12-29 23:34:53 +01:00
|
|
|
|
2011-08-12 11:33:45 +02:00
|
|
|
set -e
|
2011-09-12 17:11:59 +02:00
|
|
|
[ -n "$RBENV_DEBUG" ] && set -x
|
2011-08-04 06:16:28 +02:00
|
|
|
|
2015-11-20 15:20:01 +01:00
|
|
|
# Provide rbenv completions
|
|
|
|
if [ "$1" = "--complete" ]; then
|
|
|
|
echo -
|
|
|
|
echo --no-rehash
|
|
|
|
echo bash
|
|
|
|
echo fish
|
|
|
|
echo ksh
|
|
|
|
echo zsh
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2011-08-04 07:45:40 +02:00
|
|
|
print=""
|
2011-12-26 02:59:24 +01:00
|
|
|
no_rehash=""
|
2024-05-03 16:59:39 +02:00
|
|
|
shells=()
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
case "$1" in
|
|
|
|
"-" )
|
2012-01-17 15:50:40 +01:00
|
|
|
print=1
|
2024-05-03 16:59:39 +02:00
|
|
|
;;
|
|
|
|
"--no-rehash" )
|
2012-01-17 15:50:40 +01:00
|
|
|
no_rehash=1
|
2024-05-03 16:59:39 +02:00
|
|
|
;;
|
|
|
|
* )
|
|
|
|
shells+=("$1")
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
2012-01-17 15:50:40 +01:00
|
|
|
done
|
2011-12-26 02:59:24 +01:00
|
|
|
|
2024-05-03 16:59:39 +02:00
|
|
|
if [ "${#shells[@]}" -eq 0 ]; then
|
2015-05-10 16:03:06 +02:00
|
|
|
shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)"
|
2014-01-02 22:36:03 +01:00
|
|
|
shell="${shell%% *}"
|
2015-12-24 13:23:01 +01:00
|
|
|
shell="${shell##-}"
|
2015-05-10 16:17:35 +02:00
|
|
|
shell="${shell:-$SHELL}"
|
|
|
|
shell="${shell##*/}"
|
2024-05-03 16:59:39 +02:00
|
|
|
shells=("${shell%%-*}")
|
2011-08-04 06:16:28 +02:00
|
|
|
fi
|
|
|
|
|
2022-09-24 23:59:13 +02:00
|
|
|
root="${BASH_SOURCE:-$0}"
|
|
|
|
root="${root%/*}"
|
|
|
|
root="${root%/*}"
|
2011-08-04 06:16:28 +02:00
|
|
|
|
2022-09-25 22:32:46 +02:00
|
|
|
rbenv_in_path=true
|
|
|
|
if [ -n "$RBENV_ORIG_PATH" ]; then
|
2022-10-07 16:34:38 +02:00
|
|
|
PATH="$RBENV_ORIG_PATH" type -P rbenv >/dev/null || rbenv_in_path=""
|
2022-09-25 22:32:46 +02:00
|
|
|
fi
|
|
|
|
|
2011-08-04 07:45:40 +02:00
|
|
|
if [ -z "$print" ]; then
|
2024-05-03 16:59:39 +02:00
|
|
|
display_path() {
|
|
|
|
if [ "${1/#$HOME\/}" != "$1" ]; then
|
|
|
|
# shellcheck disable=SC2088
|
|
|
|
printf '~/%s' "${1/#$HOME\/}"
|
2015-10-26 15:45:52 +01:00
|
|
|
else
|
2024-05-03 16:59:39 +02:00
|
|
|
printf '%s' "$1"
|
2015-10-26 15:45:52 +01:00
|
|
|
fi
|
2024-05-03 16:59:39 +02:00
|
|
|
}
|
2011-08-04 07:45:40 +02:00
|
|
|
|
2022-09-25 22:32:46 +02:00
|
|
|
rbenv_command=rbenv
|
|
|
|
if [ -z "$rbenv_in_path" ]; then
|
2024-05-03 16:59:39 +02:00
|
|
|
rbenv_command="$(display_path "$root/bin/rbenv")"
|
2022-09-25 22:32:46 +02:00
|
|
|
fi
|
|
|
|
|
2024-05-03 16:59:39 +02:00
|
|
|
color_start=""
|
|
|
|
color_end=""
|
|
|
|
if [ -t 1 ]; then
|
|
|
|
color_start=$'\e[33;1m'
|
|
|
|
color_end=$'\e[m'
|
|
|
|
fi
|
|
|
|
|
|
|
|
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
|
2013-09-28 15:04:24 +02:00
|
|
|
case "$shell" in
|
2024-05-03 16:59:39 +02:00
|
|
|
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" \
|
2024-07-05 14:44:17 +02:00
|
|
|
"eval \"\$($rbenv_command init - --no-rehash bash)\""
|
2024-05-03 16:59:39 +02:00
|
|
|
;;
|
|
|
|
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" \
|
2024-07-05 14:44:17 +02:00
|
|
|
"eval \"\$($rbenv_command init - --no-rehash zsh)\""
|
2024-05-03 16:59:39 +02:00
|
|
|
;;
|
|
|
|
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)\""
|
|
|
|
;;
|
2013-09-28 15:04:24 +02:00
|
|
|
fish )
|
2024-05-03 16:59:39 +02:00
|
|
|
write_config "${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish" \
|
2024-07-05 14:44:17 +02:00
|
|
|
"status --is-interactive; and $rbenv_command init - --no-rehash fish | source"
|
2013-09-28 15:04:24 +02:00
|
|
|
;;
|
|
|
|
* )
|
2024-05-03 16:59:39 +02:00
|
|
|
printf 'unsupported shell: "%s"\n' "$shell" >&2
|
|
|
|
status=1
|
2013-09-28 15:04:24 +02:00
|
|
|
;;
|
|
|
|
esac
|
2024-05-03 16:59:39 +02:00
|
|
|
done
|
|
|
|
exit $status
|
2011-08-04 07:45:40 +02:00
|
|
|
fi
|
|
|
|
|
2011-09-11 18:58:57 +02:00
|
|
|
mkdir -p "${RBENV_ROOT}/"{shims,versions}
|
2011-08-04 07:48:37 +02:00
|
|
|
|
2024-05-03 16:59:39 +02:00
|
|
|
shell="${shells[0]}"
|
2013-09-28 18:43:39 +02:00
|
|
|
case "$shell" in
|
|
|
|
fish )
|
2022-09-24 23:59:13 +02:00
|
|
|
[ -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"
|
2013-09-28 18:43:39 +02:00
|
|
|
;;
|
|
|
|
* )
|
2022-09-24 23:59:13 +02:00
|
|
|
# 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"
|
2020-01-28 17:47:59 +01:00
|
|
|
|
|
|
|
completion="${root}/completions/rbenv.${shell}"
|
|
|
|
if [ -r "$completion" ]; then
|
2022-09-24 23:59:13 +02:00
|
|
|
printf "source '%s'\n" "$completion"
|
2020-01-28 17:47:59 +01:00
|
|
|
fi
|
2013-09-28 18:43:39 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2011-12-26 02:59:24 +01:00
|
|
|
if [ -z "$no_rehash" ]; then
|
2014-11-29 06:16:14 +01:00
|
|
|
echo 'command rbenv rehash 2>/dev/null'
|
2011-12-26 02:59:24 +01:00
|
|
|
fi
|
2011-08-23 18:34:42 +02:00
|
|
|
|
2022-10-09 14:48:13 +02:00
|
|
|
IFS=$'\n' read -d '' -r -a commands <<<"$(rbenv-commands --sh)" || true
|
|
|
|
|
2013-08-15 16:01:13 +02:00
|
|
|
case "$shell" in
|
|
|
|
fish )
|
|
|
|
cat <<EOS
|
|
|
|
function rbenv
|
|
|
|
set command \$argv[1]
|
2013-09-28 15:04:24 +02:00
|
|
|
set -e argv[1]
|
2013-08-15 16:01:13 +02:00
|
|
|
|
|
|
|
switch "\$command"
|
|
|
|
case ${commands[*]}
|
2020-02-17 21:32:12 +01:00
|
|
|
rbenv "sh-\$command" \$argv|source
|
2013-08-15 16:01:13 +02:00
|
|
|
case '*'
|
|
|
|
command rbenv "\$command" \$argv
|
|
|
|
end
|
|
|
|
end
|
2013-09-28 15:58:13 +02:00
|
|
|
EOS
|
|
|
|
;;
|
2023-05-13 18:56:22 +02:00
|
|
|
ksh | ksh93 | mksh )
|
2013-09-28 15:58:13 +02:00
|
|
|
cat <<EOS
|
|
|
|
function rbenv {
|
|
|
|
typeset command
|
2013-08-15 16:01:13 +02:00
|
|
|
EOS
|
|
|
|
;;
|
|
|
|
* )
|
|
|
|
cat <<EOS
|
2011-12-24 23:49:22 +01:00
|
|
|
rbenv() {
|
2013-09-28 15:58:13 +02:00
|
|
|
local command
|
|
|
|
EOS
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2014-06-02 19:36:49 +02:00
|
|
|
if [ "$shell" != "fish" ]; then
|
2013-09-28 15:58:13 +02:00
|
|
|
IFS="|"
|
|
|
|
cat <<EOS
|
2017-01-02 07:06:39 +01:00
|
|
|
command="\${1:-}"
|
2011-09-11 02:50:12 +02:00
|
|
|
if [ "\$#" -gt 0 ]; then
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2011-09-11 02:45:36 +02:00
|
|
|
case "\$command" in
|
2011-08-23 18:34:42 +02:00
|
|
|
${commands[*]})
|
2015-10-12 01:31:57 +02:00
|
|
|
eval "\$(rbenv "sh-\$command" "\$@")";;
|
2011-08-23 18:34:42 +02:00
|
|
|
*)
|
2011-09-11 02:45:36 +02:00
|
|
|
command rbenv "\$command" "\$@";;
|
2011-09-11 02:50:12 +02:00
|
|
|
esac
|
|
|
|
}
|
2011-08-23 18:34:42 +02:00
|
|
|
EOS
|
2014-06-02 19:36:49 +02:00
|
|
|
fi
|