Skip to content

Commit cc4a9dd

Browse files
authored
Show versions of registered plugins (#30)
* Use hatchling for the build backend * Show versions of plugin * Use lower case
1 parent 143d0ed commit cc4a9dd

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

pyodide_cli/app.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
2-
from importlib.metadata import EntryPoint
2+
from functools import cache
3+
from importlib.metadata import Distribution, EntryPoint
34
from importlib.metadata import distribution as importlib_distribution
45
from importlib.metadata import entry_points
56

@@ -16,16 +17,29 @@
1617

1718

1819
def version_callback(value: bool):
19-
if value:
20-
typer.echo(f"Pyodide CLI Version: {__version__}")
21-
raise typer.Exit()
20+
if not value:
21+
return
22+
23+
typer.echo(f"pyodide CLI version: {__version__}")
24+
25+
eps = entry_points(group="pyodide.cli")
26+
# filter out duplicate pkgs
27+
pkgs = {_entrypoint_to_pkgname(ep): _entrypoint_to_version(ep) for ep in eps}
28+
for pkg, version in pkgs.items():
29+
typer.echo(f"{pkg} version: {version}")
30+
31+
raise typer.Exit()
2232

2333

2434
@app.callback(no_args_is_help=True)
2535
def callback(
2636
ctx: typer.Context,
2737
version: bool = typer.Option(
28-
None, "--version", callback=version_callback, is_eager=True
38+
None,
39+
"--version",
40+
callback=version_callback,
41+
is_eager=True,
42+
help="Show the version of the Pyodide CLI",
2943
),
3044
):
3145
"""A command line interface for Pyodide.
@@ -36,14 +50,26 @@ def callback(
3650
pass
3751

3852

39-
def _entrypoint_to_pkgname(entrypoint: EntryPoint) -> str:
40-
"""Find package name from entrypoint"""
41-
53+
@cache
54+
def _entrypoint_to_distribution(entrypoint: EntryPoint) -> Distribution:
55+
"""Find package distribution from entrypoint"""
4256
top_level = entrypoint.value.split(".")[0]
4357
dist = importlib_distribution(top_level)
58+
return dist
59+
60+
61+
def _entrypoint_to_pkgname(entrypoint: EntryPoint) -> str:
62+
"""Find package name from entrypoint"""
63+
dist = _entrypoint_to_distribution(entrypoint)
4464
return dist.metadata["name"]
4565

4666

67+
def _entrypoint_to_version(entrypoint: EntryPoint) -> str:
68+
"""Find package version from entrypoint"""
69+
dist = _entrypoint_to_distribution(entrypoint)
70+
return dist.metadata["version"]
71+
72+
4773
def _inject_origin(docstring: str, origin: str) -> str:
4874
return f"{docstring}\n\n{origin}"
4975

pyodide_cli/tests/test_cli.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ def plugins():
1919

2020
def test_cli_help():
2121
output = check_output(["pyodide", "--help"]).decode("utf-8")
22-
msg = "A command line interface for Pyodide."
23-
assert msg in output
22+
assert "A command line interface for Pyodide." in output
23+
24+
25+
def test_cli_version(plugins):
26+
output = check_output(["pyodide", "--version"]).decode("utf-8")
27+
assert "pyodide CLI version:" in output
28+
assert "plugin-test version: 1.0.0" in output
2429

2530

2631
def test_click_click_object_defintion():

0 commit comments

Comments
 (0)