Last active
December 20, 2025 09:41
-
-
Save virajp/c542ff926710ddbd2f65490c900018f5 to your computer and use it in GitHub Desktop.
macOS setup script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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