create hook: version-name

Expose a `version-name` hook.

It is invoked *after* the traditional `RBENV_VERSION` lookup. Which means hook scripts can interrogate `$RBENV_VERSION_FILE` and/or `$RBENV_VERSION` (or use the executables).

The hooks are then run, giving plugins a chance to alter `RBENV_VERSION`. Once the hooks have run, we now have (in `$RBENV_VERSION`) the actual version we want to use (or it's empty which defaults to `system` per normal). Lastly, the same logic remains for checking if the version exists, or trimming the `ruby-` prefix.

Prime example: the ruby-bundler-ruby-version plugin can select a ruby by using the `ruby` directive from the `Gemfile` if a local `.ruby-version` doesn't exist.
This commit is contained in:
Jason Karns 2015-05-27 23:10:39 -04:00 committed by Mislav Marohnić
parent 5b9e4f0584
commit 258e4413b1
2 changed files with 26 additions and 0 deletions

View file

@ -8,6 +8,13 @@ if [ -z "$RBENV_VERSION" ]; then
RBENV_VERSION="$(rbenv-version-file-read "$RBENV_VERSION_FILE" || true)"
fi
OLDIFS="$IFS"
IFS=$'\n' scripts=(`rbenv-hooks version-name`)
IFS="$OLDIFS"
for script in "${scripts[@]}"; do
source "$script"
done
if [ -z "$RBENV_VERSION" ] || [ "$RBENV_VERSION" = "system" ]; then
echo "system"
exit

View file

@ -2,6 +2,13 @@
load test_helper
export RBENV_HOOK_PATH="${RBENV_ROOT}/rbenv.d"
create_hook() {
mkdir -p "${RBENV_ROOT}/rbenv.d/version-name"
cat > "${RBENV_ROOT}/rbenv.d/version-name/$1" <<<"$2"
}
create_version() {
mkdir -p "${RBENV_ROOT}/versions/$1"
}
@ -22,6 +29,18 @@ setup() {
assert_success "system"
}
@test "RBENV_VERSION can be overridden by hook" {
create_version "1.8.7"
create_version "1.9.3"
RBENV_VERSION=1.8.7 run rbenv-version-name
assert_success "1.8.7"
create_hook test.bash "RBENV_VERSION=1.9.3"
RBENV_VERSION=1.8.7 run rbenv-version-name
assert_success "1.9.3"
}
@test "RBENV_VERSION has precedence over local" {
create_version "1.8.7"
create_version "1.9.3"