|
7 | 7 | from __future__ import annotations |
8 | 8 |
|
9 | 9 | import argparse |
| 10 | +import json |
10 | 11 | import re |
11 | 12 | import shutil |
12 | 13 | import sys |
|
31 | 32 | "dist", |
32 | 33 | "site", |
33 | 34 | } |
| 35 | +PUBLISH_METADATA_PATH = Path("fern/publish-metadata.json") |
34 | 36 | FERN_DEVNOTE_SUPPORT_PATHS = [ |
35 | 37 | "fern/assets", |
36 | 38 | "fern/components/Authors.tsx", |
@@ -166,6 +168,49 @@ def validate_redirect_targets(published_root: Path) -> None: |
166 | 168 | ) |
167 | 169 |
|
168 | 170 |
|
| 171 | +def write_publish_metadata(published_root: Path, args: argparse.Namespace, action: str) -> None: |
| 172 | + provided = [ |
| 173 | + args.metadata_source_repository, |
| 174 | + args.metadata_source_ref, |
| 175 | + args.metadata_source_sha, |
| 176 | + args.metadata_release_tag, |
| 177 | + args.metadata_published_branch, |
| 178 | + ] |
| 179 | + if not any(provided): |
| 180 | + return |
| 181 | + |
| 182 | + missing = [ |
| 183 | + name |
| 184 | + for name, value in ( |
| 185 | + ("metadata source repository", args.metadata_source_repository), |
| 186 | + ("metadata source ref", args.metadata_source_ref), |
| 187 | + ("metadata source sha", args.metadata_source_sha), |
| 188 | + ) |
| 189 | + if not value |
| 190 | + ] |
| 191 | + if missing: |
| 192 | + raise PublishedBranchError(f"Incomplete publish metadata; missing {', '.join(missing)}") |
| 193 | + |
| 194 | + metadata: dict[str, object] = { |
| 195 | + "schema_version": 1, |
| 196 | + "kind": "fern-docs-website", |
| 197 | + "action": action, |
| 198 | + "source": { |
| 199 | + "repository": args.metadata_source_repository, |
| 200 | + "ref": args.metadata_source_ref, |
| 201 | + "sha": args.metadata_source_sha, |
| 202 | + }, |
| 203 | + } |
| 204 | + if args.metadata_release_tag: |
| 205 | + metadata["release_tag"] = args.metadata_release_tag |
| 206 | + if args.metadata_published_branch: |
| 207 | + metadata["published_branch"] = args.metadata_published_branch |
| 208 | + |
| 209 | + target = published_root / PUBLISH_METADATA_PATH |
| 210 | + target.parent.mkdir(parents=True, exist_ok=True) |
| 211 | + target.write_text(json.dumps(metadata, indent=2) + "\n") |
| 212 | + |
| 213 | + |
169 | 214 | def ignore_source(_dir: str, names: list[str]) -> set[str]: |
170 | 215 | return {name for name in names if name in SKIP_NAMES or name == "__pycache__"} |
171 | 216 |
|
@@ -347,6 +392,7 @@ def sync_source(args: argparse.Namespace) -> int: |
347 | 392 | materialize_version_nav_pages(published_root) |
348 | 393 | restore_versions_block(published_root / "fern" / "docs.yml", preserved_versions_block) |
349 | 394 | validate_redirect_targets(published_root) |
| 395 | + write_publish_metadata(published_root, args, "release-snapshot") |
350 | 396 | return 0 |
351 | 397 |
|
352 | 398 |
|
@@ -396,21 +442,32 @@ def patch_devnotes(args: argparse.Namespace) -> int: |
396 | 442 |
|
397 | 443 | source_block = extract_devnotes_block(source_nav) |
398 | 444 | replace_devnotes_block(target_nav, rewrite_devnotes_block(source_root, published_root, source_block)) |
| 445 | + write_publish_metadata(published_root, args, "devnotes-patch") |
399 | 446 | return 0 |
400 | 447 |
|
401 | 448 |
|
| 449 | +def add_metadata_args(parser: argparse.ArgumentParser) -> None: |
| 450 | + parser.add_argument("--metadata-source-repository", help="Repository used to produce this published snapshot") |
| 451 | + parser.add_argument("--metadata-source-ref", help="Git ref used to produce this published snapshot") |
| 452 | + parser.add_argument("--metadata-source-sha", help="Git commit used to produce this published snapshot") |
| 453 | + parser.add_argument("--metadata-release-tag", help="Release tag represented by this published snapshot") |
| 454 | + parser.add_argument("--metadata-published-branch", help="Published branch updated by this snapshot") |
| 455 | + |
| 456 | + |
402 | 457 | def build_parser() -> argparse.ArgumentParser: |
403 | 458 | parser = argparse.ArgumentParser(description=__doc__) |
404 | 459 | subparsers = parser.add_subparsers(required=True) |
405 | 460 |
|
406 | 461 | sync_parser = subparsers.add_parser("sync-source") |
407 | 462 | sync_parser.add_argument("--source-root", required=True, help="Repository checkout with authoring content") |
408 | 463 | sync_parser.add_argument("--published-root", required=True, help="docs-website checkout to update") |
| 464 | + add_metadata_args(sync_parser) |
409 | 465 | sync_parser.set_defaults(func=sync_source) |
410 | 466 |
|
411 | 467 | devnotes_parser = subparsers.add_parser("patch-devnotes") |
412 | 468 | devnotes_parser.add_argument("--source-root", required=True, help="Repository checkout with latest Dev Notes") |
413 | 469 | devnotes_parser.add_argument("--published-root", required=True, help="docs-website checkout to patch") |
| 470 | + add_metadata_args(devnotes_parser) |
414 | 471 | devnotes_parser.set_defaults(func=patch_devnotes) |
415 | 472 | return parser |
416 | 473 |
|
|
0 commit comments