|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Organize mathx API members into file-name categories for DocFX TOC and tabbed hub.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import json |
| 7 | +import sys |
| 8 | +from collections import defaultdict |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +try: |
| 12 | + import yaml |
| 13 | +except ImportError: |
| 14 | + print("PyYAML required: pip install pyyaml", file=sys.stderr) |
| 15 | + sys.exit(1) |
| 16 | + |
| 17 | + |
| 18 | +CATEGORY_LABELS: dict[str, str] = { |
| 19 | + "angle": "Angle", |
| 20 | + "common": "Common", |
| 21 | + "constants": "Constants", |
| 22 | + "conversion": "Conversion", |
| 23 | + "exponential": "Exponential", |
| 24 | + "fast-math": "Fast math", |
| 25 | + "hash": "Hash", |
| 26 | + "interpolation": "Interpolation", |
| 27 | + "iteration": "Iteration", |
| 28 | + "jobify": "Jobify", |
| 29 | + "klakmath": "KlakMath", |
| 30 | + "logic": "Logic", |
| 31 | + "mathf-translations": "Mathf translations", |
| 32 | + "matrix": "Matrix", |
| 33 | + "mult": "Mult", |
| 34 | + "noise": "Noise", |
| 35 | + "random": "Random", |
| 36 | + "rotation": "Rotation", |
| 37 | + "rounding": "Rounding", |
| 38 | + "sdf": "SDF", |
| 39 | + "selection": "Selection", |
| 40 | + "special": "Special", |
| 41 | + "structs": "Structs", |
| 42 | + "transformation": "Transformation", |
| 43 | + "trigonometry": "Trigonometry", |
| 44 | + "vector": "Vector", |
| 45 | + "floatx": "Float extensions", |
| 46 | + "intx": "Int extensions", |
| 47 | + "other": "Other", |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +def category_from_source_path(path: str) -> str: |
| 52 | + path = path.replace("\\", "/") |
| 53 | + if "/Runtime/" in path: |
| 54 | + rel = path.split("/Runtime/", 1)[1] |
| 55 | + else: |
| 56 | + rel = path.lstrip("./") |
| 57 | + |
| 58 | + parts = rel.split("/") |
| 59 | + if len(parts) > 1: |
| 60 | + folder = parts[0].lower() |
| 61 | + mapping = { |
| 62 | + "noise": "noise", |
| 63 | + "fastmath": "fast-math", |
| 64 | + "jobify": "jobify", |
| 65 | + "sdf": "sdf", |
| 66 | + "structs": "structs", |
| 67 | + "data": "constants", |
| 68 | + "klakmath": "klakmath", |
| 69 | + } |
| 70 | + return mapping.get(folder, folder) |
| 71 | + |
| 72 | + filename = parts[-1] |
| 73 | + if not filename.endswith(".cs"): |
| 74 | + return "other" |
| 75 | + |
| 76 | + stem = filename[:-3] |
| 77 | + if stem == "mathx": |
| 78 | + return "common" |
| 79 | + |
| 80 | + if stem.startswith("mathx."): |
| 81 | + segment = stem.split(".", 2)[1] |
| 82 | + if segment.lower() == "mathf": |
| 83 | + return "mathf-translations" |
| 84 | + return segment.lower() |
| 85 | + |
| 86 | + return "other" |
| 87 | + |
| 88 | + |
| 89 | +def load_mathx_members(yml_path: Path) -> list[dict]: |
| 90 | + with yml_path.open(encoding="utf-8") as handle: |
| 91 | + doc = yaml.safe_load(handle) |
| 92 | + |
| 93 | + members: list[dict] = [] |
| 94 | + for item in doc.get("items", []): |
| 95 | + uid = item.get("uid", "") |
| 96 | + if not uid.startswith("Unity.Mathematics.mathx."): |
| 97 | + continue |
| 98 | + source = item.get("source") or {} |
| 99 | + path = source.get("path") or source.get("remote", {}).get("path") or "" |
| 100 | + members.append( |
| 101 | + { |
| 102 | + "uid": uid, |
| 103 | + "name": item.get("name") or uid.rsplit(".", 1)[-1], |
| 104 | + "type": item.get("type") or "Member", |
| 105 | + "category": category_from_source_path(path), |
| 106 | + } |
| 107 | + ) |
| 108 | + return members |
| 109 | + |
| 110 | + |
| 111 | +def patch_toc(toc_path: Path, members: list[dict]) -> None: |
| 112 | + with toc_path.open(encoding="utf-8") as handle: |
| 113 | + toc = yaml.safe_load(handle) |
| 114 | + |
| 115 | + by_category: dict[str, list[dict]] = defaultdict(list) |
| 116 | + for member in members: |
| 117 | + by_category[member["category"]].append(member) |
| 118 | + |
| 119 | + def sort_key(name: str) -> tuple[int, str]: |
| 120 | + return (0 if name in CATEGORY_LABELS else 1, CATEGORY_LABELS.get(name, name)) |
| 121 | + |
| 122 | + categories = sorted(by_category.keys(), key=sort_key) |
| 123 | + |
| 124 | + mathx_items: list[dict] = [] |
| 125 | + for category in categories: |
| 126 | + label = CATEGORY_LABELS.get(category, category.replace("-", " ").title()) |
| 127 | + children = sorted(by_category[category], key=lambda m: m["name"].lower()) |
| 128 | + mathx_items.append( |
| 129 | + { |
| 130 | + "name": label, |
| 131 | + "items": [ |
| 132 | + {"uid": child["uid"], "name": child["name"], "type": child["type"]} |
| 133 | + for child in children |
| 134 | + ], |
| 135 | + } |
| 136 | + ) |
| 137 | + |
| 138 | + namespace_items = toc["items"][0]["items"] |
| 139 | + for index, entry in enumerate(namespace_items): |
| 140 | + if entry.get("uid") == "Unity.Mathematics.mathx": |
| 141 | + namespace_items[index] = { |
| 142 | + "uid": "Unity.Mathematics.mathx", |
| 143 | + "name": "mathx", |
| 144 | + "type": "Class", |
| 145 | + "items": mathx_items, |
| 146 | + } |
| 147 | + break |
| 148 | + else: |
| 149 | + raise RuntimeError("mathx entry not found in api/metadata/toc.yml") |
| 150 | + |
| 151 | + toc["memberLayout"] = "SeparatePages" |
| 152 | + |
| 153 | + with toc_path.open("w", encoding="utf-8", newline="\n") as handle: |
| 154 | + yaml.safe_dump( |
| 155 | + toc, |
| 156 | + handle, |
| 157 | + sort_keys=False, |
| 158 | + default_flow_style=False, |
| 159 | + allow_unicode=True, |
| 160 | + width=120, |
| 161 | + ) |
| 162 | + |
| 163 | + |
| 164 | +def member_href(uid: str) -> str: |
| 165 | + return uid + ".html" |
| 166 | + |
| 167 | + |
| 168 | +def write_category_manifest(manifest_dir: Path, members: list[dict]) -> None: |
| 169 | + by_category: dict[str, list[dict]] = defaultdict(list) |
| 170 | + for member in members: |
| 171 | + by_category[member["category"]].append( |
| 172 | + {"name": member["name"], "href": member_href(member["uid"]), "type": member["type"]} |
| 173 | + ) |
| 174 | + |
| 175 | + categories_dir = manifest_dir / "mathx-categories" |
| 176 | + categories_dir.mkdir(parents=True, exist_ok=True) |
| 177 | + |
| 178 | + for stale in categories_dir.glob("*.json"): |
| 179 | + stale.unlink() |
| 180 | + |
| 181 | + index = {"categories": []} |
| 182 | + for category in sorted( |
| 183 | + by_category.keys(), |
| 184 | + key=lambda c: (0 if c in CATEGORY_LABELS else 1, CATEGORY_LABELS.get(c, c)), |
| 185 | + ): |
| 186 | + label = CATEGORY_LABELS.get(category, category.replace("-", " ").title()) |
| 187 | + payload = sorted(by_category[category], key=lambda m: m["name"].lower()) |
| 188 | + (categories_dir / f"{category}.json").write_text(json.dumps(payload, indent=2), encoding="utf-8") |
| 189 | + index["categories"].append({"id": category, "label": label, "count": len(payload)}) |
| 190 | + |
| 191 | + (manifest_dir / "mathx-categories.json").write_text(json.dumps(index, indent=2), encoding="utf-8") |
| 192 | + |
| 193 | + |
| 194 | +def main() -> int: |
| 195 | + root = Path(__file__).resolve().parents[1] |
| 196 | + metadata_dir = root / "api" / "metadata" |
| 197 | + yml_path = metadata_dir / "Unity.Mathematics.mathx.yml" |
| 198 | + toc_path = metadata_dir / "toc.yml" |
| 199 | + |
| 200 | + if not yml_path.is_file(): |
| 201 | + print(f"Missing metadata: {yml_path}", file=sys.stderr) |
| 202 | + return 1 |
| 203 | + |
| 204 | + members = load_mathx_members(yml_path) |
| 205 | + if not members: |
| 206 | + print("No mathx members found in metadata.", file=sys.stderr) |
| 207 | + return 1 |
| 208 | + |
| 209 | + patch_toc(toc_path, members) |
| 210 | + write_category_manifest(metadata_dir, members) |
| 211 | + |
| 212 | + counts = defaultdict(int) |
| 213 | + for member in members: |
| 214 | + counts[member["category"]] += 1 |
| 215 | + |
| 216 | + print(f"Organized {len(members)} mathx members into {len(counts)} categories.") |
| 217 | + for category, count in sorted(counts.items(), key=lambda kv: (-kv[1], kv[0])): |
| 218 | + label = CATEGORY_LABELS.get(category, category) |
| 219 | + print(f" {label}: {count}") |
| 220 | + |
| 221 | + return 0 |
| 222 | + |
| 223 | + |
| 224 | +if __name__ == "__main__": |
| 225 | + raise SystemExit(main()) |
0 commit comments