You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
250 lines
8.1 KiB
250 lines
8.1 KiB
#!/bin/bash
|
|
# Ruby Version Manager (rbenv) Setup Script
|
|
# Installs rbenv and Ruby 3.1+ for CocoaPods compatibility
|
|
|
|
set -e
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
log_info() {
|
|
echo -e "${GREEN}[INFO]${NC} $1"
|
|
}
|
|
|
|
log_warn() {
|
|
echo -e "${YELLOW}[WARN]${NC} $1"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
log_step() {
|
|
echo -e "${BLUE}[STEP]${NC} $1"
|
|
}
|
|
|
|
# Check if rbenv is already installed
|
|
if command -v rbenv &> /dev/null; then
|
|
log_info "rbenv is already installed"
|
|
RBENV_INSTALLED=true
|
|
else
|
|
log_step "Installing rbenv..."
|
|
RBENV_INSTALLED=false
|
|
fi
|
|
|
|
# Install rbenv via Homebrew (recommended on macOS)
|
|
if [ "$RBENV_INSTALLED" = false ]; then
|
|
if command -v brew &> /dev/null; then
|
|
log_info "Installing rbenv via Homebrew..."
|
|
brew install rbenv ruby-build
|
|
|
|
# Initialize rbenv in shell
|
|
if [ -n "$ZSH_VERSION" ]; then
|
|
SHELL_CONFIG="$HOME/.zshrc"
|
|
else
|
|
SHELL_CONFIG="$HOME/.bash_profile"
|
|
fi
|
|
|
|
# Add rbenv initialization to shell config
|
|
if ! grep -q "rbenv init" "$SHELL_CONFIG" 2>/dev/null; then
|
|
log_info "Adding rbenv initialization to $SHELL_CONFIG..."
|
|
echo '' >> "$SHELL_CONFIG"
|
|
echo '# rbenv initialization' >> "$SHELL_CONFIG"
|
|
echo 'eval "$(rbenv init - zsh)"' >> "$SHELL_CONFIG"
|
|
fi
|
|
|
|
# Load rbenv in current session
|
|
eval "$(rbenv init - zsh)"
|
|
|
|
log_info "✓ rbenv installed successfully"
|
|
else
|
|
log_warn "Homebrew not found. Installing rbenv manually..."
|
|
|
|
# Manual installation via git
|
|
if [ ! -d "$HOME/.rbenv" ]; then
|
|
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
|
|
fi
|
|
|
|
if [ ! -d "$HOME/.rbenv/plugins/ruby-build" ]; then
|
|
mkdir -p ~/.rbenv/plugins
|
|
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
|
|
fi
|
|
|
|
# Add to PATH
|
|
export PATH="$HOME/.rbenv/bin:$PATH"
|
|
eval "$(rbenv init - zsh)"
|
|
|
|
# Add to shell config
|
|
if [ -n "$ZSH_VERSION" ]; then
|
|
SHELL_CONFIG="$HOME/.zshrc"
|
|
else
|
|
SHELL_CONFIG="$HOME/.bash_profile"
|
|
fi
|
|
|
|
if ! grep -q "rbenv init" "$SHELL_CONFIG" 2>/dev/null; then
|
|
echo '' >> "$SHELL_CONFIG"
|
|
echo '# rbenv initialization' >> "$SHELL_CONFIG"
|
|
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> "$SHELL_CONFIG"
|
|
echo 'eval "$(rbenv init - zsh)"' >> "$SHELL_CONFIG"
|
|
fi
|
|
|
|
log_info "✓ rbenv installed manually"
|
|
fi
|
|
fi
|
|
|
|
# Reload shell config
|
|
log_step "Reloading shell configuration..."
|
|
if [ -n "$ZSH_VERSION" ]; then
|
|
source ~/.zshrc 2>/dev/null || true
|
|
else
|
|
source ~/.bash_profile 2>/dev/null || true
|
|
fi
|
|
|
|
# Ensure rbenv is in PATH
|
|
export PATH="$HOME/.rbenv/bin:$PATH"
|
|
eval "$(rbenv init - zsh)" 2>/dev/null || eval "$(rbenv init - bash)" 2>/dev/null || true
|
|
|
|
# Check current Ruby version
|
|
log_step "Checking current Ruby version..."
|
|
CURRENT_RUBY=$(ruby -v 2>/dev/null | cut -d' ' -f2 | cut -d. -f1,2) || CURRENT_RUBY="unknown"
|
|
|
|
if [ "$CURRENT_RUBY" != "unknown" ]; then
|
|
RUBY_MAJOR=$(echo "$CURRENT_RUBY" | cut -d. -f1)
|
|
RUBY_MINOR=$(echo "$CURRENT_RUBY" | cut -d. -f2)
|
|
|
|
if [ "$RUBY_MAJOR" -ge 3 ] && [ "$RUBY_MINOR" -ge 1 ]; then
|
|
log_info "✓ Ruby version $CURRENT_RUBY is already >= 3.1.0"
|
|
log_info "You can proceed with CocoaPods installation"
|
|
exit 0
|
|
else
|
|
log_warn "Current Ruby version: $CURRENT_RUBY (needs 3.1.0+)"
|
|
fi
|
|
fi
|
|
|
|
# Check if rbenv has a suitable Ruby version already installed
|
|
log_step "Checking installed Ruby versions..."
|
|
if rbenv versions | grep -qE "3\.(1|2|3|4)\."; then
|
|
INSTALLED_RUBY=$(rbenv versions | grep -E "3\.(1|2|3|4)\." | tail -1 | sed 's/^[[:space:]]*//' | cut -d' ' -f1)
|
|
log_info "Found installed Ruby version: $INSTALLED_RUBY"
|
|
|
|
# Set as global if not already set
|
|
CURRENT_GLOBAL=$(rbenv global)
|
|
if [ "$CURRENT_GLOBAL" != "$INSTALLED_RUBY" ]; then
|
|
log_info "Setting $INSTALLED_RUBY as default..."
|
|
rbenv global "$INSTALLED_RUBY"
|
|
rbenv rehash
|
|
fi
|
|
|
|
# Verify it works
|
|
export PATH="$HOME/.rbenv/bin:$PATH"
|
|
eval "$(rbenv init - zsh)" 2>/dev/null || eval "$(rbenv init - bash)" 2>/dev/null || true
|
|
|
|
if ruby -rpsych -e "true" 2>/dev/null; then
|
|
VERIFIED_RUBY=$(ruby -v)
|
|
log_info "✓ Ruby $VERIFIED_RUBY is working correctly"
|
|
log_info ""
|
|
log_info "Ruby setup complete!"
|
|
log_info ""
|
|
log_info "Next steps:"
|
|
log_info " 1. Reload your shell: source ~/.zshrc"
|
|
log_info " 2. Verify Ruby: ruby -v"
|
|
log_info " 3. Install CocoaPods: gem install cocoapods"
|
|
exit 0
|
|
else
|
|
log_warn "Installed Ruby $INSTALLED_RUBY found but psych extension not working"
|
|
log_warn "May need to reinstall Ruby or install libyaml dependencies"
|
|
fi
|
|
fi
|
|
|
|
# Check for libyaml dependency (required for psych extension)
|
|
log_step "Checking for libyaml dependency..."
|
|
LIBYAML_FOUND=false
|
|
if command -v brew &> /dev/null; then
|
|
if brew list libyaml &> /dev/null; then
|
|
LIBYAML_FOUND=true
|
|
log_info "✓ libyaml found via Homebrew"
|
|
else
|
|
log_warn "libyaml not installed. Installing via Homebrew..."
|
|
if brew install libyaml; then
|
|
LIBYAML_FOUND=true
|
|
log_info "✓ libyaml installed successfully"
|
|
else
|
|
log_error "Failed to install libyaml via Homebrew"
|
|
fi
|
|
fi
|
|
else
|
|
# Check if libyaml headers exist in system locations
|
|
if find /usr/local /opt /Library -name "yaml.h" 2>/dev/null | grep -q yaml.h; then
|
|
LIBYAML_FOUND=true
|
|
log_info "✓ libyaml headers found in system"
|
|
else
|
|
log_warn "libyaml not found. Ruby installation may fail."
|
|
log_warn "Install libyaml via Homebrew: brew install libyaml"
|
|
log_warn "Or install via MacPorts: sudo port install libyaml"
|
|
fi
|
|
fi
|
|
|
|
# List available Ruby versions
|
|
log_step "Fetching available Ruby versions..."
|
|
rbenv install --list | grep -E "^[[:space:]]*3\.[1-9]" | tail -5 || log_warn "Could not fetch Ruby versions list"
|
|
|
|
# Install Ruby 3.1.0 (preferred for compatibility)
|
|
log_step "Installing Ruby 3.1.0..."
|
|
if rbenv install 3.1.0; then
|
|
log_info "✓ Ruby 3.1.0 installed successfully"
|
|
|
|
# Set as global default
|
|
log_step "Setting Ruby 3.1.0 as default..."
|
|
rbenv global 3.1.0
|
|
|
|
# Verify installation
|
|
export PATH="$HOME/.rbenv/bin:$PATH"
|
|
eval "$(rbenv init - zsh)" 2>/dev/null || eval "$(rbenv init - bash)" 2>/dev/null || true
|
|
|
|
NEW_RUBY=$(ruby -v)
|
|
log_info "✓ Current Ruby version: $NEW_RUBY"
|
|
|
|
# Verify psych extension works
|
|
if ruby -rpsych -e "true" 2>/dev/null; then
|
|
log_info "✓ psych extension verified"
|
|
else
|
|
log_warn "psych extension may not be working correctly"
|
|
log_warn "This may affect CocoaPods installation"
|
|
fi
|
|
|
|
# Rehash to make Ruby available
|
|
rbenv rehash
|
|
|
|
log_info ""
|
|
log_info "Ruby setup complete!"
|
|
log_info ""
|
|
log_info "Next steps:"
|
|
log_info " 1. Reload your shell: source ~/.zshrc"
|
|
log_info " 2. Verify Ruby: ruby -v"
|
|
log_info " 3. Install CocoaPods: gem install cocoapods"
|
|
|
|
else
|
|
log_error "Failed to install Ruby 3.1.0"
|
|
|
|
if [ "$LIBYAML_FOUND" = false ]; then
|
|
log_error ""
|
|
log_error "Installation failed. This is likely due to missing libyaml dependency."
|
|
log_error ""
|
|
log_error "To fix:"
|
|
if command -v brew &> /dev/null; then
|
|
log_error " brew install libyaml"
|
|
else
|
|
log_error " Install Homebrew: /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
|
|
log_error " Then: brew install libyaml"
|
|
fi
|
|
log_error ""
|
|
log_error "After installing libyaml, run this script again."
|
|
else
|
|
log_error "Installation failed. Please check your internet connection and try again."
|
|
fi
|
|
exit 1
|
|
fi
|