|
| 1 | +"""Hatch build hook that bundles the webui (Vite) into nanobot/web/dist. |
| 2 | +
|
| 3 | +Triggered automatically by `python -m build` (and any other hatch-driven build) |
| 4 | +so published wheels and sdists ship a fresh webui without requiring developers |
| 5 | +to remember `cd webui && bun run build` beforehand. |
| 6 | +
|
| 7 | +Behaviour: |
| 8 | +
|
| 9 | +- Skips for editable installs (`pip install -e .`). Editable mode is for Python |
| 10 | + development; webui contributors use `cd webui && bun run dev` (Vite HMR) and |
| 11 | + do not need a packaged `dist/`. |
| 12 | +- No-op when `webui/package.json` is absent (e.g. installing from an sdist that |
| 13 | + already contains a prebuilt `nanobot/web/dist/`). |
| 14 | +- Skips when `NANOBOT_SKIP_WEBUI_BUILD=1` is set. |
| 15 | +- Skips when `nanobot/web/dist/index.html` already exists, unless |
| 16 | + `NANOBOT_FORCE_WEBUI_BUILD=1` is set. |
| 17 | +- Uses `bun` when available, otherwise falls back to `npm`. The chosen tool |
| 18 | + performs `install` followed by `run build`. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import os |
| 24 | +import shutil |
| 25 | +import subprocess |
| 26 | +from pathlib import Path |
| 27 | + |
| 28 | +from hatchling.builders.hooks.plugin.interface import BuildHookInterface |
| 29 | + |
| 30 | + |
| 31 | +class WebUIBuildHook(BuildHookInterface): |
| 32 | + PLUGIN_NAME = "webui-build" |
| 33 | + |
| 34 | + def initialize(self, version: str, build_data: dict) -> None: # noqa: D401 |
| 35 | + root = Path(self.root) |
| 36 | + webui_dir = root / "webui" |
| 37 | + package_json = webui_dir / "package.json" |
| 38 | + dist_dir = root / "nanobot" / "web" / "dist" |
| 39 | + index_html = dist_dir / "index.html" |
| 40 | + |
| 41 | + # `pip install -e .` builds an editable wheel; skip the (slow) webui |
| 42 | + # bundle since editable installs target Python development and webui |
| 43 | + # work uses `bun run dev` instead. |
| 44 | + if self.target_name == "wheel" and version == "editable": |
| 45 | + self.app.display_info( |
| 46 | + "[webui-build] skipped for editable install " |
| 47 | + "(use `cd webui && bun run build` to bundle webui manually)" |
| 48 | + ) |
| 49 | + return |
| 50 | + |
| 51 | + if os.environ.get("NANOBOT_SKIP_WEBUI_BUILD") == "1": |
| 52 | + self.app.display_info("[webui-build] skipped via NANOBOT_SKIP_WEBUI_BUILD=1") |
| 53 | + return |
| 54 | + |
| 55 | + if not package_json.is_file(): |
| 56 | + self.app.display_info( |
| 57 | + "[webui-build] no webui/ source tree, assuming prebuilt nanobot/web/dist/" |
| 58 | + ) |
| 59 | + return |
| 60 | + |
| 61 | + force = os.environ.get("NANOBOT_FORCE_WEBUI_BUILD") == "1" |
| 62 | + if index_html.is_file() and not force: |
| 63 | + self.app.display_info( |
| 64 | + f"[webui-build] reusing existing build at {dist_dir} " |
| 65 | + "(set NANOBOT_FORCE_WEBUI_BUILD=1 to rebuild)" |
| 66 | + ) |
| 67 | + return |
| 68 | + |
| 69 | + runner = self._pick_runner() |
| 70 | + if runner is None: |
| 71 | + raise RuntimeError( |
| 72 | + "[webui-build] neither `bun` nor `npm` is available on PATH; " |
| 73 | + "install one or set NANOBOT_SKIP_WEBUI_BUILD=1 to bypass." |
| 74 | + ) |
| 75 | + |
| 76 | + self.app.display_info(f"[webui-build] using {runner} to build webui") |
| 77 | + self._run([runner, "install"], cwd=webui_dir) |
| 78 | + self._run([runner, "run", "build"], cwd=webui_dir) |
| 79 | + |
| 80 | + if not index_html.is_file(): |
| 81 | + raise RuntimeError( |
| 82 | + f"[webui-build] build finished but {index_html} is missing; " |
| 83 | + "check webui/vite.config.ts outDir." |
| 84 | + ) |
| 85 | + self.app.display_info(f"[webui-build] webui ready at {dist_dir}") |
| 86 | + |
| 87 | + @staticmethod |
| 88 | + def _pick_runner() -> str | None: |
| 89 | + for candidate in ("bun", "npm"): |
| 90 | + if shutil.which(candidate): |
| 91 | + return candidate |
| 92 | + return None |
| 93 | + |
| 94 | + def _run(self, cmd: list[str], *, cwd: Path) -> None: |
| 95 | + self.app.display_info(f"[webui-build] $ {' '.join(cmd)} (cwd={cwd})") |
| 96 | + try: |
| 97 | + subprocess.run(cmd, cwd=cwd, check=True) |
| 98 | + except subprocess.CalledProcessError as exc: |
| 99 | + raise RuntimeError( |
| 100 | + f"[webui-build] command failed ({exc.returncode}): {' '.join(cmd)}" |
| 101 | + ) from exc |
0 commit comments