|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from logging import getLogger |
| 4 | +from os import getenv |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from hatchling.builders.hooks.plugin.interface import BuildHookInterface |
| 8 | + |
| 9 | +from .structs import HatchJsBuildConfig, HatchJsBuildPlan |
| 10 | +from .utils import import_string |
| 11 | + |
| 12 | +__all__ = ("HatchJsBuildHook",) |
| 13 | + |
| 14 | + |
| 15 | +class HatchJsBuildHook(BuildHookInterface[HatchJsBuildConfig]): |
| 16 | + """The hatch-js build hook.""" |
| 17 | + |
| 18 | + PLUGIN_NAME = "hatch-js" |
| 19 | + _logger = getLogger(__name__) |
| 20 | + |
| 21 | + def initialize(self, version: str, build_data: dict[str, Any]) -> None: |
| 22 | + """Initialize the plugin.""" |
| 23 | + # Log some basic information |
| 24 | + project_name = self.metadata.config["project"]["name"] |
| 25 | + self._logger.info("Initializing hatch-js plugin version %s", version) |
| 26 | + self._logger.info(f"Running hatch-js: {project_name}") |
| 27 | + |
| 28 | + # Only run if creating wheel |
| 29 | + # TODO: Add support for specify sdist-plan |
| 30 | + if self.target_name != "wheel": |
| 31 | + self._logger.info("ignoring target name %s", self.target_name) |
| 32 | + return |
| 33 | + |
| 34 | + # Skip if SKIP_HATCH_JS is set |
| 35 | + # TODO: Support CLI once https://github.com/pypa/hatch/pull/1743 |
| 36 | + if getenv("SKIP_HATCH_JS"): |
| 37 | + self._logger.info("Skipping the build hook since SKIP_HATCH_JS was set") |
| 38 | + return |
| 39 | + |
| 40 | + # Get build config class or use default |
| 41 | + build_config_class = import_string(self.config["build-config-class"]) if "build-config-class" in self.config else HatchJsBuildConfig |
| 42 | + |
| 43 | + # Instantiate build config |
| 44 | + config = build_config_class(name=project_name, **self.config) |
| 45 | + |
| 46 | + # Get build plan class or use default |
| 47 | + build_plan_class = import_string(self.config["build-plan-class"]) if "build-plan-class" in self.config else HatchJsBuildPlan |
| 48 | + |
| 49 | + # Instantiate builder |
| 50 | + build_plan = build_plan_class(**config.model_dump()) |
| 51 | + |
| 52 | + # Generate commands |
| 53 | + build_plan.generate() |
| 54 | + |
| 55 | + # Log commands if in verbose mode |
| 56 | + if config.verbose: |
| 57 | + for command in build_plan.commands: |
| 58 | + self._logger.warning(command) |
| 59 | + |
| 60 | + # Execute build plan |
| 61 | + build_plan.execute() |
| 62 | + |
| 63 | + # Perform any cleanup actions |
| 64 | + build_plan.cleanup() |
| 65 | + |
| 66 | + # if build_plan.libraries: |
| 67 | + # # force include libraries |
| 68 | + # for library in build_plan.libraries: |
| 69 | + # name = library.get_qualified_name(build_plan.platform.platform) |
| 70 | + # build_data["force_include"][name] = name |
| 71 | + |
| 72 | + # build_data["pure_python"] = False |
| 73 | + # machine = platform_machine() |
| 74 | + # version_major = version_info.major |
| 75 | + # version_minor = version_info.minor |
| 76 | + # if "darwin" in sys_platform: |
| 77 | + # os_name = "macosx_11_0" |
| 78 | + # elif "linux" in sys_platform: |
| 79 | + # os_name = "linux" |
| 80 | + # else: |
| 81 | + # os_name = "win" |
| 82 | + # if all([lib.py_limited_api for lib in build_plan.libraries]): |
| 83 | + # build_data["tag"] = f"cp{version_major}{version_minor}-abi3-{os_name}_{machine}" |
| 84 | + # else: |
| 85 | + # build_data["tag"] = f"cp{version_major}{version_minor}-cp{version_major}{version_minor}-{os_name}_{machine}" |
| 86 | + # else: |
| 87 | + # build_data["pure_python"] = False |
| 88 | + # machine = platform_machine() |
| 89 | + # version_major = version_info.major |
| 90 | + # version_minor = version_info.minor |
| 91 | + # # TODO abi3 |
| 92 | + # if "darwin" in sys_platform: |
| 93 | + # os_name = "macosx_11_0" |
| 94 | + # elif "linux" in sys_platform: |
| 95 | + # os_name = "linux" |
| 96 | + # else: |
| 97 | + # os_name = "win" |
| 98 | + # build_data["tag"] = f"cp{version_major}{version_minor}-cp{version_major}{version_minor}-{os_name}_{machine}" |
| 99 | + |
| 100 | + # # force include libraries |
| 101 | + # for path in Path(".").rglob("*"): |
| 102 | + # if path.is_dir(): |
| 103 | + # continue |
| 104 | + # if str(path).startswith(str(build_plan.cmake.build)) or str(path).startswith("dist"): |
| 105 | + # continue |
| 106 | + # if path.suffix in (".pyd", ".dll", ".so", ".dylib"): |
| 107 | + # build_data["force_include"][str(path)] = str(path) |
| 108 | + |
| 109 | + # for path in build_data["force_include"]: |
| 110 | + # self._logger.warning(f"Force include: {path}") |
0 commit comments