From e0e2d936393e04550647419b8c6da7c633887244 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Wed, 3 Aug 2011 08:39:56 -0500 Subject: [PATCH 1/9] Add support for RBENV_VERSION=system --- libexec/rbenv-prefix | 6 ++++++ libexec/rbenv-version | 5 +++++ libexec/rbenv-which | 29 ++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) 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 7f308e2d..42dde049 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 if [ -x "$RBENV_COMMAND_PATH" ]; then echo "$RBENV_COMMAND_PATH" From 2099355ad5babc9facf83586eb9ddac96125ac1a Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Wed, 3 Aug 2011 20:41:06 -0500 Subject: [PATCH 2/9] Pull in @telemachus' Bash autocompletion defintion from https://gist.github.com/1122379 --- completions/rbenv.bash | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 completions/rbenv.bash diff --git a/completions/rbenv.bash b/completions/rbenv.bash new file mode 100644 index 00000000..804ac2f2 --- /dev/null +++ b/completions/rbenv.bash @@ -0,0 +1,41 @@ +_commands() +{ + local cur commands + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + commands="exec prefix rehash set-default set-local version versions\ + whence which" + + COMPREPLY=( $( compgen -W "${commands}" -- ${cur} ) ) +} + +_rubies() +{ + local cur rubies + local ROOT=$HOME/.rbenv/versions + COMPREPLY=() + cur=${COMP_WORDS[COMP_CWORD]} + rubies=($ROOT/*) + # remove all but the final part of the name + rubies="${rubies[@]##*/}" + + COMPREPLY=( $( compgen -W "${rubies}" -- ${cur} ) ) +} + +_rbenv() +{ + local cur prev + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + + if [[ "${prev}" == set-default ]]; then + _rubies + else + _commands + fi +} + +complete -F _rbenv rbenv + +# vim: set ts=4 sw=4 tw=75 filetype=sh: \ No newline at end of file From 4e79ba15f760284850fa42c4341eef2b87709c24 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Wed, 3 Aug 2011 20:44:29 -0500 Subject: [PATCH 3/9] Match style --- completions/rbenv.bash | 57 +++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/completions/rbenv.bash b/completions/rbenv.bash index 804ac2f2..7843f6b4 100644 --- a/completions/rbenv.bash +++ b/completions/rbenv.bash @@ -1,41 +1,36 @@ -_commands() -{ - local cur commands - COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - commands="exec prefix rehash set-default set-local version versions\ - whence which" +_commands() { + local cur commands + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + commands="exec prefix rehash set-default set-local version versions\ + whence which" - COMPREPLY=( $( compgen -W "${commands}" -- ${cur} ) ) + COMPREPLY=( $( compgen -W "$commands" -- $cur ) ) } -_rubies() -{ - local cur rubies - local ROOT=$HOME/.rbenv/versions - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} - rubies=($ROOT/*) - # remove all but the final part of the name - rubies="${rubies[@]##*/}" +_rubies() { + local cur rubies + local ROOT="${HOME}/.rbenv/versions" + COMPREPLY=() + cur=${COMP_WORDS[COMP_CWORD]} + rubies=($ROOT/*) + # remove all but the final part of the name + rubies="${rubies[@]##*/}" - COMPREPLY=( $( compgen -W "${rubies}" -- ${cur} ) ) + COMPREPLY=( $( compgen -W "$rubies" -- $cur ) ) } -_rbenv() -{ - local cur prev - COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - prev="${COMP_WORDS[COMP_CWORD-1]}" +_rbenv() { + local cur prev + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" - if [[ "${prev}" == set-default ]]; then - _rubies - else - _commands - fi + if [ "$prev" = "set-default" ]; then + _rubies + else + _commands + fi } complete -F _rbenv rbenv - -# vim: set ts=4 sw=4 tw=75 filetype=sh: \ No newline at end of file From f7c463bed96a04ec5ecf12897f9fdf6aac6163db Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Wed, 3 Aug 2011 21:43:40 -0500 Subject: [PATCH 4/9] Rename helpers --- completions/rbenv.bash | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/completions/rbenv.bash b/completions/rbenv.bash index 7843f6b4..438f5f14 100644 --- a/completions/rbenv.bash +++ b/completions/rbenv.bash @@ -1,4 +1,4 @@ -_commands() { +_rbenv_commands() { local cur commands COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" @@ -8,16 +8,16 @@ _commands() { COMPREPLY=( $( compgen -W "$commands" -- $cur ) ) } -_rubies() { - local cur rubies +_rbenv_versions() { + local cur versions local ROOT="${HOME}/.rbenv/versions" COMPREPLY=() cur=${COMP_WORDS[COMP_CWORD]} - rubies=($ROOT/*) + versions=($ROOT/*) # remove all but the final part of the name - rubies="${rubies[@]##*/}" + versions="${versions[@]##*/}" - COMPREPLY=( $( compgen -W "$rubies" -- $cur ) ) + COMPREPLY=( $( compgen -W "$versions" -- $cur ) ) } _rbenv() { @@ -27,9 +27,9 @@ _rbenv() { prev="${COMP_WORDS[COMP_CWORD-1]}" if [ "$prev" = "set-default" ]; then - _rubies + _rbenv_versions else - _commands + _rbenv_commands fi } From df034f5d354d0bab5e70294c1d8694bb3f4da6da Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Wed, 3 Aug 2011 21:50:03 -0500 Subject: [PATCH 5/9] Defer to `rbenv versions` instead of reading ~/.rbenv/versions manually --- completions/rbenv.bash | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/completions/rbenv.bash b/completions/rbenv.bash index 438f5f14..de53fc69 100644 --- a/completions/rbenv.bash +++ b/completions/rbenv.bash @@ -9,13 +9,10 @@ _rbenv_commands() { } _rbenv_versions() { - local cur versions - local ROOT="${HOME}/.rbenv/versions" COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} - versions=($ROOT/*) - # remove all but the final part of the name - versions="${versions[@]##*/}" + local cur=${COMP_WORDS[COMP_CWORD]} + local versions=(system $(rbenv versions --bare)) + versions="${versions[@]}" COMPREPLY=( $( compgen -W "$versions" -- $cur ) ) } From 3b13dc9c144b62641ee04872b9090580000f242f Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Wed, 3 Aug 2011 22:20:01 -0500 Subject: [PATCH 6/9] Add rbenv-commands --- libexec/rbenv-commands | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100755 libexec/rbenv-commands 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 + From e4a040f1e092a29f42099482e20ba5017b7e09e2 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Wed, 3 Aug 2011 22:20:25 -0500 Subject: [PATCH 7/9] Defer to `rbenv commands` --- completions/rbenv.bash | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/completions/rbenv.bash b/completions/rbenv.bash index de53fc69..539c05bf 100644 --- a/completions/rbenv.bash +++ b/completions/rbenv.bash @@ -1,11 +1,7 @@ _rbenv_commands() { - local cur commands COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - commands="exec prefix rehash set-default set-local version versions\ - whence which" - - COMPREPLY=( $( compgen -W "$commands" -- $cur ) ) + local cur="${COMP_WORDS[COMP_CWORD]}" + COMPREPLY=( $( compgen -W "$(rbenv commands)" -- $cur ) ) } _rbenv_versions() { From ccf7c29fc4f460129376fcb6b812e52cd292d4bf Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Wed, 3 Aug 2011 22:22:45 -0500 Subject: [PATCH 8/9] Simplify _rbenv_versions --- completions/rbenv.bash | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/completions/rbenv.bash b/completions/rbenv.bash index 539c05bf..91107085 100644 --- a/completions/rbenv.bash +++ b/completions/rbenv.bash @@ -6,10 +6,8 @@ _rbenv_commands() { _rbenv_versions() { COMPREPLY=() - local cur=${COMP_WORDS[COMP_CWORD]} - local versions=(system $(rbenv versions --bare)) - versions="${versions[@]}" - + local cur="${COMP_WORDS[COMP_CWORD]}" + local versions="$(echo system; rbenv versions --bare)" COMPREPLY=( $( compgen -W "$versions" -- $cur ) ) } From f904d1b92f6588efac620e16579453727760b5aa Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Wed, 3 Aug 2011 22:28:30 -0500 Subject: [PATCH 9/9] Complete versions for `rbenv set-local` and `rbenv prefix` too --- completions/rbenv.bash | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/completions/rbenv.bash b/completions/rbenv.bash index 91107085..0e8a383d 100644 --- a/completions/rbenv.bash +++ b/completions/rbenv.bash @@ -12,16 +12,18 @@ _rbenv_versions() { } _rbenv() { - local cur prev COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - prev="${COMP_WORDS[COMP_CWORD-1]}" + local cur="${COMP_WORDS[COMP_CWORD]}" + local prev="${COMP_WORDS[COMP_CWORD-1]}" - if [ "$prev" = "set-default" ]; then + case "$prev" in + set-* | prefix ) _rbenv_versions - else + ;; + * ) _rbenv_commands - fi + ;; + esac } complete -F _rbenv rbenv