| 1 |
"""Stage the files harlequin.sh vendors, and the manifest that dates them. |
| 2 |
|
| 3 |
Three artifacts are generated in this repo and read on the site: the base |
| 4 |
config schema, whose `$id` is a URL there; the generated CLI reference, which |
| 5 |
is a docs page; and the skill, which the site publishes beside the two install |
| 6 |
lines that do not need it. The site vendors them rather than fetching them at |
| 7 |
build time, so its build stays hermetic and a stale page is the worst this |
| 8 |
pipeline can do to it. |
| 9 |
|
| 10 |
Usage: |
| 11 |
uv run python scripts/publish_artifacts.py [--output DIRECTORY] |
| 12 |
|
| 13 |
Copies rather than generates: the committed files are what the wheel ships and |
| 14 |
what `test_cli_reference.py` and `test_config_schema.py` pin, and a publish |
| 15 |
that regenerated them could put something on the site that is in no release. |
| 16 |
The output is a pure function of the checkout -- no timestamps, no host -- so |
| 17 |
running it twice writes the same bytes and the vendoring PR's diff is only what |
| 18 |
actually changed. |
| 19 |
|
| 20 |
`.github/workflows/publish-artifacts.yml` runs this and opens that PR. |
| 21 |
""" |
| 22 |
|
| 23 |
from __future__ import annotations |
| 24 |
|
| 25 |
import argparse |
| 26 |
import hashlib |
| 27 |
import json |
| 28 |
from importlib.metadata import version |
| 29 |
from pathlib import Path |
| 30 |
from typing import Any, NamedTuple |
| 31 |
|
| 32 |
from harlequin.hsql.modes import skill as skill_mode |
| 33 |
|
| 34 |
REPO_ROOT = Path(__file__).resolve().parents[1] |
| 35 |
|
| 36 |
DEFAULT_OUTPUT = REPO_ROOT / "dist" / "artifacts" |
| 37 |
|
| 38 |
MANIFEST_NAME = "manifest.json" |
| 39 |
"""What records the version the others came from, so a reader can tell when |
| 40 |
the page they are on is behind the release they have installed.""" |
| 41 |
|
| 42 |
|
| 43 |
class Artifact(NamedTuple): |
| 44 |
"""One file this repo publishes, and the name it is published under.""" |
| 45 |
|
| 46 |
source: Path |
| 47 |
name: str |
| 48 |
|
| 49 |
|
| 50 |
def artifacts() -> list[Artifact]: |
| 51 |
"""Every file the site vendors, in the layout it vendors them in. |
| 52 |
|
| 53 |
The references come from the skill directory rather than a list, because a |
| 54 |
`SKILL.md` whose pointers dangle is the one way this can publish a skill |
| 55 |
that is worse than no skill -- the same reason `--skill -o` copies them. |
| 56 |
""" |
| 57 |
staged = [ |
| 58 |
Artifact( |
| 59 |
REPO_ROOT / "src" / "harlequin" / "schemas" / "config-v1.json", |
| 60 |
"config-v1.json", |
| 61 |
), |
| 62 |
Artifact( |
| 63 |
REPO_ROOT / "docs" / "generated" / "hsql-reference.md", "hsql-reference.md" |
| 64 |
), |
| 65 |
Artifact(skill_mode.SKILL_DIR / skill_mode.FILENAME, skill_mode.FILENAME), |
| 66 |
] |
| 67 |
staged.extend( |
| 68 |
Artifact(path, f"{skill_mode.REFERENCES}/{path.name}") |
| 69 |
for path in skill_mode.reference_paths() |
| 70 |
) |
| 71 |
return staged |
| 72 |
|
| 73 |
|
| 74 |
def build_manifest(staged: list[Artifact]) -> dict[str, Any]: |
| 75 |
"""What was published, from where, and out of which release.""" |
| 76 |
return { |
| 77 |
"version": version("harlequin"), |
| 78 |
"generated_by": "scripts/publish_artifacts.py", |
| 79 |
"files": [ |
| 80 |
{ |
| 81 |
"path": artifact.name, |
| 82 |
"source": artifact.source.relative_to(REPO_ROOT).as_posix(), |
| 83 |
"bytes": len(artifact.source.read_bytes()), |
| 84 |
"sha256": hashlib.sha256(artifact.source.read_bytes()).hexdigest(), |
| 85 |
} |
| 86 |
for artifact in sorted(staged, key=lambda artifact: artifact.name) |
| 87 |
], |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
def stage(directory: Path) -> list[Path]: |
| 92 |
"""Write every artifact and the manifest into `directory`, and say which. |
| 93 |
|
| 94 |
Byte-for-byte copies: a line ending changed on the way through is a diff on |
| 95 |
every publish and a file the site serves that no release contains. |
| 96 |
""" |
| 97 |
staged = artifacts() |
| 98 |
missing = [artifact.source for artifact in staged if not artifact.source.is_file()] |
| 99 |
if missing: |
| 100 |
raise FileNotFoundError( |
| 101 |
"cannot publish what is not here: " |
| 102 |
+ ", ".join(path.relative_to(REPO_ROOT).as_posix() for path in missing) |
| 103 |
) |
| 104 |
written = [] |
| 105 |
for artifact in staged: |
| 106 |
destination = directory / artifact.name |
| 107 |
destination.parent.mkdir(parents=True, exist_ok=True) |
| 108 |
destination.write_bytes(artifact.source.read_bytes()) |
| 109 |
written.append(destination) |
| 110 |
manifest = directory / MANIFEST_NAME |
| 111 |
manifest.write_text( |
| 112 |
json.dumps(build_manifest(staged), indent=2) + "\n", |
| 113 |
encoding="utf-8", |
| 114 |
newline="\n", |
| 115 |
) |
| 116 |
written.append(manifest) |
| 117 |
return written |
| 118 |
|
| 119 |
|
| 120 |
def main() -> None: |
| 121 |
parser = argparse.ArgumentParser(description=__doc__) |
| 122 |
parser.add_argument( |
| 123 |
"-o", |
| 124 |
"--output", |
| 125 |
type=Path, |
| 126 |
default=DEFAULT_OUTPUT, |
| 127 |
help="the directory to stage the artifacts in", |
| 128 |
) |
| 129 |
directory = parser.parse_args().output |
| 130 |
for path in stage(directory): |
| 131 |
print(f"wrote {path}") |
| 132 |
|
| 133 |
|
| 134 |
if __name__ == "__main__": |
| 135 |
main() |