Skip to content

Commit 65cc471

Browse files
committed
feat: add Hatchling build hook for generating Java stubs
1 parent 71f761e commit 65cc471

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

pyproject.toml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ classifiers = [
3232

3333
# NB: Keep this in sync with environment.yml AND dev-environment.yml!
3434
requires-python = ">=3.9"
35-
dependencies = [
36-
"jpype1 >= 1.3.0",
37-
"jgo",
38-
"cjdk",
39-
"stubgenj",
40-
]
35+
dependencies = ["jpype1 >= 1.3.0", "jgo", "cjdk", "stubgenj"]
4136

4237
[project.optional-dependencies]
4338
# NB: Keep this in sync with dev-environment.yml!
@@ -51,12 +46,16 @@ dev = [
5146
"pandas",
5247
"ruff",
5348
"toml",
54-
"validate-pyproject[all]"
49+
"validate-pyproject[all]",
5550
]
5651

5752
[project.scripts]
5853
scyjava-stubgen = "scyjava._stubs._cli:main"
5954

55+
[project.entry-points.hatch]
56+
mypyc = "scyjava._stubs._hatchling"
57+
58+
6059
[project.urls]
6160
homepage = "https://github.com/scijava/scyjava"
6261
documentation = "https://github.com/scijava/scyjava/blob/main/README.md"

src/scyjava/_stubs/_hatchling.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Hatchling build hook for generating Java stubs."""
2+
3+
import shutil
4+
from pathlib import Path
5+
6+
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
7+
from hatchling.plugin import hookimpl
8+
9+
10+
from scyjava._stubs._genstubs import generate_stubs
11+
12+
13+
class ScyjavaBuildHook(BuildHookInterface):
14+
"""Custom build hook for generating Java stubs."""
15+
16+
PLUGIN_NAME = "scyjava"
17+
18+
def initialize(self, version: str, build_data: dict) -> None:
19+
"""Initialize the build hook with the version and build data."""
20+
breakpoint()
21+
if self.target_name != "wheel":
22+
return
23+
dest = Path(self.root, "src")
24+
shutil.rmtree(dest, ignore_errors=True) # remove the old stubs
25+
26+
# actually build the stubs
27+
coord = f"{self.config['maven_coord']}:{self.metadata.version}"
28+
prefixes = self.config.get("prefixes", [])
29+
generate_stubs(endpoints=[coord], prefixes=prefixes, output_dir=dest)
30+
31+
# add all packages to the build config
32+
packages = [str(x.relative_to(self.root)) for x in dest.iterdir()]
33+
self.build_config.target_config.setdefault("packages", packages)
34+
35+
36+
@hookimpl
37+
def hatch_register_build_hook():
38+
return ScyjavaBuildHook

0 commit comments

Comments
 (0)