devin15/cursor2api-rust
0
1#!/bin/bash2 3# 设置错误时退出4set -e5 6# 颜色输出7RED='\033[0;31m'8GREEN='\033[0;32m'9BLUE='\033[0;34m'10NC='\033[0m' # No Color11 12info() {13 echo -e "${BLUE}[INFO] $1${NC}"14}15 16error() {17 echo -e "${RED}[ERROR] $1${NC}"18 exit 119}20 21# 检查是否为 root 用户(FreeBSD 和 Linux)22if [ "$(uname)" != "Darwin" ] && [ "$EUID" -ne 0 ]; then23 error "请使用 root 权限运行此脚本 (sudo ./setup.sh)"24fi25 26# 检测包管理器27if command -v brew &> /dev/null; then28 PKG_MANAGER="brew"29 info "检测到 macOS/Homebrew 系统"30elif command -v pkg &> /dev/null; then31 PKG_MANAGER="pkg"32 info "检测到 FreeBSD 系统"33elif command -v apt-get &> /dev/null; then34 PKG_MANAGER="apt-get"35 info "检测到 Debian/Ubuntu 系统"36elif command -v dnf &> /dev/null; then37 PKG_MANAGER="dnf"38 info "检测到 Fedora/RHEL 系统"39elif command -v yum &> /dev/null; then40 PKG_MANAGER="yum"41 info "检测到 CentOS 系统"42else43 error "未检测到支持的包管理器"44fi45 46# 更新包管理器缓存47info "更新包管理器缓存..."48case $PKG_MANAGER in49 "brew")50 brew update51 ;;52 "pkg")53 pkg update54 ;;55 *)56 $PKG_MANAGER update -y57 ;;58esac59 60# 安装基础构建工具61info "安装基础构建工具..."62case $PKG_MANAGER in63 "brew")64 brew install \65 protobuf \66 pkg-config \67 openssl \68 curl \69 git \70 node71 ;;72 "pkg")73 pkg install -y \74 gmake \75 protobuf \76 pkgconf \77 openssl \78 curl \79 git \80 node81 ;;82 "apt-get")83 $PKG_MANAGER install -y --no-install-recommends \84 build-essential \85 protobuf-compiler \86 pkg-config \87 libssl-dev \88 ca-certificates \89 curl \90 tzdata \91 git92 ;;93 *)94 $PKG_MANAGER install -y \95 gcc \96 gcc-c++ \97 make \98 protobuf-compiler \99 pkg-config \100 openssl-devel \101 ca-certificates \102 curl \103 tzdata \104 git105 ;;106esac107 108# 安装 Node.js 和 npm(如果还没有通过包管理器安装)109if ! command -v node &> /dev/null && [ "$PKG_MANAGER" != "brew" ] && [ "$PKG_MANAGER" != "pkg" ]; then110 info "安装 Node.js 和 npm..."111 if [ "$PKG_MANAGER" = "apt-get" ]; then112 curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -113 $PKG_MANAGER install -y nodejs114 else115 curl -fsSL https://rpm.nodesource.com/setup_lts.x | bash -116 $PKG_MANAGER install -y nodejs117 fi118fi119 120# 安装 Rust(如果未安装)121if ! command -v rustc &> /dev/null; then122 info "安装 Rust..."123 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y124 . "$HOME/.cargo/env"125fi126 127# 添加目标平台128info "添加 Rust 目标平台..."129case "$(uname)" in130 "FreeBSD")131 rustup target add x86_64-unknown-freebsd132 ;;133 "Darwin")134 rustup target add x86_64-apple-darwin aarch64-apple-darwin135 ;;136 *)137 rustup target add x86_64-unknown-linux-gnu138 ;;139esac140 141# 清理包管理器缓存142case $PKG_MANAGER in143 "apt-get")144 rm -rf /var/lib/apt/lists/*145 ;;146 "pkg")147 pkg clean -y148 ;;149esac150 151# 设置时区(除了 macOS)152if [ "$(uname)" != "Darwin" ]; then153 info "设置时区为 Asia/Shanghai..."154 ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime155fi156 157echo -e "${GREEN}安装完成!${NC}"