#!/usr/bin/env bash # 把一个插件从它的独立仓库存进本体:拷贝为 src/plugin-.js。 # 自 2026-10-17 起,构建会自动发现 src/plugin-*.js ⇒ **无需登记 sourceModules、无需重生成顺序快照**。 # 契约:插件之间不得依赖彼此的加载顺序(各自只依赖 window.IWBPluginHost 与自身闭包)。 # # 用法: # scripts/plugins-sync.sh <插件仓路径或 URL> <插件 id> [源文件,默认 dist/plugin.js] [--force] # # 例: # scripts/plugins-sync.sh https://github.com/me/iwb-plugin-whitebg whitebg # 首次合入 # scripts/plugins-sync.sh https://github.com/me/iwb-plugin-whitebg whitebg dist/plugin.js --force # 更新 # # 退出码:0 = 已合入;其他 = 失败。 set -euo pipefail usage() { sed -n '2,14p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//' } REPO="${1:-}" ID="${2:-}" SRC_FILE="${3:-dist/plugin.js}" FORCE="${4:-}" if [ -z "$REPO" ] || [ -z "$ID" ]; then usage exit 2 fi if ! [[ "$ID" =~ ^[a-z][a-z0-9-]{2,31}$ ]]; then echo "插件 id 不合法:须匹配 ^[a-z][a-z0-9-]{2,31}$" >&2 exit 2 fi echo "[plugins-sync] ⚠️ 仅同步已审计的插件:插件是完全可信的同源代码(可读 API Key/图片、注入 UI),无沙箱(见规则库 §13.35)。" >&2 ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" TARGET="$ROOT/src/plugin-$ID.js" if [ -e "$TARGET" ] && [ "$FORCE" != "--force" ]; then echo "目标已存在:$TARGET" >&2 echo "要更新它,请带 --force(或用插件仓的版本号决定),例如:" >&2 echo " scripts/plugins-sync.sh '$REPO' $ID '$SRC_FILE' --force" >&2 exit 1 fi TMP="$(mktemp -d)" trap 'rm -rf "$TMP"' EXIT echo "[plugins-sync] 拉取 $REPO" git clone --depth 1 --quiet "$REPO" "$TMP/repo" if [ ! -f "$TMP/repo/$SRC_FILE" ]; then echo "插件仓缺少源文件 ${SRC_FILE}(可用第三个参数指定,例如 dist/index.js)" >&2 exit 1 fi if ! head -c 4096 "$TMP/repo/$SRC_FILE" | grep -q "IWBPluginHost"; then echo "警告:$SRC_FILE 里没看到 IWBPluginHost.register —— 请确认这确实是插件产物。" >&2 fi cp "$TMP/repo/$SRC_FILE" "$TARGET" SOURCE_SHA="$(git -C "$TMP/repo" rev-parse HEAD)" SOURCE_TAG="$(git -C "$TMP/repo" describe --tags --always 2>/dev/null || echo "$SOURCE_SHA")" echo "[plugins-sync] 已合入 $TARGET" echo echo "来源(请写进提交信息,便于追溯):" echo " $REPO@$SOURCE_TAG ($SOURCE_SHA)" echo echo "下一步:" echo " git add src/plugin-$ID.js && git commit -m 'feat(plugin): 合入 $ID@$SOURCE_TAG'" echo " npm run verify && npm run deploy:test"