Skip to content

Commit 8218265

Browse files
committed
fix(install): correct version detection for multi-version Ruby
- Add get_specific_ruby_version() to check specific rbenv version - Report version using the specific ruby binary, not the shim - Don't change global version when another is already set - Use RBENV_VERSION to run gem commands in specific version - Simplify update_ruby and reconcile_ruby to use install_ruby
1 parent 632dfd9 commit 8218265

1 file changed

Lines changed: 42 additions & 39 deletions

File tree

scripts/install_ruby.sh

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ ACTION="${1:-reconcile}"
88
# Target Ruby version (default: latest stable)
99
RUBY_VERSION="${RUBY_VERSION:-3.3.6}"
1010

11+
# Get version of a specific Ruby installation
12+
get_specific_ruby_version() {
13+
local version="$1"
14+
local ruby_bin="$HOME/.rbenv/versions/$version/bin/ruby"
15+
if [ -x "$ruby_bin" ]; then
16+
"$ruby_bin" --version 2>/dev/null || true
17+
fi
18+
}
19+
20+
# Extract major.minor from full version (3.4.8 -> 3.4)
21+
get_version_cycle() {
22+
echo "$1" | sed 's/^\([0-9]*\.[0-9]*\).*/\1/'
23+
}
24+
1125
update_ruby_build() {
1226
local ruby_build_dir="$HOME/.rbenv/plugins/ruby-build"
1327
if [ -d "$ruby_build_dir/.git" ]; then
@@ -64,6 +78,12 @@ install_ruby() {
6478
RUBY_VERSION=$(get_latest_ruby_version)
6579
fi
6680

81+
# Check version before install
82+
local before="$(get_specific_ruby_version "$RUBY_VERSION")"
83+
local version_cycle="$(get_version_cycle "$RUBY_VERSION")"
84+
local display_name="ruby"
85+
[ -n "$version_cycle" ] && display_name="ruby@${version_cycle}"
86+
6787
echo "Installing Ruby $RUBY_VERSION via rbenv..."
6888
if ! rbenv install --skip-existing "$RUBY_VERSION"; then
6989
echo "[ruby] Error: Failed to install Ruby $RUBY_VERSION" >&2
@@ -78,44 +98,38 @@ install_ruby() {
7898
return 1
7999
fi
80100

81-
rbenv global "$RUBY_VERSION" || true
101+
# Only set global if no global version is set yet
102+
local current_global="$(rbenv global 2>/dev/null || true)"
103+
if [ -z "$current_global" ] || [ "$current_global" = "system" ]; then
104+
rbenv global "$RUBY_VERSION" || true
105+
fi
82106
rbenv rehash || true
83107

84-
# Update gem itself
85-
gem update --system || true
108+
# Update gem in this specific version
109+
RBENV_VERSION="$RUBY_VERSION" gem update --system || true
86110

87-
# Install common gems
88-
gem install bundler rake || true
111+
# Install common gems in this specific version
112+
RBENV_VERSION="$RUBY_VERSION" gem install bundler rake || true
89113
rbenv rehash || true
114+
115+
# Report version for this specific install
116+
local after="$(get_specific_ruby_version "$RUBY_VERSION")"
117+
local path="$HOME/.rbenv/versions/$RUBY_VERSION/bin/ruby"
118+
printf "[%s] before: %s\n" "$display_name" "${before:-<none>}"
119+
printf "[%s] after: %s\n" "$display_name" "${after:-<none>}"
120+
if [ -x "$path" ]; then printf "[%s] path: %s\n" "$display_name" "$path"; fi
90121
}
91122

92123
update_ruby() {
93124
ensure_rbenv
94125

95-
# Get current version
96-
local current_version target_version
97-
current_version=$(rbenv global 2>/dev/null || echo "")
98-
99126
# Use RUBY_VERSION if set, otherwise get latest from rbenv
100-
if [ -n "${RUBY_VERSION:-}" ] && [ "$RUBY_VERSION" != "latest" ]; then
101-
target_version="$RUBY_VERSION"
102-
else
103-
target_version=$(get_latest_ruby_version)
104-
RUBY_VERSION="$target_version"
127+
if [ -z "${RUBY_VERSION:-}" ] || [ "$RUBY_VERSION" = "latest" ]; then
128+
RUBY_VERSION=$(get_latest_ruby_version)
105129
fi
106130

107-
echo "Current Ruby: $current_version"
108-
echo "Target Ruby: $target_version"
109-
110-
# Install target if different
111-
if [ "$current_version" != "$target_version" ]; then
112-
install_ruby
113-
else
114-
# Just update gems
115-
gem update --system || true
116-
gem update bundler rake || true
117-
rbenv rehash || true
118-
fi
131+
# install_ruby handles its own before/after reporting
132+
install_ruby
119133
}
120134

121135
uninstall_ruby() {
@@ -140,23 +154,12 @@ prefers_rbenv_ruby() {
140154
}
141155

142156
reconcile_ruby() {
143-
local before after path
144-
before="$(command -v ruby >/dev/null 2>&1 && ruby --version || true)"
145-
146157
if ! prefers_rbenv_ruby; then
147158
echo "Removing apt-managed Ruby in favor of rbenv..."
148159
apt_remove_if_present ruby ruby-dev ruby-rubygems || true
149-
install_ruby
150-
else
151-
update_ruby
152160
fi
153-
154-
after="$(command -v ruby >/dev/null 2>&1 && ruby --version || true)"
155-
path="$(command -v ruby 2>/dev/null || true)"
156-
157-
printf "[%s] before: %s\n" "ruby" "${before:-<none>}"
158-
printf "[%s] after: %s\n" "ruby" "${after:-<none>}"
159-
if [ -n "$path" ]; then printf "[%s] path: %s\n" "ruby" "$path"; fi
161+
# install_ruby handles its own before/after reporting
162+
install_ruby
160163
}
161164

162165
case "$ACTION" in

0 commit comments

Comments
 (0)