Skip to content

Instantly share code, notes, and snippets.

@virajp
Last active December 20, 2025 09:41
Show Gist options
  • Select an option

  • Save virajp/c542ff926710ddbd2f65490c900018f5 to your computer and use it in GitHub Desktop.

Select an option

Save virajp/c542ff926710ddbd2f65490c900018f5 to your computer and use it in GitHub Desktop.
macOS setup script
#!/bin/zsh
set -e # Exit immediately if a command exits with a non-zero status
echo "πŸš€ Starting macOS Bootstrap..."
# --- STEP 1: Xcode Command Line Tools ---
# Required for Homebrew and Git
if ! xcode-select -p 1>/dev/null; then
echo "πŸ›  Installing Xcode Command Line Tools..."
xcode-select --install
# Wait for the user to finish the installation popup
echo "⚠️ Press any key ONLY after the Xcode Tools installation popup has finished..."
read -k1 -s
# REVALIDATION: Check if tools are actually present now
if ! xcode-select -p 1>/dev/null; then
echo "❌ Error: Xcode Command Line Tools were not found."
echo " The installation may have failed or was cancelled."
echo " Please run this script again and ensure the installation completes."
exit 1
fi
echo "βœ… Xcode Command Line Tools installed successfully."
else
echo "βœ… Xcode Command Line Tools are present."
fi
# --- STEP 2: Homebrew ---
if (( ! $+commands[brew] )); then
echo "🍺 Homebrew not found. Installing..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Configure PATH for the current session
if [[ -f /opt/homebrew/bin/brew ]]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [[ -f /usr/local/bin/brew ]]; then
eval "$(/usr/local/bin/brew shellenv)"
fi
else
echo "βœ… Homebrew is already installed."
fi
# Run Brew Doctor
echo "🩺 Running brew doctor..."
# We use '|| true' to prevent the script from exiting if brew doctor finds warnings
if brew doctor; then
echo "βœ… Your system is ready to brew."
else
echo "⚠️ Brew doctor found some issues (see above)."
echo " Proceeding with setup, but you may want to check these later."
fi
# --- STEP 3: GitHub CLI (gh) ---
if (( ! $+commands[gh] )); then
echo "πŸ™ Installing GitHub CLI..."
brew install gh
else
echo "βœ… GitHub CLI is installed."
fi
# --- STEP 4: Authentication ---
if ! gh auth status >/dev/null 2>&1; then
echo "πŸ”‘ You need to login to GitHub..."
# -w opens the browser, HTTPS is standard for fresh setups
gh auth login -p HTTPS -w
else
echo "βœ… Already logged in to GitHub."
fi
# --- STEP 5: Discover Private Repo ---
echo "πŸ” Searching for your private setup repository..."
# FIX: Swapped 'full_name' for 'nameWithOwner' based on error log
REPO_NAME=$(gh repo list --limit 100 --visibility private --json nameWithOwner -q '.[].nameWithOwner | select(endswith("macos-setup"))' | head -n 1)
if [[ -z "$REPO_NAME" ]]; then
echo "❌ Error: Could not automatically find a private repository ending in 'macos-setup'."
# Loop until a valid repo is entered
while true; do
echo "Please enter the full repository name manually (e.g., username/macos-setup):"
read "REPO_NAME"
echo "πŸ” Verifying access to '$REPO_NAME'..."
# 'gh repo view' returns 0 if repo exists and is accessible, 1 otherwise
if gh repo view "$REPO_NAME" >/dev/null 2>&1; then
echo "βœ… Repository '$REPO_NAME' confirmed."
break
else
echo "❌ Error: Repository '$REPO_NAME' not found or you don't have access."
echo " Please check the spelling and ensure you are logged into the correct account."
fi
done
else
echo "βœ… Found repository: $REPO_NAME"
fi
# --- STEP 6: Prepare Target Directory ---
# Parse the string "username/repo" into variables
GITHUB_USER="${REPO_NAME%/*}" # Remove everything after the slash
REPO_SLUG="${REPO_NAME#*/}" # Remove everything before the slash
# Construct the specific path you requested
TARGET_DIR="$HOME/Projects/github.com/$GITHUB_USER/$REPO_SLUG"
echo "πŸ“‚ Target Directory: $TARGET_DIR"
# --- STEP 7: Clone or Pull ---
if [[ -d "$TARGET_DIR" ]]; then
echo "πŸ”„ Directory exists. Pulling latest changes..."
git -C "$TARGET_DIR" pull
else
echo "⬇️ Cloning $REPO_NAME..."
# gh repo clone handles creating the directory structure if it doesn't exist
gh repo clone "$REPO_NAME" "$TARGET_DIR"
fi
# --- STEP 8: Handover to Private Script ---
# Looks for 'setup' (no extension)
SETUP_SCRIPT="$TARGET_DIR/setup"
if [[ -f "$SETUP_SCRIPT" ]]; then
echo "πŸš€ Handing over to private setup script..."
chmod +x "$SETUP_SCRIPT"
"$SETUP_SCRIPT"
else
echo "❌ Error: 'setup' script not found in $TARGET_DIR"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment