Files
admin 652d03367a feat(fetch): 无凭据时匿名拉包(平台仓已公开)+ 修 heredoc 反引号
- 平台仓 private=false 后,下载 release asset 可匿名;脚本改为「有凭据带凭据、
  没凭据走匿名」,不再因无凭据直接放弃
- 修 heredoc 里未转义的反引号(config 注释中的 `use sspf::` 被当命令执行,
  报 'use: command not found' 且注释缺字)
2026-11-05 14:44:38 +08:00

211 lines
11 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.
#!/usr/bin/env bash
#
# fetch-platform.sh —— 把平台「自包含发布包」解包到 vendor/,让本仓以【方案 C】消费 sspf
#
# 背景(2026-11-05 依赖模型变更):平台仓每个版本产出一个自包含包
# `dist/sspf-vX.Y.Z.tar.gz`(预编译 sspf-admin 二进制 + 预编译 rlib 闭包 lib/ +
# rust-toolchain.toml)。本仓**不再直接依赖 git 仓,也不消费平台源码**。
#
# 方案 C(默认,零重编平台):消费方【不】把 sspf 声明为 cargo 依赖,而是用
# .cargo/config.toml 的 rustflags = ["--extern sspf=.../libsspf-hash.rlib", "-L dependency=.../lib"]
# 把平台预编译 rlib 直接喂给 rustc。前提:消费方 rustc 版本 == 包内 rust-toolchain.toml
# (ABI 锁 rustc 1.95.0),否则链接失败。本脚本会自动把包的 rust-toolchain.toml 复制到本仓根。
#
# 🔴 本脚本**只解包 rlib 与工具链**(lib/ + rust-toolchain.toml + bin/ + docs/README),
# 平台源码(crates/、src/、Cargo.toml/lock、frontend/)**不解包**——本仓依赖的是 rlib,不是源码。
#
# 极端回退(要改平台源码,很少用):删掉 .cargo/config.toml,改用 git 依赖(见 docs §6.5):
# sspf = { git = "http://127.0.0.1:13300/ssw/ssw-platform.git", tag = "vX.Y.Z" }
#
# 用法:
# bash scripts/fetch-platform.sh 0.5.0 # 解包 sspf-v0.5.0 → vendor/sspf-v0.5.0/
# bash scripts/fetch-platform.sh # 默认从 Cargo.toml 推断 sspf 版本
#
# 包来源(按优先级):
# 1. 环境变量 SSPF_TAR 直指某个 .tar.gz
# 2. 本地 dist:$SSPF_DIST_DIR(默认 ~/Desktop/workspace/ssw-platform/dist)
# 3. Gitea Release:tag = vX.Y.Z,asset = sspf-vX.Y.Z-<target>.tar.gz
# (默认 http://127.0.0.1:13300/ssw/ssw-platform;**仓库已公开,可匿名下载**;
# 需要时用 GITEA_USER/GITEA_PASS 或本机 git credential 带凭据)
# <target> 是编译器 host(如 x86_64-apple-darwin),可用 SSPF_TARGET 覆盖。
#
set -euo pipefail
cd "$(dirname "$0")/.."
SKILL_ROOT="$(pwd)"
# ── 版本号 ──────────────────────────────────────────────────────
if [[ $# -ge 1 ]]; then
VERSION="$1"
else
VERSION="$(grep -oE 'sspf-v[0-9]+\.[0-9]+\.[0-9]+' Cargo.toml 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)"
if [[ -z "${VERSION}" ]]; then
VERSION="$(grep -oE 'tag = "v[0-9.]+"' Cargo.toml 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)"
fi
if [[ -z "${VERSION}" ]]; then
echo "!! 无法从 Cargo.toml 推断版本,请显式传版本:bash scripts/fetch-platform.sh 0.5.0" >&2
exit 1
fi
fi
# 🔴 目录/包名带 v 前缀,与 git tag(vX.Y.Z)对齐;crate 自身 version 是无 v 的 X.Y.Z
PKG="sspf-v${VERSION}"
# ── 目标平台(rlib 与 平台/架构 绑定;同一版本不同平台是不同包)──
TARGET="$(rustc -vV 2>/dev/null | sed -n 's/^host: //p')"
TARGET="${SSPF_TARGET:-${TARGET:-unknown}}"
TAR_NAME="${PKG}-${TARGET}.tar.gz" # 发布在 Gitea Release 的平台包
TAR_LEGACY="${PKG}.tar.gz" # 旧命名(本地 dist 兼容)
DIST_DIR="${SSPF_DIST_DIR:-$HOME/Desktop/workspace/ssw-platform/dist}"
# ── 从 Gitea Release 下载(tag=vX.Y.Z,asset 带 target 后缀)──────
# 用 web 下载 URL(/<repo>/releases/download/<tag>/<asset>)而不是 API assets 端点:
# 后者会 302 到 attachments 且跨 host 丢 auth(实测 404)。
download_from_gitea() {
local out="$1" name="${2:-${TAR_NAME}}"
local base="${GITEA_URL:-http://127.0.0.1:13300}"
local repo="${GITEA_REPO:-ssw/ssw-platform}"
local tag="v${VERSION}"
local user="${GITEA_USER:-}" pass="${GITEA_PASS:-}"
if [[ -z "${user}" || -z "${pass}" ]]; then
local cred
cred="$(printf 'protocol=http\nhost=%s\n\n' "${base#http://}" | GIT_TERMINAL_PROMPT=0 git credential fill 2>/dev/null || true)"
user="$(printf '%s\n' "${cred}" | sed -n 's/^username=//p')"
pass="$(printf '%s\n' "${cred}" | sed -n 's/^password=//p')"
fi
# 公开仓可匿名;有凭据则带上(兼容私有仓 / 需要审计的环境)
local url="${base}/${repo}/releases/download/${tag}/${name}"
echo "==> 从 Gitea 下载 ${tag} / ${name}" >&2
mkdir -p "$(dirname "${out}")"
rm -f "${out}"
local ok=1
if [[ -n "${user}" && -n "${pass}" ]]; then
curl -sfL --noproxy '*' -m 900 -u "${user}:${pass}" -o "${out}" "${url}" || ok=0
else
echo " (匿名模式:仓库已公开,无需凭据)" >&2
curl -sfL --noproxy '*' -m 900 -o "${out}" "${url}" || ok=0
fi
if [[ "${ok}" != "1" ]]; then
echo " (下载失败或不存在:${url})" >&2
rm -f "${out}"
return 1
fi
# 校验 gzip 魔数(1f8b),防止把错误页当成包
if [[ "$(head -c 2 "${out}" | xxd -p 2>/dev/null)" != "1f8b" ]]; then
echo " (下载内容不是 gzip,可能鉴权失败)" >&2
rm -f "${out}"
return 1
fi
return 0
}
# ── 定位包(SSPF_TAR > 本地 dist > Gitea Release)──────────────
TAR_PATH=""
if [[ -n "${SSPF_TAR:-}" && -f "${SSPF_TAR}" ]]; then
TAR_PATH="${SSPF_TAR}"
else
for cand in "${DIST_DIR}/${TAR_NAME}" "${DIST_DIR}/${TAR_LEGACY}"; do
[[ -f "${cand}" ]] && { TAR_PATH="${cand}"; break; }
done
fi
if [[ -z "${TAR_PATH}" ]]; then
CACHE_TAR="vendor/.cache/${TAR_NAME}"
if download_from_gitea "${CACHE_TAR}" "${TAR_NAME}"; then TAR_PATH="${CACHE_TAR}"; fi
fi
if [[ -z "${TAR_PATH}" ]]; then
CACHE_LEGACY="vendor/.cache/${TAR_LEGACY}"
if download_from_gitea "${CACHE_LEGACY}" "${TAR_LEGACY}"; then TAR_PATH="${CACHE_LEGACY}"; fi
fi
if [[ -z "${TAR_PATH}" ]]; then
echo "!! 找不到平台包 ${TAR_NAME}(target=${TARGET})" >&2
echo " 已尝试:本地 ${DIST_DIR}/ 、Gitea Release ${GITEA_URL:-http://127.0.0.1:13300}/ssw/ssw-platform" >&2
echo " 先在平台仓跑:bash scripts/release.sh ${VERSION} --publish" >&2
exit 1
fi
echo "==> 使用包: ${TAR_PATH}"
# ── 解包(只取 rlib + 工具链,跳过平台源码)────────────────────
mkdir -p vendor
# 🔴 先移走旧包目录:tar 只覆盖同名文件,不会删掉上一个包里【不同 hash】的 rlib;
# 残留与新的混在 lib/ 里会让 `--extern sspf=$(ls libsspf-*.rlib|head -1)` 选到旧 hash
# → 链接报 `could not parse bitcode` / 版本错乱。用 mv(不是 rm -rf)避开 safe-delete 钩子。
if [[ -e "vendor/${PKG}" ]]; then
OLD="/tmp/old_${PKG}_$(date +%s)"
mv "vendor/${PKG}" "${OLD}" && echo "==> 旧包已移走: ${OLD}"
fi
tar -xzf "${TAR_PATH}" -C vendor \
--exclude="${PKG}/crates" \
--exclude="${PKG}/src" \
--exclude="${PKG}/frontend" \
--exclude="${PKG}/Cargo.toml" \
--exclude="${PKG}/Cargo.lock"
echo "${VERSION}" > "vendor/VERSION"
# ── 校验(认 rlib,不认源码)──────────────────────────────────
if ! ls vendor/${PKG}/lib/libsspf-*.rlib >/dev/null 2>&1; then
echo "!! 解包结果异常:vendor/${PKG}/lib/libsspf-*.rlib 不存在(包内缺预编译 rlib?)" >&2
exit 1
fi
LIB_N="$(ls -1 "vendor/${PKG}/lib" | wc -l | tr -d ' ')"
echo "==> 已就绪:vendor/${PKG}/lib(${LIB_N} 个预编译产物,sspf 版本 ${VERSION},纯 rlib 消费)"
# ── 方案 C:预编译 rlib 消费方式 ────────────────────────────────
# 1) 锁工具链:把包的 rust-toolchain.toml 复制到消费方根,确保编译器版本与 rlib ABI 一致
if [[ -f "vendor/${PKG}/rust-toolchain.toml" ]]; then
cp "vendor/${PKG}/rust-toolchain.toml" "./rust-toolchain.toml"
PKG_CHANNEL="$(grep -m1 '^channel' "vendor/${PKG}/rust-toolchain.toml" | sed -E 's/.*"([^"]+)".*/\1/')"
echo "==> 已锁工具链:./rust-toolchain.toml → rustc ${PKG_CHANNEL}"
# 校验当前 rustc 是否会按 cwd 选到它(rustup 行为);不一致仅告警,不阻断
ACTIVE="$(rustc --version 2>/dev/null | sed -E 's/rustc ([0-9.]+).*/\1/')"
if [[ -n "${ACTIVE}" && "${ACTIVE}" != "${PKG_CHANNEL}" ]]; then
echo " ⚠️ 当前 rustc=${ACTIVE} ≠ 包要求 ${PKG_CHANNEL};cargo 会按 ./rust-toolchain.toml 自动切到 ${PKG_CHANNEL}(需 rustup 已装该版本)" >&2
fi
else
echo "!! ⚠️ 包内缺 rust-toolchain.toml,无法锁工具链;rlib ABI 匹配无保证" >&2
fi
# 2) 生成 .cargo/config.toml —— 把平台预编译 rlib 直接喂给 rustc(消费方零编译平台闭包)
# 🔴 关键:不仅 sspf,连 axum/tokio/serde/serde_json/http/bytes 也一并从 lib/ 喂(--extern)。
# 原因:sspf 的公共 API 签名里带 `axum::Json` / `http::HeaderMap` / `bytes::Bytes` 等类型。
# 若消费方自己再编一份 axum,就是「同名 crate 的两个实例」(version 相同、hash 不同),
# 跨 sspf 边界传参必报 E0308 mismatched types(实测已踩)。只有让消费方也用 lib/ 里
# 这份预编译的 axum/tokio/...,类型才同源、能对上。
# ⇒ 消费方 Cargo.toml **不要**再声明这些依赖(否则 cargo 又编一份,冲突)。
# 消费方只编译**自己仓库内的 crate**(packs/backend);外部依赖全来自平台包 lib/。
SSPF_RLIB="$(ls vendor/${PKG}/lib/libsspf-*.rlib 2>/dev/null | head -1)"
if [[ -n "${SSPF_RLIB}" ]]; then
# 逐个从 lib/ 找 rlib,生成 --extern 行(缺哪个就跳过)
EXTERN_LINES=""
for c in sspf axum tokio serde serde_json http bytes; do
rl="$(ls "vendor/${PKG}/lib/lib${c}-"*.rlib 2>/dev/null | head -1)"
[[ -z "${rl}" ]] && continue
EXTERN_LINES="${EXTERN_LINES} \"--extern\", \"${c}=${SKILL_ROOT}/${rl}\","$'\n'
done
mkdir -p .cargo
cat > .cargo/config.toml <<EOF
# 自动生成(scripts/fetch-platform.sh)—— 方案 C:预编译 rlib 消费方式
# 把平台预编译 rlib 直接喂给 rustc,消费方【不编译】平台 17 个 crate 闭包,
# 也不编 axum/tokio/serde/serde_json(全部取自平台 lib/,保证与 sspf 同一 crate 实例)。
# ⚠️ 前提:rustc 版本 == vendor/${PKG}/rust-toolchain.toml(ABI 锁,否则链接失败)。
# 极端回退(要改平台源码):删本文件 + 改用 git 依赖 sspf = { git = "http://127.0.0.1:13300/ssw/ssw-platform.git", tag = "v${VERSION}" }
[build]
# rustc 用
rustflags = [
${EXTERN_LINES} "-L", "dependency=${SKILL_ROOT}/vendor/${PKG}/lib",
]
# 🔴 cargo test 的 doctest 走 rustdoc,**不吃 build.rustflags**;必须同样喂一遍,
# 否则测试阶段 doctest 里的 \`use sspf::\` 会 E0432(unresolved import)。
rustdocflags = [
${EXTERN_LINES} "-L", "dependency=${SKILL_ROOT}/vendor/${PKG}/lib",
]
EOF
echo "==> 已生成 .cargo/config.toml(rustflags + rustdocflags:--extern sspf/axum/tokio/serde/serde_json/http/bytes + -L dependency)"
echo " 消费方 Cargo.toml 无需声明这些依赖;代码照用 use sspf:: / use axum::"
else
echo "!! ⚠️ 包内 lib/ 缺 libsspf-*.rlib(旧包?)。请用新版 release.sh 重新出包:" >&2
echo " 或极端回退:删 .cargo/config.toml + 改用 git 依赖(tag = v${VERSION})" >&2
fi
echo ""
echo "下一步:"
echo " • cargo build (方案 C:直接链接预编译 rlib,零编译平台)"
echo " • 升版本:bash scripts/fetch-platform.sh <新版本>"