pyenv/libexec/rbenv-exec
Mislav Marohnić db143bb654 rbenv exec: avoid mutating PATH
Enables shelling out from a ruby process started with rbenv to a ruby
process with a different RBENV_VERSION. Fixes #121

This removes the workaround created for #15 and solves `ruby -S` support
by setting RUBYPATH. PATH is never changed.

To illustrate how RUBYPATH changes in various configurations:

    PATH=~/bin:~/.rbenv/shims:/usr/bin:/bin
    RBENV_VERSION=1.8 ruby -S rake
    #=> executes ~/.rbenv/versions/1.8/bin/rake
    #=> RUBYPATH=~/bin:~/.rbenv/versions/1.8/bin:/usr/bin:/bin

    RBENV_VERSION=2.0 ruby -S rake
    #=> executes ~/.rbenv/versions/2.0/bin/rake
    #=> RUBYPATH=~/bin:~/.rbenv/versions/2.0/bin:/usr/bin:/bin

    RBENV_VERSION=system ruby -S rake
    #=> executes /usr/bin/rake
    #=> RUBYPATH=~/bin:/rbenv_shims_were_here:/usr/bin:/bin

    RBENV_VERSION=1.8 ruby -S rake
    #=> executes ~/.rbenv/versions/1.8/bin/rake
    #=> RUBYPATH=~/bin:~/.rbenv/versions/1.8/bin:/usr/bin:/bin
2013-06-20 20:41:22 +02:00

83 lines
1.9 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# Summary: Run an executable with the selected Ruby version
#
# Usage: rbenv exec <command> [args...]
#
# Runs an executable contained by the currently selected Ruby's bin
# directory. Rough equivalent of:
#
# exec "$(rbenv prefix)/bin/$command" args...
set -e
[ -n "$RBENV_DEBUG" ] && set -x
rubypath=""
# Provide rbenv completions
while true; do
case "$1" in
"--complete" )
exec rbenv shims --short
;;
"--rubypath" )
rubypath=1
shift 1
;;
* )
break
;;
esac
done
# Replace any "RBENV_ROOT/shims" or "RBENV_ROOT/versions/*/bin" paths in the
# list with the given path. If no replacements were made, prepend the path onto
# the list.
replace_shims_path() {
local path="$1"
local dir="$2"
# fake directory that serves as a placeholder for shims location in RUBYPATH:
local placeholder="/rbenv_shims_were_here"
local found=""
local result=""
local -a paths
IFS=: paths=($path)
for path in "${paths[@]}"; do
if [[ $path = "${RBENV_ROOT}/shims" || $path == "${RBENV_ROOT}/versions/"*/bin || $path = $placeholder ]]; then
found=1
result="${result}${dir:-$placeholder}:"
else
result="${result}${path}:"
fi
done
# if no rbenv paths were replaced, simply prepend the path
[ -n "$found" -o -z "$dir" ] || result="${dir}:${path}"
echo "${result%:}"
}
RBENV_VERSION="$(rbenv-version-name)"
RBENV_COMMAND="$1"
if [ -z "$RBENV_COMMAND" ]; then
rbenv-help --usage exec >&2
exit 1
fi
export RBENV_VERSION
RBENV_COMMAND_PATH="$(rbenv-which "$RBENV_COMMAND")"
OLDIFS="$IFS"
IFS=$'\n' scripts=(`rbenv-hooks exec`)
IFS="$OLDIFS"
for script in "${scripts[@]}"; do
source "$script"
done
shift 1
if [ -n "$rubypath" ]; then
bindir=""
[ "$RBENV_VERSION" != "system" ] && bindir="${RBENV_COMMAND_PATH%/*}"
export RUBYPATH="$(replace_shims_path "${RUBYPATH:-$PATH}" "$bindir")"
fi
exec -a "$RBENV_COMMAND" "$RBENV_COMMAND_PATH" "$@"