|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 5 | +. "$DIR/lib/common.sh" |
| 6 | + |
| 7 | +ACTION="${1:-reconcile}" |
| 8 | +# Target Ruby version (default: latest stable) |
| 9 | +RUBY_VERSION="${RUBY_VERSION:-3.3.6}" |
| 10 | + |
| 11 | +ensure_rbenv() { |
| 12 | + if [ ! -d "$HOME/.rbenv" ]; then |
| 13 | + echo "Installing rbenv..." |
| 14 | + git clone https://github.com/rbenv/rbenv.git "$HOME/.rbenv" |
| 15 | + cd "$HOME/.rbenv" && src/configure && make -C src || true |
| 16 | + fi |
| 17 | + |
| 18 | + # Install ruby-build plugin if missing |
| 19 | + if [ ! -d "$HOME/.rbenv/plugins/ruby-build" ]; then |
| 20 | + echo "Installing ruby-build plugin..." |
| 21 | + git clone https://github.com/rbenv/ruby-build.git "$HOME/.rbenv/plugins/ruby-build" |
| 22 | + fi |
| 23 | + |
| 24 | + ensure_rbenv_loaded |
| 25 | +} |
| 26 | + |
| 27 | +ensure_rbenv_loaded() { |
| 28 | + # Add rbenv to PATH and initialize if not already done |
| 29 | + export PATH="$HOME/.rbenv/bin:$PATH" |
| 30 | + if command -v rbenv >/dev/null 2>&1; then |
| 31 | + eval "$(rbenv init - bash)" || true |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +get_latest_ruby_version() { |
| 36 | + ensure_rbenv |
| 37 | + # Get latest stable Ruby version from rbenv |
| 38 | + rbenv install --list 2>/dev/null | grep -E '^\s*[0-9]+\.[0-9]+\.[0-9]+$' | tail -1 | tr -d ' ' || echo "3.3.6" |
| 39 | +} |
| 40 | + |
| 41 | +install_ruby() { |
| 42 | + ensure_rbenv |
| 43 | + |
| 44 | + # Use latest if RUBY_VERSION not specified |
| 45 | + if [ "$RUBY_VERSION" = "latest" ]; then |
| 46 | + RUBY_VERSION=$(get_latest_ruby_version) |
| 47 | + fi |
| 48 | + |
| 49 | + echo "Installing Ruby $RUBY_VERSION via rbenv..." |
| 50 | + rbenv install --skip-existing "$RUBY_VERSION" || true |
| 51 | + rbenv global "$RUBY_VERSION" || true |
| 52 | + rbenv rehash || true |
| 53 | + |
| 54 | + # Update gem itself |
| 55 | + gem update --system || true |
| 56 | + |
| 57 | + # Install common gems |
| 58 | + gem install bundler rake || true |
| 59 | + rbenv rehash || true |
| 60 | +} |
| 61 | + |
| 62 | +update_ruby() { |
| 63 | + ensure_rbenv |
| 64 | + |
| 65 | + # Get current and latest versions |
| 66 | + local current_version latest_version |
| 67 | + current_version=$(rbenv global 2>/dev/null || echo "") |
| 68 | + latest_version=$(get_latest_ruby_version) |
| 69 | + |
| 70 | + echo "Current Ruby: $current_version" |
| 71 | + echo "Latest Ruby: $latest_version" |
| 72 | + |
| 73 | + # Install latest if different |
| 74 | + if [ "$current_version" != "$latest_version" ]; then |
| 75 | + RUBY_VERSION="$latest_version" |
| 76 | + install_ruby |
| 77 | + else |
| 78 | + # Just update gems |
| 79 | + gem update --system || true |
| 80 | + gem update bundler rake || true |
| 81 | + rbenv rehash || true |
| 82 | + fi |
| 83 | +} |
| 84 | + |
| 85 | +uninstall_ruby() { |
| 86 | + # Remove rbenv-managed Ruby |
| 87 | + if [ -d "$HOME/.rbenv" ]; then |
| 88 | + rm -rf "$HOME/.rbenv" |
| 89 | + fi |
| 90 | + |
| 91 | + # Remove apt Ruby if present |
| 92 | + apt_remove_if_present ruby ruby-dev ruby-rubygems || true |
| 93 | +} |
| 94 | + |
| 95 | +prefers_rbenv_ruby() { |
| 96 | + local ruby_path |
| 97 | + ruby_path="$(command -v ruby 2>/dev/null || true)" |
| 98 | + |
| 99 | + # Prefer rbenv if Ruby is under ~/.rbenv |
| 100 | + case "$ruby_path" in |
| 101 | + "$HOME/.rbenv/"*) return 0 ;; |
| 102 | + *) return 1 ;; |
| 103 | + esac |
| 104 | +} |
| 105 | + |
| 106 | +reconcile_ruby() { |
| 107 | + local before after path |
| 108 | + before="$(command -v ruby >/dev/null 2>&1 && ruby --version || true)" |
| 109 | + |
| 110 | + if ! prefers_rbenv_ruby; then |
| 111 | + echo "Removing apt-managed Ruby in favor of rbenv..." |
| 112 | + apt_remove_if_present ruby ruby-dev ruby-rubygems || true |
| 113 | + install_ruby |
| 114 | + else |
| 115 | + update_ruby |
| 116 | + fi |
| 117 | + |
| 118 | + after="$(command -v ruby >/dev/null 2>&1 && ruby --version || true)" |
| 119 | + path="$(command -v ruby 2>/dev/null || true)" |
| 120 | + |
| 121 | + printf "[%s] before: %s\n" "ruby" "${before:-<none>}" |
| 122 | + printf "[%s] after: %s\n" "ruby" "${after:-<none>}" |
| 123 | + if [ -n "$path" ]; then printf "[%s] path: %s\n" "ruby" "$path"; fi |
| 124 | +} |
| 125 | + |
| 126 | +case "$ACTION" in |
| 127 | + install) install_ruby ;; |
| 128 | + update) update_ruby ;; |
| 129 | + uninstall) uninstall_ruby ;; |
| 130 | + reconcile) reconcile_ruby ;; |
| 131 | + *) echo "Usage: $0 {install|update|uninstall|reconcile}" ; exit 2 ;; |
| 132 | +esac |
0 commit comments