206 lines
5.8 KiB
Bash
Executable File
Raw Permalink 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.

#!/bin/bash
#
# AOSP 编译错误分析 Agent — 一键安装脚本
#
# 用法:
# ./install.sh # 交互式安装
# ./install.sh --token <pat> # 自动配置 token
# ./install.sh --binary ./analyze # 使用本地二进制
# ./install.sh --url https://xxx/analyze # 从 URL 下载
#
set -e
# ── 默认配置 ──
INSTALL_DIR="/usr/local/bin"
BINARY_NAME="analyze"
CONFIG_FILE="$HOME/.aosp-analyzer.env"
# 颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# ── 参数解析 ──
TOKEN=""
BINARY_PATH=""
DOWNLOAD_URL=""
while [[ $# -gt 0 ]]; do
case $1 in
--token) TOKEN="$2"; shift 2 ;;
--binary) BINARY_PATH="$2"; shift 2 ;;
--url) DOWNLOAD_URL="$2"; shift 2 ;;
--dir) INSTALL_DIR="$2"; shift 2 ;;
-h|--help)
echo "用法: install.sh [选项]"
echo ""
echo "选项:"
echo " --token <key> 火山方舟 API Key(ARK_API_KEY)"
echo " --binary <path> 本地二进制文件路径"
echo " --url <url> 下载二进制文件的 URL"
echo " --dir <path> 安装目录 (默认: /usr/local/bin)"
echo " -h, --help 显示帮助"
exit 0
;;
*) echo "未知参数: $1"; exit 1 ;;
esac
done
# ── 辅助函数 ──
info() { echo -e "${BLUE}ℹ${NC} $1"; }
ok() { echo -e "${GREEN}✅${NC} $1"; }
warn() { echo -e "${YELLOW}⚠${NC} $1"; }
err() { echo -e "${RED}❌${NC} $1"; exit 1; }
# ── 安装流程 ──
echo ""
echo "========================================"
echo " AOSP 编译错误分析 Agent — 安装向导"
echo "========================================"
echo ""
# Step 1: 获取二进制文件
TARGET="${INSTALL_DIR}/${BINARY_NAME}"
if [[ -n "$BINARY_PATH" ]]; then
# 从本地文件复制
info "从本地文件安装: ${BINARY_PATH}"
if [[ ! -f "$BINARY_PATH" ]]; then
err "文件不存在: ${BINARY_PATH}(当前目录: $(pwd))
提示: 构建产物在 dist/ 下 —— Linux 用 ./dist/analyze,macOS 用 ./dist/analyze-darwin" 1>&2
fi
mkdir -p "$INSTALL_DIR"
cp "$BINARY_PATH" "$TARGET"
chmod +x "$TARGET"
elif [[ -n "$DOWNLOAD_URL" ]]; then
# 从 URL 下载
info "下载二进制: ${DOWNLOAD_URL}"
mkdir -p "$INSTALL_DIR"
if command -v curl &>/dev/null; then
curl -fSL "$DOWNLOAD_URL" -o "$TARGET"
elif command -v wget &>/dev/null; then
wget -q "$DOWNLOAD_URL" -O "$TARGET"
else
err "需要 curl 或 wget"
fi
chmod +x "$TARGET"
else
# 交互选择
echo " 请选择安装方式:"
echo " 1) 从本地文件复制"
echo " 2) 从 URL 下载"
echo ""
read -p " 选择 [1/2]: " choice < /dev/tty
case $choice in
1)
read -p " 二进制文件路径: " BINARY_PATH < /dev/tty
if [[ ! -f "$BINARY_PATH" ]]; then
err "文件不存在: ${BINARY_PATH}(当前目录: $(pwd))
提示: 构建产物在 dist/ 下 —— Linux 用 ./dist/analyze,macOS 用 ./dist/analyze-darwin" 1>&2
fi
mkdir -p "$INSTALL_DIR"
cp "$BINARY_PATH" "$TARGET"
chmod +x "$TARGET"
;;
2)
read -p " 下载 URL: " DOWNLOAD_URL < /dev/tty
[[ -n "$DOWNLOAD_URL" ]] || err "URL 不能为空"
mkdir -p "$INSTALL_DIR"
curl -fSL "$DOWNLOAD_URL" -o "$TARGET" 2>/dev/null || \
wget -q "$DOWNLOAD_URL" -O "$TARGET"
chmod +x "$TARGET"
;;
*)
err "无效选择"
;;
esac
fi
# 平台不匹配警告(防止在 macOS 上误装 Linux ELF)
if command -v file &>/dev/null; then
if file "$TARGET" | grep -q "ELF" && [[ "$(uname)" != "Linux" ]]; then
warn "这是 Linux 二进制,但当前系统是 $(uname),本机无法运行"
echo " 本机使用请安装 macOS 版: ./scripts/install.sh --binary ./dist/analyze-darwin ..."
fi
fi
ok "二进制已安装到: ${TARGET}"
# Step 2: 配置 API Key
if [[ -n "$TOKEN" ]]; then
info "配置火山方舟 API Key..."
cat > "$CONFIG_FILE" << EOF
# AOSP 编译错误分析 Agent 配置
# 由 install.sh 自动生成
export ARK_API_KEY="${TOKEN}"
EOF
chmod 600 "$CONFIG_FILE"
ok "API Key 已保存到: ${CONFIG_FILE}"
elif [[ -z "${ARK_API_KEY:-}" ]]; then
echo ""
info "配置火山方舟 API Key"
echo " 获取 API Key: https://console.volcengine.com/ark/region:cn-beijing/apikey"
echo ""
read -p " 请输入 ARK_API_KEY (留空跳过): " INPUT_TOKEN < /dev/tty
if [[ -n "$INPUT_TOKEN" ]]; then
cat > "$CONFIG_FILE" << EOF
# AOSP 编译错误分析 Agent 配置
export ARK_API_KEY="${INPUT_TOKEN}"
EOF
chmod 600 "$CONFIG_FILE"
ok "API Key 已保存到: ${CONFIG_FILE}"
else
warn "未配置 API Key,使用前请手动设置:"
echo " export ARK_API_KEY="<your-key>""
fi
else
ok "检测到环境变量中已有 ARK_API_KEY"
fi
# Step 3: 配置 Shell 自动加载
SHELL_RC=""
if [[ -f "$HOME/.zshrc" ]]; then
SHELL_RC="$HOME/.zshrc"
elif [[ -f "$HOME/.bashrc" ]]; then
SHELL_RC="$HOME/.bashrc"
fi
if [[ -n "$SHELL_RC" && -f "$CONFIG_FILE" ]]; then
SOURCE_LINE="source ${CONFIG_FILE}"
if ! grep -qF "$SOURCE_LINE" "$SHELL_RC" 2>/dev/null; then
echo "" >> "$SHELL_RC"
echo "# AOSP 编译错误分析 Agent" >> "$SHELL_RC"
echo "$SOURCE_LINE" >> "$SHELL_RC"
ok "已添加自动加载到: ${SHELL_RC}"
fi
fi
# Step 4: 验证安装
echo ""
echo "========================================"
echo " 安装完成"
echo "========================================"
echo ""
echo " 二进制: ${TARGET}"
echo " 配置: ${CONFIG_FILE:-未配置}"
echo " 依赖: 无(仅需网络访问 ark.cn-beijing.volces.com)"
echo ""
echo " 使用方式:"
echo " source ${CONFIG_FILE:-~/.bashrc} # 加载环境变量"
echo " cd /path/to/aosp"
echo " m 2>&1 | tee build.log"
echo " analyze build.log"
echo ""
echo " 卸载方式:"
echo " curl -fsSL .../uninstall.sh | bash -s --"
echo ""