Skip to content

Commit 1a7352d

Browse files
committed
refactor: restructure a little and add tests
Signed-off-by: Henry Schreiner <[email protected]>
1 parent ac6fdfc commit 1a7352d

File tree

11 files changed

+101
-96
lines changed

11 files changed

+101
-96
lines changed

docs/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dynamic_metadata
1313
```
1414

1515
```{eval-rst}
16-
.. click:: scikit_build_core.cli.main:skbuild
16+
.. click:: scikit_build_core.cli.__main__:skbuild
1717
:prog: skbuild
1818
1919
```

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Discussions = "https://github.com/scikit-build/scikit-build-cli/discussions"
5959
Changelog = "https://github.com/scikit-build/scikit-build-cli/releases"
6060

6161
[project.scripts]
62-
skbuild = "scikit_build_cli.__main__:run_cli"
62+
skbuild = "scikit_build_cli.__main__:skbuild"
6363

6464
[project.entry-points."skbuild.commands"]
6565
build = "scikit_build_cli.commands.build:build"

src/scikit_build_cli/__init__.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@
22

33
from ._version import version as __version__
44

5-
# Load all subcommands
6-
# Load and expose the main CLI interface
7-
from .main import skbuild
8-
9-
__all__: list[str] = ["skbuild", "__version__"]
5+
__all__: list[str] = ["__version__"]

src/scikit_build_cli/__main__.py

+74-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,83 @@
11
from __future__ import annotations
22

3-
from . import skbuild
3+
import pathlib
4+
from collections.abc import MutableMapping, Sequence
5+
from importlib.metadata import EntryPoint
46

7+
import click
58

6-
def run_cli() -> None:
9+
from . import __version__
10+
from ._compat.importlib import metadata
11+
12+
__all__ = ["skbuild"]
13+
14+
15+
def __dir__() -> list[str]:
16+
return __all__
17+
18+
19+
class LazyGroup(click.Group):
720
"""
8-
Entry point to skbuild command.
21+
Lazy loader for click commands. Based on Click's documentation, but uses
22+
EntryPoints.
923
"""
10-
skbuild()
24+
25+
def __init__(
26+
self,
27+
name: str | None = None,
28+
commands: MutableMapping[str, click.Command]
29+
| Sequence[click.Command]
30+
| None = None,
31+
*,
32+
lazy_subcommands: Sequence[EntryPoint] = (),
33+
**kwargs: object,
34+
):
35+
super().__init__(name, commands, **kwargs)
36+
self.lazy_subcommands = {v.name: v for v in lazy_subcommands}
37+
38+
def list_commands(self, ctx: click.Context) -> list[str]:
39+
return sorted([*super().list_commands(ctx), *self.lazy_subcommands])
40+
41+
def get_command(self, ctx: click.Context, cmd_name: str) -> click.Command | None:
42+
if cmd_name in self.lazy_subcommands:
43+
return self._lazy_load(cmd_name)
44+
return super().get_command(ctx, cmd_name)
45+
46+
def _lazy_load(self, cmd_name: str) -> click.Command:
47+
ep = self.lazy_subcommands[cmd_name]
48+
cmd_object = ep.load()
49+
if not isinstance(cmd_object, click.Command):
50+
msg = f"Lazy loading of {ep} failed by returning a non-command object"
51+
raise ValueError(msg)
52+
return cmd_object
53+
54+
55+
# Add all plugin commands.
56+
CMDS = list(metadata.entry_points(group="skbuild.commands"))
57+
58+
59+
@click.group("skbuild", cls=LazyGroup, lazy_subcommands=CMDS)
60+
@click.version_option(__version__)
61+
@click.help_option("--help", "-h")
62+
@click.option(
63+
"--root",
64+
"-r",
65+
type=click.Path(
66+
exists=True,
67+
file_okay=False,
68+
dir_okay=True,
69+
writable=True,
70+
path_type=pathlib.Path,
71+
),
72+
help="Path to the Python project's root",
73+
)
74+
@click.pass_context
75+
def skbuild(ctx: click.Context, root: pathlib.Path) -> None: # noqa: ARG001
76+
"""
77+
scikit-build Main CLI interface
78+
"""
79+
# TODO: Add specific implementations
1180

1281

1382
if __name__ == "__main__":
14-
run_cli()
83+
skbuild()

src/scikit_build_cli/commands/build.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ def __dir__() -> list[str]:
1818

1919
@click.command()
2020
@_build_dir
21+
@click.help_option("--help", "-h")
2122
@click.pass_context
2223
def build(ctx: click.Context, build_dir: Path) -> None: # noqa: ARG001
2324
"""
24-
Run cmake build step
25+
Run CMake build step
2526
"""
2627
# TODO: Add specific implementations

src/scikit_build_cli/commands/configure.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ def __dir__() -> list[str]:
1818

1919
@click.command()
2020
@_build_dir
21+
@click.help_option("--help", "-h")
2122
@click.pass_context
2223
def configure(ctx: click.Context, build_dir: Path) -> None: # noqa: ARG001
2324
"""
24-
Run cmake configure step
25+
Run CMake configure step
2526
"""
2627
# TODO: Add specific implementations

src/scikit_build_cli/commands/dynamic_metadata.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def __dir__() -> list[str]:
1010

1111

1212
@click.command()
13+
@click.help_option("--help", "-h")
1314
@click.pass_context
1415
def dynamic_metadata(ctx: click.Context) -> None: # noqa: ARG001
1516
"""

src/scikit_build_cli/commands/install.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ def __dir__() -> list[str]:
1818

1919
@click.command()
2020
@_build_dir
21+
@click.help_option("--help", "-h")
2122
@click.pass_context
2223
def install(ctx: click.Context, build_dir: Path) -> None: # noqa: ARG001
2324
"""
24-
Run cmake install step
25+
Run CMake install step
2526
"""
2627
# TODO: Add specific implementations

src/scikit_build_cli/commands/metadata.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def __dir__() -> list[str]:
1010

1111

1212
@click.command()
13+
@click.help_option("--help", "-h")
1314
@click.pass_context
1415
def metadata(ctx: click.Context) -> None: # noqa: ARG001
1516
"""

src/scikit_build_cli/main.py

-78
This file was deleted.

tests/test_package.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,21 @@
22

33
import importlib.metadata
44

5-
import scikit_build_cli as m
5+
from click.testing import CliRunner
66

7+
import scikit_build_cli
8+
import scikit_build_cli.__main__
79

8-
def test_version():
9-
assert importlib.metadata.version("scikit_build_cli") == m.__version__
10+
11+
def test_version() -> None:
12+
assert (
13+
importlib.metadata.version("scikit_build_cli") == scikit_build_cli.__version__
14+
)
15+
16+
17+
def test_help_text() -> None:
18+
runner = CliRunner()
19+
result = runner.invoke(scikit_build_cli.__main__.skbuild, ["--help"])
20+
assert result.exit_code == 0
21+
assert "Run CMake build step" in result.output
22+
assert "Run CMake configure step" in result.output

0 commit comments

Comments
 (0)