Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion setup-mac.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,19 @@ else
fi

echo "Installing Ruby..."
brew install ruby
read -r -p "Choose Ruby installation method (1 for direct brew install, 2 for rbenv): " ruby_choice
if [ "$ruby_choice" = "1" ]; then
brew install ruby
elif [ "$ruby_choice" = "2" ]; then
brew install rbenv
# shellcheck disable=SC2016
echo 'eval "$(rbenv init -)"' >> ~/.zshrc
export PATH="/opt/homebrew/bin:$PATH"
rbenv global 3.2.2
else
echo "Invalid choice, installing Ruby directly."
brew install ruby
fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The rbenv installation logic has a few critical issues and can be improved for clarity and robustness.

  1. Broken rbenv workflow: The current logic will fail because:

    • It tries to set a global Ruby version (rbenv global 3.2.2) without first installing it via rbenv install 3.2.2.
    • rbenv commands won't work correctly in the current script session because rbenv is not initialized. The echo ... >> ~/.zshrc only configures future shell sessions. You need to run eval "$(rbenv init -)" within the script.
    • The export PATH on line 105 is not the correct way to configure the path for rbenv; this is handled by rbenv init.
  2. Code Structure: The if/elif/else block duplicates the brew install ruby command. Using a case statement is a cleaner way to handle multiple choices and improves maintainability.

  3. Missing Error Handling: For consistency with other installation steps in this script (like Python and Node.js), it's good practice to add explicit error handling for the installation commands.

Here is a suggested replacement that addresses these points:

read -r -p "Choose Ruby installation method (1 for direct brew install, 2 for rbenv): " ruby_choice
case "$ruby_choice" in
  2)
    log "Installing rbenv..."
    if ! brew install rbenv; then
      log "ERROR: Failed to install rbenv."
      exit 1
    fi
    # Add rbenv to zsh for future sessions
    # shellcheck disable=SC2016
    echo 'eval "$(rbenv init -)"' >> ~/.zshrc
    # Initialize rbenv for the current script session
    eval "$(rbenv init -)"

    log "Installing Ruby 3.2.2 with rbenv (this may take a while)..."
    if rbenv install 3.2.2; then
      rbenv global 3.2.2
      log "Ruby 3.2.2 installed and set as global via rbenv."
    else
      log "ERROR: Failed to install Ruby 3.2.2 with rbenv."
      exit 1
    fi
    ;;
  *)
    if [ "$ruby_choice" != "1" ]; then
      echo "Invalid choice, installing Ruby directly."
    fi
    if brew install ruby; then
      log "Ruby installed directly via Homebrew."
    else
      log "ERROR: Failed to install Ruby."
      exit 1
    fi
    ;;
esac


echo "Installing Cocoapods..."
brew install cocoapods
Expand Down