#!/bin/bash # check_encode_unique.sh — 阶段 2.5 enCode 校验 (硬关卡, 重复 = FAIL) # # 来源: tdd2 实战 2026-07-25 BS-47 (字典冲突) + BS-新131 (form 改名) + BS-新133 # 规则: docs/{app}/design.md §9 enCode 清单必须全平台唯一 # # 用法: bash check_encode_unique.sh # 例: bash check_encode_unique.sh docs/tdd2/ # # 退出码: # 0 = PASS (无冲突) # 1 = FAIL (有冲突, 见 stdout 输出) # 2 = WARN (fullName 冲突, 建议加 appCode 前缀) set -e APP_DIR="${1:?用法: $0 }" APP_CODE=$(basename "$APP_DIR") DESIGN_MD="$APP_DIR/design.md" # v0.22+ (2026-07-28 tdd5 实战): --ignore-existing 跳过已存在的 enCode (阶段 6 复跑误判) # 阶段 2.5 (设计时) 跑不带 --ignore-existing: 严格模式, 任何冲突都报 FAIL # 阶段 6 (总结时) 跑带 --ignore-existing: 宽松模式, 已创建的 enCode 不算冲突, 只比对"越界新增" IGNORE_EXISTING="" for arg in "$@"; do case "$arg" in --ignore-existing) IGNORE_EXISTING=1 ;; esac done echo "🔍 阶段 2.5 enCode 校验: $APP_CODE" echo " design: $DESIGN_MD" if [ -n "$IGNORE_EXISTING" ]; then echo " 模式: --ignore-existing (跳过已建 enCode, 阶段 6 复跑专用)" fi echo "" if [ ! -f "$DESIGN_MD" ]; then echo "❌ FAIL: design.md 不存在: $DESIGN_MD" exit 1 fi # 1. 解析 design.md §9 enCode 清单 # 匹配: | {enCode} | {fullName} | 后面带 "UNIQUE" 或 "❌" 等 echo "📋 1. 解析 design.md §9 enCode 清单..." # 用 python 解析 markdown 表格 (跨平台稳定) DICT_ENCODES=$(python3 -c " import re, sys with open('$DESIGN_MD', 'r', encoding='utf-8') as f: content = f.read() m = re.search(r'### 9\.1.*?(?=### 9\.[234]|\Z)', content, re.DOTALL) if not m: sys.exit(0) table = m.group(0) encodes = [] for line in table.split('\n'): if not line.strip().startswith('|'): continue cells = [c.strip() for c in line.split('|')[1:-1]] if len(cells) >= 2 and cells[0] not in ('字典 enCode', '', '---') and not all(set(c) <= set('-—') for c in cells): encodes.append(cells[0]) print('\n'.join(encodes)) ") FORM_ENCODES=$(python3 -c " import re, sys with open('$DESIGN_MD', 'r', encoding='utf-8') as f: content = f.read() m = re.search(r'### 9\.2.*?(?=### 9\.[34]|\Z)', content, re.DOTALL) if not m: sys.exit(0) table = m.group(0) encodes = [] for line in table.split('\n'): if not line.strip().startswith('|'): continue cells = [c.strip() for c in line.split('|')[1:-1]] if len(cells) >= 2 and cells[0] not in ('form enCode', '', '---') and not all(set(c) <= set('-—') for c in cells): encodes.append(cells[0]) print('\n'.join(encodes)) ") WF_ENCODES=$(python3 -c " import re, sys with open('$DESIGN_MD', 'r', encoding='utf-8') as f: content = f.read() m = re.search(r'### 9\.3.*?(?=### 9\.4|\Z)', content, re.DOTALL) if not m: sys.exit(0) table = m.group(0) encodes = [] for line in table.split('\n'): if not line.strip().startswith('|'): continue cells = [c.strip() for c in line.split('|')[1:-1]] if len(cells) >= 2 and cells[0] not in ('workflow enCode', '', '---') and not all(set(c) <= set('-—') for c in cells): encodes.append(cells[0]) print('\n'.join(encodes)) ") MENU_ENCODES=$(python3 -c " import re, sys with open('$DESIGN_MD', 'r', encoding='utf-8') as f: content = f.read() m = re.search(r'### 9\.4.*?(?=## |\Z)', content, re.DOTALL) if not m: sys.exit(0) table = m.group(0) encodes = [] for line in table.split('\n'): if not line.strip().startswith('|'): continue cells = [c.strip() for c in line.split('|')[1:-1]] if len(cells) >= 2 and cells[0] not in ('menu enCode', '', '---') and not all(set(c) <= set('-—') for c in cells): encodes.append(cells[0]) print('\n'.join(encodes)) ") if [ -z "$DICT_ENCODES$FORM_ENCODES$WF_ENCODES$MENU_ENCODES" ]; then echo "❌ FAIL: design.md §9 没找到 enCode 清单" echo " 请按 references/encode-validation.md §3 模板填写" exit 1 fi echo " ✓ dict enCode: $(echo "$DICT_ENCODES" | wc -l) 个" echo " ✓ form enCode: $(echo "$FORM_ENCODES" | wc -l) 个" echo " ✓ workflow enCode: $(echo "$WF_ENCODES" | wc -l) 个" echo " ✓ menu enCode: $(echo "$MENU_ENCODES" | wc -l) 个" # 2. 拉平台当前 enCode echo "" echo "📡 2. 拉 dacli 当前 enCode 库..." DCCLI="npx tsx $ccWorkDir/dacli/src/cli.ts" # 字典 enCode + fullName DICT_PLATFORM_JSON=$($DCCLI dict typeList --json 2>/dev/null | python3 -c " import json, sys d = json.load(sys.stdin) items = [] def walk(lst): for x in lst: ec = x.get('enCode') fn = x.get('fullName') id_ = x.get('id') if ec: items.append({'enCode': ec, 'fullName': fn or '', 'id': id_}) if x.get('children'): walk(x['children']) walk(d.get('list', [])) print(json.dumps(items)) " 2>/dev/null || echo "[]") # form enCode FORM_PLATFORM_JSON=$($DCCLI form list --json 2>/dev/null | python3 -c " import json, sys d = json.load(sys.stdin) items = [] for x in d.get('list', []): items.append({'enCode': x.get('enCode'), 'fullName': x.get('fullName') or '', 'id': x.get('id')}) print(json.dumps(items)) " 2>/dev/null || echo "[]") # workflow enCode WF_PLATFORM_JSON=$($DCCLI workflow selector --json 2>/dev/null | python3 -c " import json, sys try: d = json.load(sys.stdin) items = [] for x in d: if isinstance(x, dict): items.append({'enCode': x.get('enCode'), 'fullName': x.get('fullName') or '', 'id': x.get('id')}) print(json.dumps(items)) except: print('[]') " 2>/dev/null || echo "[]") echo " ✓ 平台字典: $(echo "$DICT_PLATFORM_JSON" | python3 -c "import json,sys; print(len(json.load(sys.stdin)))") 个" echo " ✓ 平台表单: $(echo "$FORM_PLATFORM_JSON" | python3 -c "import json,sys; print(len(json.load(sys.stdin)))") 个" echo " ✓ 平台工作流: $(echo "$WF_PLATFORM_JSON" | python3 -c "import json,sys; print(len(json.load(sys.stdin)))") 个" # 3. 校验 echo "" echo "🔍 3. enCode 冲突检查..." PASS_COUNT=0 FAIL_COUNT=0 WARN_COUNT=0 check_encodes() { local TYPE="$1" local DESIGN_LIST="$2" local PLATFORM_JSON="$3" if [ -z "$DESIGN_LIST" ]; then return fi while IFS= read -r ENCODE; do [ -z "$ENCODE" ] && continue # 检查 enCode 是否已存在 set +e CONFLICT=$(echo "$PLATFORM_JSON" | python3 -c " import json, sys data = json.load(sys.stdin) target = '$ENCODE' for x in data: if x.get('enCode') == target: print(f\"enCode='{target}' 冲突: 已存在 (fullName='{x.get('fullName','')}', id='{x.get('id','')}')\") sys.exit(0) sys.exit(1) " 2>&1) RET=$? set -e if [ $RET -eq 0 ] && [ -n "$CONFLICT" ]; then if [ -n "$IGNORE_EXISTING" ]; then echo " ⚠️ $TYPE EXISTING: $ENCODE (已建, --ignore-existing 跳过)" PASS_COUNT=$((PASS_COUNT+1)) else echo " ❌ $TYPE FAIL: $CONFLICT" FAIL_COUNT=$((FAIL_COUNT+1)) fi else echo " ✅ $TYPE UNIQUE: $ENCODE" PASS_COUNT=$((PASS_COUNT+1)) fi done <<< "$DESIGN_LIST" } check_encodes "dict" "$DICT_ENCODES" "$DICT_PLATFORM_JSON" check_encodes "form" "$FORM_ENCODES" "$FORM_PLATFORM_JSON" check_encodes "wf" "$WF_ENCODES" "$WF_PLATFORM_JSON" # 4. 总结 echo "" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "📊 校验总结:" echo " ✅ PASS: $PASS_COUNT 个 enCode 唯一" echo " ❌ FAIL: $FAIL_COUNT 个 enCode 冲突" echo " ⚠️ WARN: $WARN_COUNT 个 fullName 建议改" if [ $FAIL_COUNT -gt 0 ]; then echo "" echo "❌ FAIL: $FAIL_COUNT 个 enCode 冲突, 必须在阶段 2 修复" echo "" echo "修复步骤:" echo " 1. 改 design.md §9 的 enCode (加 appCode 前缀或换语义)" echo " 2. 重跑: bash $0 $APP_DIR" echo " 3. 通过后才进阶段 3 (审核) → 阶段 4 (执行)" exit 1 fi echo "" echo "✅ PASS: 所有 enCode 唯一, 可进阶段 3 (审核)" exit 0