634487b9d8
lib/brew.sh:
Set MAS_NO_AUTO_INDEX=1 before brew bundle install to suppress the
per-app mdimport call that mas otherwise runs after each App Store
installation. Run one mdimport /Applications batch pass afterwards.
lib/restore-state.sh (new):
Lightweight key=value state file (~/.local/state/ssr/restore-state).
ssr::rs::init / ssr::rs::run / ssr::rs::set/get / ssr::rs::error /
ssr::rs::report — tracks step outcomes and accumulated errors.
bin/ssr-restore:
--resume flag: skip steps already marked ok in state file.
Each step (brew_install, brew_restore, mackup_restore, macos_defaults,
prefs_import) wrapped in ssr::rs::run; errors accumulate without abort.
Formatted report printed at end of every run.
bin/ssr-status:
ssr status restore — full restore step report + error log
ssr status backup — Brewfile lines, unmanaged apps, prefs snapshot
ssr status — general view now shows one-line last-restore
summary and hint to run 'ssr status restore'
Co-authored-by: Cursor <cursoragent@cursor.com>
88 lines
3.3 KiB
Bash
88 lines
3.3 KiB
Bash
#!/usr/bin/env bash
|
|
# lib/brew.sh — Homebrew install + Brewfile dump/restore.
|
|
|
|
ssr::brew::ensure_installed() {
|
|
if command -v brew >/dev/null 2>&1; then
|
|
return 0
|
|
fi
|
|
|
|
# Homebrew's install script requires sudo on macOS to write to /opt/homebrew
|
|
# (Apple Silicon) or /usr/local (Intel). Check before running it so we can
|
|
# give a clear error instead of a cryptic failure.
|
|
if ! sudo -n true 2>/dev/null; then
|
|
ssr::warn "Homebrew is not installed and sudo access is required to install it."
|
|
ssr::warn "Please run the following as an admin user and re-run ssr restore:"
|
|
ssr::warn " /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
|
|
ssr::die "Cannot install Homebrew without sudo access."
|
|
fi
|
|
|
|
ssr::log "Homebrew not found — installing"
|
|
# NONINTERACTIVE=1 skips the confirmation prompt; sudo is available (checked above).
|
|
NONINTERACTIVE=1 /bin/bash -c \
|
|
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
|
|
|
|
# Ensure brew is on PATH for both Apple Silicon and Intel layouts.
|
|
if [[ -x /opt/homebrew/bin/brew ]]; then
|
|
eval "$(/opt/homebrew/bin/brew shellenv)"
|
|
elif [[ -x /usr/local/bin/brew ]]; then
|
|
eval "$(/usr/local/bin/brew shellenv)"
|
|
fi
|
|
command -v brew >/dev/null 2>&1 || ssr::die "brew still not on PATH after install"
|
|
}
|
|
|
|
ssr::brew::update() {
|
|
ssr::log "brew update"
|
|
brew update --quiet
|
|
}
|
|
|
|
ssr::brew::upgrade() {
|
|
[[ "${SSR_BREW_UPGRADE:-true}" == "true" ]] || return 0
|
|
ssr::log "brew upgrade"
|
|
brew upgrade --quiet || ssr::warn "brew upgrade reported errors (continuing)"
|
|
}
|
|
|
|
ssr::brew::cleanup() {
|
|
[[ "${SSR_BREW_CLEANUP:-true}" == "true" ]] || return 0
|
|
ssr::log "brew cleanup"
|
|
brew cleanup --quiet || true
|
|
}
|
|
|
|
# Dumps Brewfile to the cloud backup dir with a timestamped rotation copy.
|
|
ssr::brew::dump() {
|
|
local backup_dir="$1" brewfile="$1/Brewfile"
|
|
ssr::ensure_dir "$backup_dir"
|
|
if [[ -f "$brewfile" ]]; then
|
|
cp -f "$brewfile" "$brewfile.bak"
|
|
fi
|
|
ssr::log "brew bundle dump → $brewfile"
|
|
(
|
|
cd "$backup_dir"
|
|
brew bundle dump --describe --quiet --force --file=Brewfile
|
|
)
|
|
ssr::ok "Brewfile written ($(wc -l < "$brewfile" | tr -d ' ') lines)"
|
|
}
|
|
|
|
ssr::brew::restore() {
|
|
local brewfile="$1"
|
|
[[ -f "$brewfile" ]] || ssr::die "Brewfile not found: $brewfile"
|
|
ssr::log "brew bundle install ← $brewfile"
|
|
|
|
# Suppress per-app Spotlight indexing: mas would otherwise run mdimport
|
|
# after every single App Store installation, which adds minutes of latency
|
|
# spread across the run. We do one batch mdimport after bundle completes.
|
|
local rc=0
|
|
MAS_NO_AUTO_INDEX=1 brew bundle install --file="$brewfile" --verbose || rc=$?
|
|
|
|
# Single batch Spotlight index of /Applications after all installs.
|
|
ssr::log "Indexing /Applications in Spotlight (one-time batch after all installs)"
|
|
mdimport /Applications 2>/dev/null || true
|
|
[[ -d "$HOME/Applications" ]] && mdimport "$HOME/Applications" 2>/dev/null || true
|
|
|
|
if [[ $rc -ne 0 ]]; then
|
|
ssr::warn "brew bundle install finished with errors (rc=$rc) — some packages were not installed."
|
|
ssr::warn "Rerun manually: brew bundle install --file=\"$brewfile\""
|
|
else
|
|
ssr::ok "brew bundle install completed successfully."
|
|
fi
|
|
}
|