#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" BLUE='\033[0;34m' GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' have_cmd() { command -v "$1" >/dev/null 2>&1 } ensure_local_bin_on_path() { export PATH="$HOME/.local/bin:$PATH" export PATH="$HOME/.bun/bin:$PATH" } install_uv_if_needed() { if have_cmd uv; then echo -e "${GREEN}uv 已存在: $(command -v uv)${NC}" return fi echo -e "${BLUE}安装 uv...${NC}" curl -LsSf https://astral.sh/uv/install.sh | sh ensure_local_bin_on_path if ! have_cmd uv; then echo -e "${RED}uv 安装失败,请手动检查 ~/.local/bin 是否加入 PATH${NC}" exit 1 fi } install_bun_if_needed() { if have_cmd bun; then echo -e "${GREEN}bun 已存在: $(command -v bun)${NC}" return fi echo -e "${BLUE}安装 bun...${NC}" curl -fsSL https://bun.sh/install | bash ensure_local_bin_on_path if ! have_cmd bun; then echo -e "${RED}bun 安装失败,请手动检查 ~/.bun/bin 是否加入 PATH${NC}" exit 1 fi } ensure_python_alias() { mkdir -p "$HOME/.local/bin" if [ ! -e "$HOME/.local/bin/python" ]; then ln -sf /usr/bin/python3 "$HOME/.local/bin/python" fi ensure_local_bin_on_path } sync_python_env() { echo -e "${BLUE}同步 Python 依赖...${NC}" cd "$ROOT_DIR" uv python install 3.14 uv sync --frozen --group dev } sync_frontend_env() { echo -e "${BLUE}同步前端依赖...${NC}" cd "$ROOT_DIR/frontend" bun install } ensure_env_files() { echo -e "${BLUE}检查环境变量模板...${NC}" if [ ! -f "$ROOT_DIR/backend/.env" ] && [ -f "$ROOT_DIR/backend/.env.example" ]; then cp "$ROOT_DIR/backend/.env.example" "$ROOT_DIR/backend/.env" echo -e "${YELLOW}已创建 backend/.env,请按需修改 SECRET_KEY / 数据库 / AI 服务配置${NC}" fi if [ ! -f "$ROOT_DIR/aiprovider/.env" ] && [ -f "$ROOT_DIR/aiprovider/.env.example" ]; then cp "$ROOT_DIR/aiprovider/.env.example" "$ROOT_DIR/aiprovider/.env" echo -e "${YELLOW}已创建 aiprovider/.env,请按需修改 provider/model/api key${NC}" fi if [ ! -f "$ROOT_DIR/frontend/.env.local" ] && [ -f "$ROOT_DIR/frontend/.env.example" ]; then cp "$ROOT_DIR/frontend/.env.example" "$ROOT_DIR/frontend/.env.local" echo -e "${YELLOW}已创建 frontend/.env.local,可按需覆盖 VITE_API_URL / VITE_WS_URL${NC}" fi } report_optional_tools() { if have_cmd docker; then echo -e "${GREEN}docker 已存在: $(command -v docker)${NC}" else echo -e "${YELLOW}未检测到 docker。如需容器方式启动 backend/aiprovider,请安装 Docker。${NC}" fi } print_next_steps() { echo "" echo -e "${GREEN}开发环境引导完成${NC}" echo "下一步建议:" echo " 1. cd \"$ROOT_DIR\"" echo " 2. uv run pytest backend/tests/test_api.py -q -s" echo " 3. cd \"$ROOT_DIR/frontend\" && bun run build" echo " 4. cd \"$ROOT_DIR\" && ./planet.sh start" echo "" echo "前端统一使用 Bun,不使用 npm/pnpm/yarn。" } main() { ensure_local_bin_on_path install_uv_if_needed install_bun_if_needed ensure_python_alias sync_python_env sync_frontend_env ensure_env_files report_optional_tools print_next_steps } main "$@"