gitlink-cli/skills/gitlink-project-bootstrap/scripts/02-generate-skeleton.sh

77 lines
3.2 KiB
Bash
Raw 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
# ================================================================
# Phase 2: 生成项目骨架文件
# 功能:读取 project-spec.json → 生成 README/LICENSE/.gitignore/CI → 推送到仓库
# 使用bash 02-generate-skeleton.sh <owner> <repo>
# 环境变量DRY_RUN=true|false (默认 true)
# ================================================================
set -e
# 自动检测并使用本地编译的 gitlink-cli
source "$(cd "$(dirname "$0")" && pwd)/../../gitlink-shared/scripts/detect-cli.sh"
OWNER=${1:?"Usage: $0 <owner> <repo>"}
REPO=${2}
DRY_RUN=${DRY_RUN:-true}
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
OUTPUT_DIR="$SCRIPT_DIR/_output"
mkdir -p "$OUTPUT_DIR"
echo "[========================================]"
echo "[ Phase 2: 生成项目骨架 ]"
echo "[ Target: $OWNER/$REPO ]"
echo "[ DRY_RUN: $DRY_RUN ]"
echo "[========================================]"
# ---- Step 1: 检查 spec ----
if [ ! -f "$OUTPUT_DIR/project-spec.json" ]; then
echo "[WARN] 未找到 project-spec.json先回退执行 Phase 1 的兜底解析"
bash "$SCRIPT_DIR/01-parse-and-create-repo.sh" "$OWNER" "$REPO" >/dev/null 2>&1 || {
echo "[ERROR] project-spec.json 仍缺失,无法继续"; exit 1;
}
fi
echo "[INFO] spec 文件就绪: $OUTPUT_DIR/project-spec.json"
# ---- Step 2: 运行骨架生成引擎 ----
echo "[INFO] 运行 skeleton-gen.js 生成 README/LICENSE/.gitignore/CI..."
node "$SCRIPT_DIR/skeleton-gen.js" "$OUTPUT_DIR"
SKEL_DIR="$OUTPUT_DIR/skeleton"
if [ ! -d "$SKEL_DIR" ]; then
echo "[ERROR] skeleton 目录未生成"; exit 1
fi
echo "[INFO] 骨架文件已生成到 $SKEL_DIR/"
# ---- Step 3: 推送到仓库dry-run 保护) ----
if [ "$DRY_RUN" = "true" ]; then
echo "[DRY-RUN] 将执行: gitlink-cli file +create 逐个推送 README/LICENSE/.gitignore/CI"
echo "[DRY-RUN](或 file +batch 单次 commit建议先 file +batch --help 确认字段)"
else
echo "[INFO] 通过 file +create 逐个推送(最稳,规避 batch 字段歧义)..."
for f in README.md LICENSE .gitignore; do
if [ -f "$SKEL_DIR/$f" ]; then
CONTENT=$(cat "$SKEL_DIR/$f")
gitlink-cli file +create --owner "$OWNER" --repo "$REPO" \
--path "$f" --content "$CONTENT" --message "chore: 初始化 $f" 2>&1 \
| tee -a "$OUTPUT_DIR/file-create.log" \
|| echo "[WARN] 推送 $f 失败(已记录)"
fi
done
# CI 配置
if [ -f "$SKEL_DIR/.devops/构建流水线.yml" ]; then
CONTENT=$(cat "$SKEL_DIR/.devops/构建流水线.yml")
gitlink-cli file +create --owner "$OWNER" --repo "$REPO" \
--path ".devops/构建流水线.yml" --content "$CONTENT" \
--message "ci: 初始化构建流水线" 2>&1 \
| tee -a "$OUTPUT_DIR/file-create.log" \
|| echo "[WARN] 推送 CI 配置失败"
fi
echo "[INFO] 骨架推送完成"
fi
echo ""
echo "[========================================]"
echo "[ Phase 2 完成 ]"
echo "[ 骨架保存至: $SKEL_DIR/ ]"
echo "[========================================]"