.local-bin/scripts/update_brew_lists.sh
Jeremie Fraeys f28146ceed
feat(scripts): update existing utility scripts
Update and improve utility scripts:
- manwhere: update man page search functionality
- scripts/promodoro.sh: improve pomodoro timer features
- scripts/setup_dev_env.sh: enhance development environment setup
- scripts/update_app_theme.sh: update application theme switching
- scripts/update_brew_lists.sh: improve brew list update logic
2026-03-28 18:33:51 -04:00

136 lines
2.9 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
# -----------------------------
# Config
# -----------------------------
BREW_PREFIX="${HOMEBREW_PREFIX:-/opt/homebrew}"
BREW_CMD="$BREW_PREFIX/bin/brew"
# Default directories
DEFAULT_LIST_DIR="$HOME/.local/bin/.brew_lists"
BREW_LIST_DIR="$DEFAULT_LIST_DIR"
CASK_LIST_DIR="$DEFAULT_LIST_DIR"
BREW_LIST="$BREW_LIST_DIR/brew_list.txt"
CASK_LIST="$CASK_LIST_DIR/cask_list.txt"
NEW_BREW_LIST="$BREW_LIST_DIR/brew_list.new.txt"
NEW_CASK_LIST="$CASK_LIST_DIR/cask_list.new.txt"
# -----------------------------
# Functions
# -----------------------------
# Check if two files differ
lists_differ() {
[ ! -f "$1" ] || [ ! -f "$2" ] || ! diff -q "$1" "$2" >/dev/null
}
# Update brew & cask lists
update_lists() {
mkdir -p "$BREW_LIST_DIR" "$CASK_LIST_DIR"
echo "Updating brew list..."
"$BREW_CMD" list --formula >"$NEW_BREW_LIST"
chmod 664 "$NEW_BREW_LIST"
echo "Updating cask list..."
"$BREW_CMD" list --cask >"$NEW_CASK_LIST"
chmod 664 "$NEW_CASK_LIST"
}
# Backup old lists
backup_lists() {
[ -f "$BREW_LIST" ] && cp "$BREW_LIST" "$BREW_LIST_DIR/brew_list.bak.txt"
[ -f "$CASK_LIST" ] && cp "$CASK_LIST" "$CASK_LIST_DIR/cask_list.bak.txt"
}
# Save updated lists
save_lists() {
backup_lists
mv "$NEW_BREW_LIST" "$BREW_LIST"
mv "$NEW_CASK_LIST" "$CASK_LIST"
echo "Updated brew and cask lists saved."
}
# Install Homebrew if missing
install_homebrew() {
if ! [ -x "$BREW_CMD" ]; then
echo "Homebrew not found. Installing..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
}
# Install packages from lists
install_packages() {
install_homebrew
if [ -s "$BREW_LIST" ]; then
echo "Installing brew packages..."
while read -r pkg; do
"$BREW_CMD" install "$pkg"
done <"$BREW_LIST"
fi
if [ -s "$CASK_LIST" ]; then
echo "Installing cask packages..."
while read -r cask; do
"$BREW_CMD" install --cask "$cask"
done <"$CASK_LIST"
fi
echo "Installation complete."
}
# Commit changes to Git
commit_to_git() {
current_dir=$PWD
cd "$HOME/.local/bin" || exit
git pull --rebase
git add "$BREW_LIST" "$CASK_LIST"
git commit -m "Update brew lists" || echo "No changes to commit"
git push origin main
cd "$current_dir" || exit
}
# -----------------------------
# Parse command-line args
# -----------------------------
while [[ $# -gt 0 ]]; do
case "$1" in
--install)
INSTALL_FLAG=true
shift
;;
--brew-dir)
shift
BREW_LIST_DIR="$1"
shift
;;
--cask-dir)
shift
CASK_LIST_DIR="$1"
shift
;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
done
# -----------------------------
# Main flow
# -----------------------------
update_lists
if lists_differ "$NEW_BREW_LIST" "$BREW_LIST" || lists_differ "$NEW_CASK_LIST" "$CASK_LIST"; then
save_lists
commit_to_git
else
echo "No changes in brew or cask lists detected."
fi
# Install packages if requested
if [ "${INSTALL_FLAG:-false}" = true ]; then
install_packages
fi