|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import argparse |
4 | | -import importlib.metadata |
5 | | -import json |
6 | | -import os |
7 | 4 | import shutil |
8 | 5 | import subprocess |
9 | | -from collections.abc import Generator |
10 | | -from contextlib import contextmanager |
11 | 6 | from pathlib import Path |
12 | | -from typing import TYPE_CHECKING, TypedDict, cast |
13 | | - |
14 | | -if TYPE_CHECKING: |
15 | | - from collections.abc import Generator |
16 | | - |
17 | | -REDIRECT_TEMPLATE = """ |
18 | | -<!DOCTYPE HTML> |
19 | | -<html lang="en-US"> |
20 | | - <head> |
21 | | - <title>Page Redirection</title> |
22 | | - <meta charset="UTF-8"> |
23 | | - <meta http-equiv="refresh" content="0; url={target}"> |
24 | | - <script type="text/javascript">window.location.href = "{target}"</script> |
25 | | - </head> |
26 | | - <body> |
27 | | - You are being redirected. If this does not work, click <a href='{target}'>this link</a> |
28 | | - </body> |
29 | | -</html> |
30 | | -""" |
31 | 7 |
|
32 | 8 | parser = argparse.ArgumentParser() |
33 | | -parser.add_argument("--version", required=False) |
34 | 9 | parser.add_argument("output") |
35 | 10 |
|
36 | 11 |
|
37 | | -class VersionSpec(TypedDict): |
38 | | - versions: list[str] |
39 | | - latest: str |
40 | | - |
41 | | - |
42 | | -@contextmanager |
43 | | -def checkout(branch: str) -> Generator[None]: |
44 | | - subprocess.run(["git", "checkout", branch], check=True) # noqa: S607 |
45 | | - yield |
46 | | - subprocess.run(["git", "checkout", "-"], check=True) # noqa: S607 |
47 | | - |
48 | | - |
49 | | -def load_version_spec() -> VersionSpec: |
50 | | - versions_file = Path("docs/_static/versions.json") |
51 | | - if versions_file.exists(): |
52 | | - return cast("VersionSpec", json.loads(versions_file.read_text())) |
53 | | - return {"versions": [], "latest": ""} |
54 | | - |
55 | | - |
56 | | -def build(output_dir: str, version: str | None) -> None: |
57 | | - if version is None: |
58 | | - version = importlib.metadata.version("sqlspec").rsplit(".")[0] |
59 | | - else: |
60 | | - os.environ["_SQLSPEC_DOCS_BUILD_VERSION"] = version |
61 | | - |
| 12 | +def build(output_dir: str) -> None: |
62 | 13 | subprocess.run(["make", "docs"], check=True) # noqa: S607 |
63 | 14 |
|
64 | | - Path(output_dir).mkdir() |
65 | | - Path(output_dir).joinpath(".nojekyll").touch(exist_ok=True) |
66 | | - |
67 | | - version_spec = load_version_spec() |
68 | | - is_latest = version == version_spec["latest"] |
69 | | - |
70 | 15 | docs_src_path = Path("docs/_build/html") |
| 16 | + output_path = Path(output_dir) |
71 | 17 |
|
72 | | - Path(output_dir).joinpath("index.html").write_text(REDIRECT_TEMPLATE.format(target="latest")) |
73 | | - |
74 | | - if is_latest: |
75 | | - shutil.copytree(docs_src_path, Path(output_dir) / "latest", dirs_exist_ok=True) |
76 | | - shutil.copytree(docs_src_path, Path(output_dir) / version, dirs_exist_ok=True) |
| 18 | + output_path.mkdir(parents=True, exist_ok=True) |
| 19 | + output_path.joinpath(".nojekyll").touch(exist_ok=True) |
77 | 20 |
|
78 | | - # copy existing versions into our output dir to preserve them when cleaning the branch |
79 | | - with checkout("gh-pages"): |
80 | | - for other_version in [*version_spec["versions"], "latest"]: |
81 | | - other_version_path = Path(other_version) |
82 | | - other_version_target_path = Path(output_dir) / other_version |
83 | | - if other_version_path.exists() and not other_version_target_path.exists(): |
84 | | - shutil.copytree(other_version_path, other_version_target_path) |
| 21 | + for item in docs_src_path.iterdir(): |
| 22 | + dest = output_path / item.name |
| 23 | + if item.is_dir(): |
| 24 | + shutil.copytree(item, dest, dirs_exist_ok=True) |
| 25 | + else: |
| 26 | + shutil.copy2(item, dest) |
85 | 27 |
|
86 | 28 |
|
87 | 29 | def main() -> None: |
88 | 30 | args = parser.parse_args() |
89 | | - build(output_dir=args.output, version=args.version) |
| 31 | + build(output_dir=args.output) |
90 | 32 |
|
91 | 33 |
|
92 | 34 | if __name__ == "__main__": |
|
0 commit comments