diff --git a/completions/rbenv.bash b/completions/rbenv.bash new file mode 100644 index 00000000..0e8a383d --- /dev/null +++ b/completions/rbenv.bash @@ -0,0 +1,29 @@ +_rbenv_commands() { + COMPREPLY=() + local cur="${COMP_WORDS[COMP_CWORD]}" + COMPREPLY=( $( compgen -W "$(rbenv commands)" -- $cur ) ) +} + +_rbenv_versions() { + COMPREPLY=() + local cur="${COMP_WORDS[COMP_CWORD]}" + local versions="$(echo system; rbenv versions --bare)" + COMPREPLY=( $( compgen -W "$versions" -- $cur ) ) +} + +_rbenv() { + COMPREPLY=() + local cur="${COMP_WORDS[COMP_CWORD]}" + local prev="${COMP_WORDS[COMP_CWORD-1]}" + + case "$prev" in + set-* | prefix ) + _rbenv_versions + ;; + * ) + _rbenv_commands + ;; + esac +} + +complete -F _rbenv rbenv diff --git a/libexec/rbenv-commands b/libexec/rbenv-commands new file mode 100755 index 00000000..49ba268b --- /dev/null +++ b/libexec/rbenv-commands @@ -0,0 +1,11 @@ +#!/usr/bin/env bash -e + +shopt -s nullglob + +{ for path in ${PATH//:/$'\n'}; do + for command in "${path}/rbenv-"*; do + echo "${command##*rbenv-}" + done + done +} | sort | uniq + diff --git a/libexec/rbenv-prefix b/libexec/rbenv-prefix index f2823b31..9d17ee42 100755 --- a/libexec/rbenv-prefix +++ b/libexec/rbenv-prefix @@ -6,6 +6,12 @@ elif [ -z "$RBENV_VERSION" ]; then RBENV_VERSION="$(rbenv-version)" fi +if [ "$RBENV_VERSION" = "system" ]; then + RUBY_PATH="$(rbenv-which ruby)" + echo "${RUBY_PATH%/*}" + exit +fi + RBENV_PREFIX_PATH="${HOME}/.rbenv/versions/${RBENV_VERSION}" if [ ! -d "$RBENV_PREFIX_PATH" ]; then echo "rbenv: version \`${RBENV_VERSION}' not installed" >&2 diff --git a/libexec/rbenv-version b/libexec/rbenv-version index 01c1afdf..2c9cae13 100755 --- a/libexec/rbenv-version +++ b/libexec/rbenv-version @@ -36,6 +36,11 @@ if [ -z "$RBENV_VERSION" ]; then fi fi +if [ "$RBENV_VERSION" = "system" ]; then + echo "$RBENV_VERSION" + exit +fi + RBENV_VERSION_PATH="${HOME}/.rbenv/versions/${RBENV_VERSION}" if [ -d "$RBENV_VERSION_PATH" ]; then diff --git a/libexec/rbenv-which b/libexec/rbenv-which index 74572f47..f38bba61 100755 --- a/libexec/rbenv-which +++ b/libexec/rbenv-which @@ -1,8 +1,35 @@ #!/usr/bin/env bash -e +expand_path() { + local cwd="$(pwd)" + cd "$1" + pwd + cd "$cwd" +} + +remove_from_path() { + local path_to_remove="$(expand_path "$1")" + local result="" + + for path in ${PATH//:/$'\n'}; do + path="$(expand_path "$path" || true)" + if [ "$path" != "$path_to_remove" ]; then + result="${result}${path}:" + fi + done + + echo "${result%:}" +} + RBENV_VERSION="$(rbenv-version)" RBENV_COMMAND="$1" -RBENV_COMMAND_PATH="${HOME}/.rbenv/versions/${RBENV_VERSION}/bin/${RBENV_COMMAND}" + +if [ "$RBENV_VERSION" = "system" ]; then + PATH="$(remove_from_path "${HOME}/.rbenv/shims")" + RBENV_COMMAND_PATH="$(command -v "$RBENV_COMMAND")" +else + RBENV_COMMAND_PATH="${HOME}/.rbenv/versions/${RBENV_VERSION}/bin/${RBENV_COMMAND}" +fi shopt -s nullglob RBENV_WHICH_PLUGINS=(/etc/rbenv.d/which/*.bash ${HOME}/.rbenv/rbenv.d/which/*.bash)