Files
script/homebrew/brew-upgrade-manager.sh
Orion 15d99f33ce fix(core): 🩹 更新 Homebrew Cask 升级命令
将 GUI 软件更新方式从使用第三方 brew-cu 切换为官方原生的 brew upgrade --cask 命令,并增加贪婪模式与强制参数以提升更新覆盖率。

- 替换 brew cu -yaq 为 brew upgrade --cask --greedy --force
- 同步更新控制台输出提示信息
2026-05-06 09:02:30 +08:00

150 lines
4.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# Homebrew 智能升级脚本(强制 Cask 更新增强版 v5.3 - 移除 sudo 密码注入)
# ================== 脚本环境设置 ==================
# set -e当命令返回非零退出状态表示失败脚本会立即退出。
set -e
# set -o pipefail在管道命令中如果任何一个子命令失败整个管道即为失败。
set -o pipefail
# --- 颜色定义 (自动检测终端是否支持) ---
if [ -t 1 ]; then
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
CYAN='\033[1;36m'
NC='\033[0m'
else
GREEN=''
YELLOW=''
BLUE=''
CYAN=''
NC=''
fi
# --- 终端宽度和打印函数 ---
DEFAULT_FALLBACK_WIDTH="130"
TERMINAL_WIDTH_OVERRIDE=""
# 解析命令行参数
while [[ $# -gt 0 ]]; do
case "$1" in
--width)
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then
TERMINAL_WIDTH_OVERRIDE="$2"
shift 2
else
echo -e "${YELLOW}Error: '--width' parameter requires a valid numeric value.${NC}"
exit 1
fi
;;
--width=*)
TERMINAL_WIDTH_OVERRIDE="${1#*=}"
if ! [[ "$TERMINAL_WIDTH_OVERRIDE" =~ ^[0-9]+$ ]]; then
echo -e "${YELLOW}Error: '--width' parameter requires a valid numeric value.${NC}"
exit 1
fi
shift
;;
*)
shift
;;
esac
done
FIXED_TERMINAL_WIDTH=""
if [[ -n "$TERMINAL_WIDTH_OVERRIDE" ]]; then
FIXED_TERMINAL_WIDTH="$TERMINAL_WIDTH_OVERRIDE"
elif [[ -n "$HB_TERMINAL_WIDTH" && "$HB_TERMINAL_WIDTH" =~ ^[0-9]+$ ]]; then
FIXED_TERMINAL_WIDTH="$HB_TERMINAL_WIDTH"
fi
terminal_width() {
local width=""
if [[ -n "$FIXED_TERMINAL_WIDTH" ]]; then
printf "%s" "$FIXED_TERMINAL_WIDTH"
return
fi
if command -v stty &>/dev/null && stty size &>/dev/null; then
width=$(stty size 2>/dev/null | awk '{print $2}')
fi
if [[ -z "$width" || "$width" -le 0 ]]; then
width=$(tput cols 2>/dev/null || echo "$DEFAULT_FALLBACK_WIDTH")
fi
if [[ -z "$width" || "$width" -le 0 ]]; then
width="$DEFAULT_FALLBACK_WIDTH"
fi
printf "%s" "$width"
}
separator() { local width; width=$(terminal_width); printf '=%.0s' $(seq 1 "$width"); printf "\n"; }
print_header() { echo -e "${BLUE}$1${NC}"; }
# ================== 流程开始 ==================
separator
print_header "Step 1: Updating Homebrew repositories (brew update -v)"
brew update -v
separator
printf "\n"
separator
print_header "Step 2: Performing health check (brew doctor)"
if ! brew doctor; then
echo -e "${YELLOW}Warning: 'brew doctor' detected issues. Manual review and resolution are recommended.${NC}"
else
echo "Homebrew environment is in good health."
fi
separator
printf "\n"
separator
print_header "Step 3: Verifying brew-cu extension for GUI Apps"
if ! brew tap | grep -q "buo/cask-upgrade"; then
echo -e "${YELLOW}Extension 'brew-cu' not found. Installing now...${NC}"
brew tap buo/cask-upgrade
else
echo -e "${GREEN}Extension 'brew-cu' is already active.${NC}"
fi
separator
printf "\n"
separator
print_header "Step 4: Executing comprehensive upgrades (Formulae & Casks)"
echo -e "${NC}"
# 1. 升级命令行工具 (Formulae)
echo -e "\n${CYAN}>>> [1/2] Upgrading CLI Formulae (brew upgrade --formula)...${NC}"
brew upgrade --formula
# 2. 升级图形界面软件 (Casks)
echo -e "\n${CYAN}>>> [2/2] Upgrading GUI Casks (brew upgrade --cask --greedy --force)...${NC}"
# 强制注入环境变量,确保终端输出依然保留 ANSI 颜色格式
export HOMEBREW_COLOR=1
# --- 终端宽度处理 ---
# 交互式终端里让 brew 直接读取当前 TTY避免 sudo 时间戳因新建 PTY 失效。
if [[ -n "$FIXED_TERMINAL_WIDTH" || ! -t 1 ]]; then
export COLUMNS
COLUMNS="$(terminal_width)"
else
unset COLUMNS
fi
brew upgrade --cask --greedy --force
separator
printf "\n"
separator
print_header "Step 5: Cleaning up old files and caches (brew cleanup --prune=all)"
brew cleanup --prune=all
separator
printf "\n"
echo -e "${GREEN}All operations completed!${NC}"
printf "\n"