hatch_build.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """Custom build hook for Hatch."""
  2. import importlib.util
  3. import pathlib
  4. import subprocess
  5. import sys
  6. from typing import Any
  7. from hatchling.builders.hooks.plugin.interface import BuildHookInterface
  8. class CustomBuilder(BuildHookInterface):
  9. """Custom build hook for Hatch."""
  10. PLUGIN_NAME = "custom"
  11. def marker(self) -> pathlib.Path:
  12. """Get the marker file path.
  13. Returns:
  14. The marker file path.
  15. """
  16. return (
  17. pathlib.Path(self.directory)
  18. / f".reflex-{self.metadata.version}.pyi_generated"
  19. )
  20. def finalize(
  21. self, version: str, build_data: dict[str, Any], artifact_path: str
  22. ) -> None:
  23. """Finalize the build process.
  24. Args:
  25. version: The version of the package.
  26. build_data: The build data.
  27. artifact_path: The path to the artifact.
  28. """
  29. if self.marker().exists():
  30. return
  31. if importlib.util.find_spec("pre_commit") and importlib.util.find_spec("toml"):
  32. import json
  33. import toml
  34. import yaml
  35. reflex_dir = pathlib.Path(__file__).parent.parent
  36. pre_commit_config = json.loads(
  37. json.dumps(
  38. toml.load(reflex_dir / "pyproject.toml")["tool"]["pre-commit"]
  39. )
  40. )
  41. (reflex_dir / ".pre-commit-config.yaml").write_text(
  42. yaml.dump(pre_commit_config), encoding="utf-8"
  43. )
  44. if not (pathlib.Path(self.root) / "scripts").exists():
  45. return
  46. for file in (pathlib.Path(self.root) / "reflex").rglob("**/*.pyi"):
  47. file.unlink(missing_ok=True)
  48. subprocess.run(
  49. [sys.executable, "-m", "reflex.utils.pyi_generator"],
  50. check=True,
  51. )
  52. self.marker().touch()