Skip to content

Instantly share code, notes, and snippets.

@TKLCZ
Forked from jamiew/brew-sync.sh
Last active May 30, 2023 19:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TKLCZ/dc68f225afef1f3f192d63737af886a1 to your computer and use it in GitHub Desktop.
Save TKLCZ/dc68f225afef1f3f192d63737af886a1 to your computer and use it in GitHub Desktop.
Upgrade and sync your Homebrew installations between Macs (Intel and Silicon M1/M2) via iCloud (including casks). You only need to chmod +x and run.
#!/bin/bash
# Sync Homebrew installations between Macs via iCloud
# Working on Intel and Silicon Macs
# Target folder on iCloud
TARGET="Homebrew"
# Homebrew binary path by platform
if [[ $(uname -m) == 'arm64' ]]; then
BREW="/opt/homebrew/bin/brew"
else
BREW="/usr/local/bin/brew"
fi
# Path to target folder
ICLOUD="$HOME/Library/Mobile Documents/com~apple~CloudDocs/$TARGET"
# Check if the target folder exists. If not, then will be created.
if [ ! -d "$ICLOUD" ]; then
mkdir "$ICLOUD"
fi
# upgrade first
$BREW upgrade
# first get local settings
echo "Reading local settings ..."
rm -f /tmp/brew-sync.*
$BREW tap > /tmp/brew-sync.taps
$BREW list > /tmp/brew-sync.installed
$BREW list -1 --cask > /tmp/brew-sync.casks
# then combine it with list in iCloud
echo "Reading settings from iCloud ..."
[ -e "$ICLOUD"/brew-sync.taps ] && cat "$ICLOUD"/brew-sync.taps >> /tmp/brew-sync.taps
[ -e "$ICLOUD"/brew-sync.installed ] && cat "$ICLOUD"/brew-sync.installed >> /tmp/brew-sync.installed
[ -e "$ICLOUD"/brew-sync.casks ] && cat "$ICLOUD"/brew-sync.casks >> /tmp/brew-sync.casks
# make the lists unique and sync into iCloud
echo "Syncing to iCloud ..."
mkdir -p "$ICLOUD"
cat /tmp/brew-sync.taps | sort | uniq > "$ICLOUD"/brew-sync.taps
cat /tmp/brew-sync.installed | sort | uniq > "$ICLOUD"/brew-sync.installed
cat /tmp/brew-sync.casks | sort | uniq > "$ICLOUD"/brew-sync.casks
# Set taps
echo "Enabling taps ..."
for TAP in `cat "$ICLOUD"/brew-sync.taps`; do
$BREW tap ${TAP} >/dev/null
done
# Install missing Homebrew packages
echo "Install missing packages ..."
for PACKAGE in `cat "$ICLOUD"/brew-sync.installed`; do
$BREW list ${PACKAGE} >/dev/null
[ "$?" != "0" ] && $BREW install ${PACKAGE}
done
echo "Install missing casks ..."
for CASK in `cat "$ICLOUD"/brew-sync.casks`; do
$BREW list -1 --cask ${CASK} >/dev/null
[ "$?" != "0" ] && $BREW install --cask ${CASK}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment