render.sh 1.35 KB
#!/usr/bin/env bash
# render.sh — scope-lock 步骤 D 渲染单个 _module.md 或 REQ-*.md
#
# 用法:
#   bash render.sh module <out_path> <module_code> <module_name> <module_brief>
#   bash render.sh req    <out_path> <req_id> <title> <goal> <rules> <constraints> <acceptance>
#
# 模板路径自定位:脚本同级父目录下 templates/{_module-template.md, req-card-template.md}

set -euo pipefail

TYPE=${1:?missing type (module|req)}
shift

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
TPL_DIR="$SCRIPT_DIR/../templates"

case "$TYPE" in
  module)
    out=${1:?missing out_path}; code=${2:?}; name=${3:?}; brief=${4:?}
    c=$(cat "$TPL_DIR/_module-template.md")
    c="${c//\{\{module_code\}\}/$code}"
    c="${c//\{\{module_name\}\}/$name}"
    c="${c//\{\{module_brief\}\}/$brief}"
    ;;
  req)
    out=${1:?missing out_path}; req_id=${2:?}; title=${3:?}; goal=${4:?}; rules=${5:?}; constraints=${6:?}; acceptance=${7:?}
    c=$(sed '/^<!--$/,/^-->$/d' "$TPL_DIR/req-card-template.md")
    c="${c//\{\{req_id\}\}/$req_id}"
    c="${c//\{\{title\}\}/$title}"
    c="${c//\{\{goal\}\}/$goal}"
    c="${c//\{\{rules\}\}/$rules}"
    c="${c//\{\{constraints\}\}/$constraints}"
    c="${c//\{\{acceptance\}\}/$acceptance}"
    ;;
  *)
    echo "render.sh: unknown type '$TYPE' (expect module|req)" >&2
    exit 1
    ;;
esac

printf '%s\n' "$c" > "$out"