|
| 1 | +import importlib.metadata |
| 2 | +import subprocess |
| 3 | +from pathlib import Path |
| 4 | +from typing import Annotated |
| 5 | +from importlib.metadata import PackageNotFoundError, version |
| 6 | + |
| 7 | +import requests |
| 8 | +import typer |
| 9 | +from packaging.version import Version |
| 10 | +from requests.exceptions import HTTPError |
| 11 | +from rich.console import Console |
| 12 | +from rich.table import Table |
| 13 | + |
| 14 | + |
| 15 | +plugin = typer.Typer() |
| 16 | + |
| 17 | + |
| 18 | +@plugin.command(name="list") |
| 19 | +def list_plugins(): |
| 20 | + """List installed plugins.""" |
| 21 | + |
| 22 | + console = Console() |
| 23 | + table = Table("Name", "Version", title="Installed CLI plugins", min_width=50, highlight=True) |
| 24 | + |
| 25 | + for name, plugin_info in sorted(find_plugins().items(), key=lambda x: x[0]): |
| 26 | + table.add_row(name, plugin_info["version"]) |
| 27 | + |
| 28 | + if table.rows: |
| 29 | + print() |
| 30 | + console.print(table) |
| 31 | + else: |
| 32 | + typer.secho("No plugins installed.", fg=typer.colors.BRIGHT_BLACK) |
| 33 | + |
| 34 | + |
| 35 | +@plugin.command() |
| 36 | +def install( |
| 37 | + name: Annotated[ |
| 38 | + str, |
| 39 | + typer.Argument( |
| 40 | + help="Name of the plugin to install, excluding the `minimal-pba-cli-plugin-` prefix." |
| 41 | + ), |
| 42 | + ], |
| 43 | +): |
| 44 | + """Install a published plugin.""" |
| 45 | + |
| 46 | + installed_plugins = find_plugins() |
| 47 | + already_installed = name in installed_plugins |
| 48 | + version_to_install: str | Version | None = None |
| 49 | + upgrade = False |
| 50 | + |
| 51 | + if already_installed: |
| 52 | + typer.secho(f"Plugin '{name}' is already installed.", fg=typer.colors.BRIGHT_YELLOW) |
| 53 | + upgrade = typer.confirm("Do you want to upgrade to the latest version?") |
| 54 | + |
| 55 | + if already_installed and not upgrade: |
| 56 | + typer.confirm("Do you want to reinstall the plugin at its current version?", abort=True) |
| 57 | + version_to_install = installed_plugins[name]["version"] |
| 58 | + |
| 59 | + if not already_installed or upgrade: |
| 60 | + try: |
| 61 | + _, version_to_install, _ = _get_latest_version(f"minimal-pba-cli-plugin-{name}") |
| 62 | + except HTTPError as e: |
| 63 | + if e.response is not None and e.response.status_code == 404: |
| 64 | + raise typer.BadParameter( |
| 65 | + f"Plugin '{name}' not found." |
| 66 | + ) from None |
| 67 | + |
| 68 | + typer.echo(f"Installing plugin '{name}' version '{version_to_install}'...") |
| 69 | + |
| 70 | + args = [ |
| 71 | + "pipx", |
| 72 | + "inject", |
| 73 | + "minimal-pba-cli", |
| 74 | + f"minimal-pba-cli-plugin-{name}=={version_to_install}", |
| 75 | + ] |
| 76 | + if already_installed: |
| 77 | + args.append("--force") |
| 78 | + |
| 79 | + _run_external_subprocess(args) |
| 80 | + |
| 81 | + |
| 82 | +@plugin.command() |
| 83 | +def install_local(path: Annotated[Path, typer.Argument(help="Path to the plugin directory.")]): |
| 84 | + """Install a local plugin.""" |
| 85 | + |
| 86 | + typer.echo(f"Installing plugin from '{path}'...") |
| 87 | + _run_external_subprocess([ |
| 88 | + "pipx", |
| 89 | + "inject", |
| 90 | + "--editable", |
| 91 | + "--force", |
| 92 | + "minimal-pba-cli", |
| 93 | + str(path), |
| 94 | + ]) |
| 95 | + |
| 96 | + |
| 97 | +@plugin.command() |
| 98 | +def uninstall(name: Annotated[str, typer.Argument(help="Name of the plugin to uninstall, excluding the `minimal-pba-cli-plugin-` prefix.")]): |
| 99 | + """Uninstall a plugin.""" |
| 100 | + |
| 101 | + typer.echo(f"Uninstalling plugin '{name}'...") |
| 102 | + _run_external_subprocess([ |
| 103 | + "pipx", |
| 104 | + "uninject", |
| 105 | + "minimal-pba-cli", |
| 106 | + f"minimal-pba-cli-plugin-{name}", |
| 107 | + ]) |
| 108 | + |
| 109 | + |
| 110 | +def _get_installed_version(name: str) -> Version | None: |
| 111 | + """Determine the currently-installed version of the specified package.""" |
| 112 | + |
| 113 | + try: |
| 114 | + return Version(version(name)) |
| 115 | + except PackageNotFoundError: |
| 116 | + return None |
| 117 | + |
| 118 | + |
| 119 | +def _get_latest_version(name: str) -> tuple[Version | None, Version, bool]: |
| 120 | + """Get the latest published version of a package.""" |
| 121 | + |
| 122 | + url = f"https://pypi.org/pypi/{name}/json" |
| 123 | + response = requests.get(url) |
| 124 | + |
| 125 | + data = response.json() |
| 126 | + latest = Version(data["info"]["version"]) |
| 127 | + current = _get_installed_version(name) |
| 128 | + return current, latest, current < latest if current else True |
| 129 | + |
| 130 | + |
| 131 | +def find_plugins() -> dict[str, dict[str, str]]: |
| 132 | + """Discover installed packages that provide CLI plugins.""" |
| 133 | + |
| 134 | + plugins = {} |
| 135 | + |
| 136 | + for installed_package in importlib.metadata.distributions(): |
| 137 | + for entry_point in installed_package.entry_points: |
| 138 | + if entry_point.group == "minimal_pba_cli": |
| 139 | + plugins[entry_point.name] = { |
| 140 | + "path": entry_point.value, |
| 141 | + "version": installed_package.version, |
| 142 | + } |
| 143 | + |
| 144 | + return plugins |
| 145 | + |
| 146 | + |
| 147 | +def _run_external_subprocess(args: list[str]) -> subprocess.CompletedProcess: |
| 148 | + """Run an external subprocess and return the result.""" |
| 149 | + |
| 150 | + result = subprocess.run(args, capture_output=True, encoding="utf-8") |
| 151 | + |
| 152 | + if result.stdout: |
| 153 | + typer.echo(result.stdout) |
| 154 | + |
| 155 | + if result.stderr: |
| 156 | + typer.echo(result.stderr, err=True) |
| 157 | + |
| 158 | + if result.returncode != 0: |
| 159 | + raise typer.Exit(code=result.returncode) |
| 160 | + |
| 161 | + return result |
0 commit comments