create hook: version-origin

Expose a `version-origin` hook.

It is invoked *before* the traditional `rbenv-version-file` lookup. Because `version-origin` is traditionally run immediately after `version-name`, then any plugin hooks that alter `version-name` would have done so. Thus, running `version-origin` prior to printing the origin gives those plugins a chance to alter the `version-origin` to match.

If any of the hooks set `$RBENV_VERSION_ORIGIN`, then it is used as the return value. Otherwise, the existing logic continues to return "environment variable" or "filename" as appropriate.

This change, in conjunction with the `version-name` hook, makes a clean seam by which plugins can inject their own ruby version setting logic. Using this seam, as opposed to altering `$RBENV_COMMAND_PATH` from the `which` hook, means that the version name and origin are set more reliably and so `version`, `version-name`, `version-origin` and `which` all work as expected. Indeed, even PS1 works now.
This commit is contained in:
Jason Karns 2015-05-27 23:29:11 -04:00 committed by Mislav Marohnić
parent 258e4413b1
commit c3a5f91ed0
2 changed files with 26 additions and 1 deletions

View file

@ -3,7 +3,16 @@
set -e set -e
[ -n "$RBENV_DEBUG" ] && set -x [ -n "$RBENV_DEBUG" ] && set -x
if [ -n "$RBENV_VERSION" ]; then OLDIFS="$IFS"
IFS=$'\n' scripts=(`rbenv-hooks version-origin`)
IFS="$OLDIFS"
for script in "${scripts[@]}"; do
source "$script"
done
if [ -n "$RBENV_VERSION_ORIGIN" ]; then
echo "$RBENV_VERSION_ORIGIN"
elif [ -n "$RBENV_VERSION" ]; then
echo "RBENV_VERSION environment variable" echo "RBENV_VERSION environment variable"
else else
rbenv-version-file rbenv-version-file

View file

@ -2,6 +2,13 @@
load test_helper load test_helper
export RBENV_HOOK_PATH="${RBENV_ROOT}/rbenv.d"
create_hook() {
mkdir -p "${RBENV_ROOT}/rbenv.d/version-origin"
cat > "${RBENV_ROOT}/rbenv.d/version-origin/$1" <<<"$2"
}
setup() { setup() {
mkdir -p "$RBENV_TEST_DIR" mkdir -p "$RBENV_TEST_DIR"
cd "$RBENV_TEST_DIR" cd "$RBENV_TEST_DIR"
@ -36,3 +43,12 @@ setup() {
run rbenv-version-origin run rbenv-version-origin
assert_success "${PWD}/.rbenv-version" assert_success "${PWD}/.rbenv-version"
} }
@test "reports from hook" {
touch .ruby-version
create_hook test.bash "RBENV_VERSION_ORIGIN=plugin"
RBENV_VERSION=1 run rbenv-version-origin
assert_success "plugin"
}