Suggest rbenv global if there is no explicit global setting (#2052)

Upon installing their first Ruby version on the system, the user may want to set it as their default; otherwise the default will remain "system" and any `gem install` attempts will fail with a permission error.

This suggestion is skipped if the user already has chosen an explicit default, even if it's "system".
This commit is contained in:
Mislav Marohnić 2022-09-25 11:20:43 +02:00 committed by GitHub
parent 2443ff3d56
commit cde170b581
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 0 deletions

View file

@ -62,6 +62,23 @@ definitions() {
ruby-build --definitions | $(type -p ggrep grep | head -1) -F "$query" || true ruby-build --definitions | $(type -p ggrep grep | head -1) -F "$query" || true
} }
suggest_selecting_global() {
# shellcheck disable=SC2155
local version_file="$(rbenv-version-file)"
[[ "$version_file" != "$RBENV_ROOT"/version || -e "$version_file" ]] && return 0
echo
colorize 1 "NOTE:"
echo -n " to activate this Ruby version as the new default, run: "
colorize 33 "rbenv global $VERSION_NAME"
echo
}
colorize() {
if [ -t 1 ]; then printf "\e[%sm%s\e[m" "$1" "$2"
else printf "%s" "$2"
fi
}
indent() { indent() {
sed 's/^/ /' sed 's/^/ /'
} }
@ -190,6 +207,7 @@ fi
# REE installer requires an existing Ruby installation to run. An # REE installer requires an existing Ruby installation to run. An
# unsatisfied local .ruby-version file can cause the installer to # unsatisfied local .ruby-version file can cause the installer to
# fail.) # fail.)
# shellcheck disable=SC2155
export RBENV_VERSION="$(rbenv-global 2>/dev/null || true)" export RBENV_VERSION="$(rbenv-global 2>/dev/null || true)"
@ -238,6 +256,7 @@ for hook in "${after_hooks[@]}"; do eval "$hook"; done
# Run `rbenv-rehash` after a successful installation. # Run `rbenv-rehash` after a successful installation.
if [ "$STATUS" == "0" ]; then if [ "$STATUS" == "0" ]; then
rbenv-rehash rbenv-rehash
suggest_selecting_global
else else
cleanup cleanup
fi fi

View file

@ -15,6 +15,7 @@ after_install 'echo after: \$STATUS'
OUT OUT
stub rbenv-hooks "install : echo '$HOOK_PATH'/install.bash" stub rbenv-hooks "install : echo '$HOOK_PATH'/install.bash"
stub rbenv-rehash "echo rehashed" stub rbenv-rehash "echo rehashed"
stub rbenv-version-file "echo .ruby-version"
definition="${TMP}/2.0.0" definition="${TMP}/2.0.0"
cat > "$definition" <<<"echo ruby-build" cat > "$definition" <<<"echo ruby-build"

View file

@ -6,6 +6,9 @@ export RBENV_ROOT="${TMP}/rbenv"
setup() { setup() {
stub rbenv-hooks 'install : true' stub rbenv-hooks 'install : true'
stub rbenv-rehash 'true' stub rbenv-rehash 'true'
stub rbenv-version-file 'echo $RBENV_ROOT/version'
mkdir -p "$RBENV_ROOT"
echo "system" > "$RBENV_ROOT/version"
} }
stub_ruby_build() { stub_ruby_build() {
@ -23,6 +26,20 @@ stub_ruby_build() {
unstub rbenv-rehash unstub rbenv-rehash
} }
@test "suggest running rbenv global after install" {
rm -rf "$RBENV_ROOT/version"
stub_ruby_build 'echo ruby-build "$@"'
run rbenv-install 2.1.2
assert_success <<OUT
ruby-build 2.1.2 ${RBENV_ROOT}/versions/2.1.2
NOTE: to activate this Ruby version as the new default, run: rbenv global 2.1.2
OUT
unstub ruby-build
}
@test "install rbenv local version by default" { @test "install rbenv local version by default" {
stub_ruby_build 'echo ruby-build "$1"' stub_ruby_build 'echo ruby-build "$1"'
stub rbenv-local 'echo 2.1.2' stub rbenv-local 'echo 2.1.2'