Files
cmbuyer/init.sh

89 lines
3.5 KiB
Bash

#!/usr/bin/env bash
# cmbuyer Unix shell 统一安装、验证与启动入口。
# 本脚本与 init.ps1 保持同一门禁语义;在 WSL 缺少 Windows 侧工具链时必须明确失败,
# 不得将跳过或 bash 语法通过误报为 Windows 产品验收。
set -euo pipefail
cd "$(dirname "$0")"
export GOTOOLCHAIN=local
fail() {
echo "错误:$*" >&2
exit 1
}
require_directory() {
local path="$1"
local name="$2"
[[ -d "$path" ]] || fail "$name 目录不存在:$path。请先完成对应初始化任务。"
}
require_file() {
local path="$1"
local name="$2"
[[ -f "$path" ]] || fail "$name 不存在:$path。请先完成对应初始化任务。"
}
require_command() {
local name="$1"
local hint="$2"
command -v "$name" >/dev/null 2>&1 || fail "缺少 $name。$hint"
}
run_step() {
local description="$1"
shift
echo "==> $description"
"$@" || fail "$description 失败。"
}
confirm_python311_or_newer() {
local executable="$1"
local context="$2"
"$executable" -c 'import sys; raise SystemExit(0 if sys.version_info >= (3, 11) else 1)' \
|| fail "$context 必须使用 Python 3.11+。不会自动覆盖既有虚拟环境;请手工删除 client/.venv 后重试。"
}
echo "==> 当前目录: $(pwd)"
require_directory admin "采购服务 admin"
require_file admin/go.mod "采购服务哨兵 admin/go.mod"
require_directory client "采购工具 client"
require_file client/requirements.txt "采购工具哨兵 client/requirements.txt"
require_file client/pyproject.toml "采购工具打包配置 client/pyproject.toml"
require_command go "请安装 Go 1.23+ 后重试。"
client_python="client/.venv/bin/python"
if [[ -e client/.venv ]]; then
[[ -d client/.venv ]] || fail "client/.venv 不是目录。不会覆盖该路径,请人工处理后重试。"
require_file "$client_python" "既有 client/.venv Python"
confirm_python311_or_newer "$client_python" "既有 client/.venv"
else
require_command python3 "请安装 Python 3.11+ 后重试。"
confirm_python311_or_newer python3 "python3"
run_step "创建 Python 3.11+ 采购工具虚拟环境" python3 -m venv client/.venv
require_file "$client_python" "新建 client/.venv Python"
confirm_python311_or_newer "$client_python" "新建 client/.venv"
fi
run_step "验证采购工具虚拟环境 Python" "$client_python" -c 'import sys; print(sys.version); raise SystemExit(0 if sys.version_info >= (3, 11) else 1)'
( cd admin && run_step "采购服务:admin/ 同步依赖" go mod download )
( cd admin && run_step "采购服务:admin/ 测试" go test ./... )
( cd admin && run_step "采购服务:admin/ 静态检查" go vet ./... )
( cd admin && run_step "采购服务:admin/ 构建" go build ./... )
( cd client && run_step "采购工具:以 editable 方式安装" .venv/bin/python -m pip install -e . )
( cd client && run_step "采购工具:验证已安装包" .venv/bin/python -c 'import cmbuyer_client; print(cmbuyer_client.__version__)' )
( cd client && run_step "采购工具:测试" .venv/bin/python -m unittest discover -s tests -t . )
( cd client && run_step "采购工具:语法检查" .venv/bin/python -m compileall -q src tests scripts )
run_step "校验 agent 上下文清单" "$client_python" scripts/validate_agent_context.py
echo
echo "==> 基线验证通过。启动命令:"
echo " 采购服务:cd admin && go run ./cmd/server"
echo " 采购工具:cd client && .venv/bin/python -m cmbuyer_client"
echo
echo "WSL / Unix shell 仅在本机工具链齐备时能通过;这不构成 Windows 或真机验收。"