diff --git a/setup.sh b/setup.sh index 19539b1..aa06157 100755 --- a/setup.sh +++ b/setup.sh @@ -83,6 +83,138 @@ get_tool_cmd() { eval echo \"\$TOOL_CMDS_$1\"; } get_tool_pkg() { eval echo \"\$TOOL_PKGS_$1\"; } get_tool_url() { eval echo \"\$TOOL_URLS_$1\"; } +# ========== 检测系统包管理器 ========== +get_sys_pkg_manager() { + if command_exists apt-get; then + echo "apt" + elif command_exists yum; then + echo "yum" + elif command_exists dnf; then + echo "dnf" + elif command_exists apk; then + echo "apk" + elif command_exists brew; then + echo "brew" + elif command_exists pacman; then + echo "pacman" + else + echo "" + fi +} + +# ========== 安装系统依赖 ========== +install_sys_dep() { + local dep=$1 + local pkg_mgr=$(get_sys_pkg_manager) + + if [[ -z "$pkg_mgr" ]]; then + error "无法检测系统包管理器,请手动安装 $dep" + return 1 + fi + + info "正在安装 $dep..." + + case "$pkg_mgr" in + apt) + apt-get update -qq && apt-get install -y "$dep" + ;; + yum) + yum install -y "$dep" + ;; + dnf) + dnf install -y "$dep" + ;; + apk) + apk add "$dep" + ;; + brew) + brew install "$dep" + ;; + pacman) + pacman -S --noconfirm "$dep" + ;; + esac + + if command_exists "$dep"; then + success "$dep 安装成功!" + return 0 + else + error "$dep 安装失败" + return 1 + fi +} + +# ========== 检测并安装系统依赖 ========== +check_system_deps() { + local missing_deps=() + + # 检测 curl + if ! command_exists curl; then + missing_deps+=("curl") + fi + + # 检测 unzip (bun 安装需要) + if ! command_exists unzip; then + missing_deps+=("unzip") + fi + + # 检测 jq (Claude Code 配置需要) + if ! command_exists jq; then + missing_deps+=("jq") + fi + + if [[ ${#missing_deps[@]} -eq 0 ]]; then + return 0 + fi + + echo "" + warning "检测到缺少以下系统依赖: ${missing_deps[*]}" + echo "" + + read -p "是否自动安装这些依赖? (Y/n) " -n 1 -r + echo "" + + if [[ $REPLY =~ ^[Nn]$ ]]; then + error "请手动安装依赖后重新运行脚本" + echo "" + local pkg_mgr=$(get_sys_pkg_manager) + case "$pkg_mgr" in + apt) + echo -e " ${CYAN}apt-get update && apt-get install -y ${missing_deps[*]}${NC}" + ;; + yum) + echo -e " ${CYAN}yum install -y ${missing_deps[*]}${NC}" + ;; + dnf) + echo -e " ${CYAN}dnf install -y ${missing_deps[*]}${NC}" + ;; + apk) + echo -e " ${CYAN}apk add ${missing_deps[*]}${NC}" + ;; + brew) + echo -e " ${CYAN}brew install ${missing_deps[*]}${NC}" + ;; + pacman) + echo -e " ${CYAN}pacman -S ${missing_deps[*]}${NC}" + ;; + *) + echo -e " 请使用系统包管理器安装: ${missing_deps[*]}" + ;; + esac + echo "" + return 1 + fi + + # 自动安装依赖 + for dep in "${missing_deps[@]}"; do + if ! install_sys_dep "$dep"; then + return 1 + fi + done + + return 0 +} + # ========== 帮助信息 ========== show_help() { cat << 'EOF' @@ -131,18 +263,6 @@ install_bun() { warning "未检测到 Node.js 或 Bun" info "推荐安装 Bun(轻量级 JavaScript 运行时)" echo "" - - # 检测 unzip 依赖 - if ! command_exists unzip; then - error "安装 Bun 需要 unzip,请先安装:" - echo "" - echo -e " Ubuntu/Debian: ${CYAN}sudo apt-get install unzip${NC}" - echo -e " Alpine: ${CYAN}apk add unzip${NC}" - echo -e " CentOS/RHEL: ${CYAN}sudo yum install unzip${NC}" - echo "" - return 1 - fi - echo -e " 安装命令: ${CYAN}curl -fsSL https://bun.sh/install | bash${NC}" echo "" @@ -281,6 +401,11 @@ main() { esac done + # 检测并安装系统依赖 (curl, unzip, jq) + if ! check_system_deps; then + exit 1 + fi + # 显示状态 show_status