124 lines
3.3 KiB
Bash
124 lines
3.3 KiB
Bash
#!/usr/bin/env bash
|
|
# Homebrew 智能升级脚本(强制 Cask 更新增强版 v5.4 - 容忍 Cask 瞬时下载失败)
|
|
|
|
# ================== 脚本环境设置 ==================
|
|
|
|
# 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}"; }
|
|
|
|
run_cask_upgrade() {
|
|
local status=0 outdated_casks
|
|
|
|
brew upgrade --cask --greedy --force || status=$?
|
|
if [[ "$status" -eq 0 ]]; then
|
|
return 0
|
|
fi
|
|
|
|
echo -e "${YELLOW}Warning: Cask upgrade reported a non-zero exit code (${status}). Verifying whether outdated casks remain...${NC}"
|
|
|
|
if ! outdated_casks="$(brew outdated --cask --greedy 2>/dev/null)"; then
|
|
echo -e "${YELLOW}Warning: Unable to verify outdated casks after the failed upgrade command.${NC}"
|
|
return "$status"
|
|
fi
|
|
|
|
if [[ -z "$outdated_casks" ]]; then
|
|
echo -e "${YELLOW}Warning: No outdated casks remain. Treating the previous Cask error as transient and continuing.${NC}"
|
|
return 0
|
|
fi
|
|
|
|
echo -e "${YELLOW}The following casks still appear to be outdated:${NC}"
|
|
printf '%s\n' "$outdated_casks"
|
|
return "$status"
|
|
}
|
|
|
|
# ================== 流程开始 ==================
|
|
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: 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)"
|
|
|
|
run_cask_upgrade
|
|
|
|
separator
|
|
printf "\n"
|
|
|
|
separator
|
|
print_header "Step 4: 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"
|