#!/usr/bin/env bash set -euo pipefail fail() { printf '\nMacish failed: %s\n' "$*" >&2; exit 1; } step() { printf '\n==> %s\n' "$*"; } run() { printf ' %s\n' "$*"; "$@"; } append_once() { local file="$1" line="$2" touch "$file" if ! grep -Fqx "$line" "$file"; then printf '\n%s\n' "$line" >>"$file"; fi } require_ready() { [[ "$(uname -s)" == "Darwin" ]] || fail "macOS is required." [[ "$EUID" -ne 0 ]] || fail "Run as your normal user, not with sudo." if [[ "${MACISH_YES:-${MACI_YES:-}}" != "1" && "${CI:-}" != "true" ]]; then if { exec 3<>/dev/tty; } 2>/dev/null; then printf '\nPress return to install, or Ctrl-C to stop. ' >&3 read -r _ <&3 || fail "Confirmation cancelled." exec 3>&- else fail "No terminal for confirmation. Review the script, then set MACISH_YES=1 to run unattended." fi fi if ! xcode-select -p >/dev/null 2>&1; then xcode-select --install || true fail "Finish installing Xcode Command Line Tools, then run this script again." fi } ensure_homebrew() { local brew_bin brew_env installer line brew_bin="$(command -v brew || true)" if [[ -z "$brew_bin" ]]; then if [[ -x /opt/homebrew/bin/brew ]]; then brew_bin=/opt/homebrew/bin/brew elif [[ -x /usr/local/bin/brew ]]; then brew_bin=/usr/local/bin/brew else installer="$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || fail "Could not download Homebrew." if [[ -t 1 ]] && { true /dev/null; then /bin/bash -c "$installer" /dev/null 2>&1; then run brew install "$1"; fi } printf '\nMacish / Ruby\nRuby 3.3, Bundler, Git, and mise. No database services.\n' require_ready step "Preparing Homebrew" ensure_homebrew step "Installing Git and mise" ensure_formula git ensure_formula mise step "Installing Ruby" run mise use --global ruby@3.3 append_once "$HOME/.zshrc" 'eval "$(mise activate zsh)"' if ! mise exec ruby@3.3 -- bundle --version >/dev/null 2>&1; then run mise exec ruby@3.3 -- gem install bundler --no-document fi step "Verifying tools" run mise exec ruby@3.3 -- ruby --version run mise exec ruby@3.3 -- bundle --version printf '\nDone. Open a new terminal to use Ruby.\n'