|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -from . import skbuild |
| 3 | +import pathlib |
| 4 | +from collections.abc import MutableMapping, Sequence |
| 5 | +from importlib.metadata import EntryPoint |
4 | 6 |
|
| 7 | +import click |
5 | 8 |
|
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): |
7 | 20 | """
|
8 |
| - Entry point to skbuild command. |
| 21 | + Lazy loader for click commands. Based on Click's documentation, but uses |
| 22 | + EntryPoints. |
9 | 23 | """
|
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 |
11 | 80 |
|
12 | 81 |
|
13 | 82 | if __name__ == "__main__":
|
14 |
| - run_cli() |
| 83 | + skbuild() |
0 commit comments