|
| 1 | +"""Sync template-managed files into a target repo using sync-manifest.json. |
| 2 | +
|
| 3 | +Usage: |
| 4 | + python sync.py TEMPLATE_ROOT REPO_ROOT |
| 5 | +
|
| 6 | +Reads sync-manifest.json from TEMPLATE_ROOT, copies files into REPO_ROOT using |
| 7 | +the mode specified for each mapping: |
| 8 | +
|
| 9 | + overwrite Full replacement. |
| 10 | + marker-preserve Replaces template-owned // #region sections while |
| 11 | + preserving repo-specific content. |
| 12 | +
|
| 13 | +Exit codes: |
| 14 | + 0 Success (prints synced file count). |
| 15 | + 1 Missing arguments or manifest not found. |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import json |
| 21 | +import re |
| 22 | +import sys |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | + |
| 26 | +def marker_preserve_copy(src: Path, dst: Path) -> None: |
| 27 | + """Copy src to dst, preserving content outside template marker regions.""" |
| 28 | + if not dst.exists(): |
| 29 | + dst.parent.mkdir(parents=True, exist_ok=True) |
| 30 | + dst.write_text(src.read_text(), encoding="utf-8") |
| 31 | + return |
| 32 | + |
| 33 | + src_text = src.read_text(encoding="utf-8") |
| 34 | + dst_text = dst.read_text(encoding="utf-8") |
| 35 | + |
| 36 | + pattern = re.compile( |
| 37 | + r"(//\s*#region\s+Template:\s*\S+.*?\n)(.*?)(//\s*#endregion\s+Template:)", |
| 38 | + re.DOTALL, |
| 39 | + ) |
| 40 | + |
| 41 | + src_regions: dict[str, str] = {} |
| 42 | + for match in pattern.finditer(src_text): |
| 43 | + name_match = re.search(r"Template:\s*(\S+)", match.group(1)) |
| 44 | + if name_match: |
| 45 | + src_regions[name_match.group(1)] = match.group(2) |
| 46 | + |
| 47 | + def replace_region(match: re.Match[str]) -> str: |
| 48 | + name_match = re.search(r"Template:\s*(\S+)", match.group(1)) |
| 49 | + if name_match and name_match.group(1) in src_regions: |
| 50 | + return match.group(1) + src_regions[name_match.group(1)] + match.group(3) |
| 51 | + return match.group(0) |
| 52 | + |
| 53 | + dst.write_text(pattern.sub(replace_region, dst_text), encoding="utf-8") |
| 54 | + |
| 55 | + |
| 56 | +def sync(template_root: Path, repo_root: Path) -> int: |
| 57 | + """Run the sync and return the number of files synced.""" |
| 58 | + manifest_path = template_root / "sync-manifest.json" |
| 59 | + if not manifest_path.exists(): |
| 60 | + print(f"Error: {manifest_path} not found", file=sys.stderr) |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | + manifest = json.loads(manifest_path.read_text(encoding="utf-8")) |
| 64 | + |
| 65 | + changed: list[str] = [] |
| 66 | + for mapping in manifest.get("files", []): |
| 67 | + src = template_root / mapping["src"] |
| 68 | + dst = repo_root / mapping["dest"] |
| 69 | + mode = mapping.get("mode", "overwrite") |
| 70 | + |
| 71 | + if not src.exists(): |
| 72 | + print(f" Skip (source missing): {mapping['src']}") |
| 73 | + continue |
| 74 | + |
| 75 | + dst.parent.mkdir(parents=True, exist_ok=True) |
| 76 | + |
| 77 | + if mode == "marker-preserve": |
| 78 | + marker_preserve_copy(src, dst) |
| 79 | + else: |
| 80 | + dst.write_text(src.read_text(encoding="utf-8"), encoding="utf-8") |
| 81 | + |
| 82 | + changed.append(mapping["dest"]) |
| 83 | + print(f" Synced: {mapping['src']} -> {mapping['dest']} ({mode})") |
| 84 | + |
| 85 | + print(f"\n{len(changed)} file(s) synced.") |
| 86 | + return len(changed) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + if len(sys.argv) != 3: |
| 91 | + print(f"Usage: {sys.argv[0]} TEMPLATE_ROOT REPO_ROOT", file=sys.stderr) |
| 92 | + sys.exit(1) |
| 93 | + |
| 94 | + sync(Path(sys.argv[1]), Path(sys.argv[2])) |
0 commit comments