为了提高脚本的可维护性,简化了 `brew-upgrade-manager.sh` 中的终端宽度逻辑。通过移除复杂的命令行参数解析和动态宽度检测,改为使用固定的 130 列配置,并同步更新了引导脚本以导出环境变量。 - 移除冗余的命令行参数解析逻辑 - 将终端宽度处理逻辑重写为简单的函数 - 强制导出 `COLUMNS` 环境变量以统一输出格式 - 优化了脚本内部执行步骤的视觉分隔效果
110 lines
2.8 KiB
Bash
110 lines
2.8 KiB
Bash
#!/usr/bin/env bash
|
||
# Homebrew 智能升级脚本(强制 Cask 更新增强版 v5.3 - 移除 sudo 密码注入)
|
||
|
||
# ================== 脚本环境设置 ==================
|
||
|
||
# set -e:当命令返回非零退出状态(表示失败)时,脚本会立即退出。
|
||
set -e
|
||
# set -o pipefail:在管道命令中,如果任何一个子命令失败,整个管道即为失败。
|
||
set -o pipefail
|
||
|
||
prepend_path_once() {
|
||
local dir="$1"
|
||
[[ -d "$dir" ]] || return 0
|
||
case ":$PATH:" in
|
||
*":$dir:"*) ;;
|
||
*) PATH="$dir:$PATH" ;;
|
||
esac
|
||
}
|
||
|
||
prepend_path_once "/usr/local/sbin"
|
||
prepend_path_once "/opt/homebrew/sbin"
|
||
export PATH
|
||
|
||
# --- 颜色定义 (自动检测终端是否支持) ---
|
||
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
|
||
|
||
# --- 终端宽度和打印函数 ---
|
||
TERMINAL_WIDTH="130"
|
||
|
||
terminal_width() {
|
||
printf "%s" "$TERMINAL_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
|
||
|
||
# --- 终端宽度处理 ---
|
||
# 固定宽度,避免动态识别终端列数导致 prompt 重绘异常。
|
||
export COLUMNS
|
||
COLUMNS="$(terminal_width)"
|
||
|
||
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"
|