Files
script/homebrew/brew-upgrade-manager.sh
Orion a2924ad7ff refactor(core): ♻️ 移除 Python PTY 转发逻辑并简化更新流程
移除复杂的 Python 脚本模拟终端逻辑,改由 brew 直接读取当前 TTY。此举解决了因新建 PTY 导致的 sudo 时间戳失效问题,并大幅精简了代码结构,提升了维护效率。

- 删除了约 80 行 Python PTY 转发引擎代码
- 优化了更新与清理任务的执行顺序
- 修复了交互式环境下权限验证的潜在冲突
2026-05-06 08:55:39 +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 cu -yaq)...${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 cu -yaq
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"