render.py 3.92 KB
#!/usr/bin/env python3
"""3-vision — python render.py"""
import json, os, sys, glob
from string import Template

HERE = os.path.dirname(os.path.abspath(__file__))
PARENT = os.path.dirname(HERE)
SHARED = os.path.join(HERE, "templates", "shared")
SKILL_TPL = os.path.join(HERE, "templates", "3-operation-flow")
DATA_DIR = os.path.join(HERE, "data")
sys.path.insert(0, PARENT)
from _resolve import resolve_refs
from _build import build_operation_tab_buttons, build_operation_tab_contents, build_changelogs_html, has_changelog, build_changelog_bar_html

def css():
    with open(os.path.join(SHARED, "design-system.css"), encoding="utf-8") as f:
        return f.read()

def tpl(name, data):
    with open(os.path.join(SKILL_TPL, name), encoding="utf-8") as f:
        raw = f.read()
    raw = resolve_refs(raw, data)
    return Template(raw)

def load_data():
    data = {}
    with open(os.path.join(DATA_DIR, "common.json"), encoding="utf-8") as f:
        data.update(json.load(f))
    ov = os.path.join(DATA_DIR, "overview.json")
    if os.path.exists(ov):
        with open(ov, encoding="utf-8") as f:
            data.update(json.load(f))
    # 扫描 F-*.json 作为 feature 列表
    features = []
    for fp in sorted(glob.glob(os.path.join(DATA_DIR, "F-*.json"))):
        with open(fp, encoding="utf-8") as f:
            features.append(json.load(f))
    data["features"] = features
    return data

def main():
    data = load_data()
    features = data.get("features", [])

    # 自动生成 coverage_rows,基于步骤实际 status 计算覆盖率
    def calc_coverage(feat):
        """计算功能的 OP 覆盖进度"""
        total = 0
        done = 0
        for scenario in feat.get("scenarios", []):
            for step in scenario.get("steps", []):
                total += 1
                if step.get("status") == "done":
                    done += 1
        return total, done

    coverage_rows = ""
    for feat in features:
        total, done = calc_coverage(feat)
        pct = (done / total * 100) if total > 0 else 0
        coverage_rows += (
            f'<div class="coverage-row">'
            f'<div class="coverage-info"><span class="coverage-module">{feat["id"]} {feat.get("title","")}</span>'
            f'<span class="coverage-count">{total} 个 OP</span></div>'
            f'<div class="coverage-bar"><div class="coverage-fill" style="width:{pct:.0f}%"></div></div>'
            f'<span class="coverage-pct">{pct:.0f}%({done}/{total})</span>'
            + ('<span class="coverage-changelog-badge">🔄 有变更</span>' if has_changelog(feat.get("changelogs",[])) else '')
            + f'<a href="{feat["id"]}-{feat.get("fileKey",feat["id"])}-operation.html" class="coverage-detail-link">查看详情 →</a>'
            f'</div>'
        )
    data["coverage_rows"] = coverage_rows

    # 为每个功能构建 tab_buttons 和 tab_contents
    for feat in features:
        scenarios = feat.get("scenarios", [])
        changelogs = feat.get("changelogs", [])
        feat["tab_buttons"] = build_operation_tab_buttons(scenarios)
        feat["tab_contents"] = build_operation_tab_contents(scenarios)
        feat["changelog_html"] = build_changelogs_html(changelogs)

    ov = tpl("overview.html", data)
    with open(os.path.join(HERE, "overview.html"), "w", encoding="utf-8") as f:
        f.write(ov.safe_substitute(**data, CSS=css()))

    det_raw = resolve_refs(open(os.path.join(SKILL_TPL, "F-XX-operation.html"), encoding="utf-8").read(), data)
    for feat in features:
        feat_data = {**data, "feature": feat}
        feat_raw = resolve_refs(det_raw, feat_data)
        html = Template(feat_raw).safe_substitute(**feat_data, CSS=css())
        name = f'{feat["id"]}-{feat.get("fileKey",feat["id"])}-operation.html'
        with open(os.path.join(HERE, name), "w", encoding="utf-8") as f:
            f.write(html)

    print(f"3-vision: overview + {len(data.get('features',[]))} 测试操作 → {HERE}")

if __name__ == "__main__":
    main()