Skip to content

Commit

Permalink
perf: plugins help load
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Nov 1, 2024
1 parent aa1c02c commit 6c44506
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/ape_plugins/_cli.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import subprocess
import sys
from pathlib import Path
from typing import Any
from typing import TYPE_CHECKING, Any

import click
from packaging.version import Version

from ape.cli.options import ape_cli_context, skip_confirmation_option
from ape.logging import logger
from ape.plugins._utils import (
PIP_COMMAND,
ModifyPluginResultHandler,
PluginMetadata,
PluginMetadataList,
PluginType,
ape_version,
get_plugin_dists,
)
from ape.utils.misc import load_config

if TYPE_CHECKING:
from ape.plugins._utils import PluginMetadata


@click.group(short_help="Manage ape plugins")
Expand All @@ -33,7 +26,10 @@ def plugins_argument():
or plugins loaded from the local config file.
"""

def load_from_file(ctx, file_path: Path) -> list[PluginMetadata]:
def load_from_file(ctx, file_path: Path) -> list["PluginMetadata"]:
from ape.plugins._utils import PluginMetadata
from ape.utils.misc import load_config

if file_path.is_dir():
name_options = (
"ape-config.yaml",
Expand All @@ -55,6 +51,8 @@ def load_from_file(ctx, file_path: Path) -> list[PluginMetadata]:
return []

def callback(ctx, param, value: tuple[str]):
from ape.plugins._utils import PluginMetadata

res = []
if not value:
ctx.obj.abort("You must give at least one requirement to install.")
Expand Down Expand Up @@ -93,6 +91,8 @@ def upgrade_option(help: str = "", **kwargs):


def _display_all_callback(ctx, param, value):
from ape.plugins._utils import PluginType

return (
(PluginType.CORE, PluginType.INSTALLED, PluginType.THIRD_PARTY, PluginType.AVAILABLE)
if value
Expand All @@ -112,6 +112,8 @@ def _display_all_callback(ctx, param, value):
help="Display all plugins installed and available (including Core)",
)
def _list(cli_ctx, to_display):
from ape.plugins._utils import PluginMetadataList, PluginType

include_available = PluginType.AVAILABLE in to_display
metadata = PluginMetadataList.load(cli_ctx.plugin_manager, include_available=include_available)
if output := metadata.to_str(include=to_display):
Expand All @@ -128,7 +130,7 @@ def _list(cli_ctx, to_display):
@plugins_argument()
@skip_confirmation_option("Don't ask for confirmation to install the plugins")
@upgrade_option(help="Upgrade the plugin to the newest available version")
def install(cli_ctx, plugins: list[PluginMetadata], skip_confirmation: bool, upgrade: bool):
def install(cli_ctx, plugins: list["PluginMetadata"], skip_confirmation: bool, upgrade: bool):
"""Install plugins"""

failures_occurred = False
Expand Down Expand Up @@ -170,6 +172,7 @@ def install(cli_ctx, plugins: list[PluginMetadata], skip_confirmation: bool, upg
@skip_confirmation_option("Don't ask for confirmation to install the plugins")
def uninstall(cli_ctx, plugins, skip_confirmation):
"""Uninstall plugins"""
from ape.plugins._utils import ModifyPluginResultHandler

failures_occurred = False
did_warn_about_version = False
Expand Down Expand Up @@ -217,6 +220,7 @@ def update():
"""
Update Ape and all plugins to the next version
"""
from ape.plugins._utils import ape_version

_change_version(ape_version.next_version_range)

Expand Down Expand Up @@ -249,6 +253,8 @@ def _install(name, spec, exit_on_fail: bool = True) -> int:
Returns:
The process return-code.
"""
from ape.plugins._utils import PIP_COMMAND

arguments = [*PIP_COMMAND, "install", f"{name}{spec}", "--quiet"]

# Run the installation process and capture output for error checking
Expand Down Expand Up @@ -281,6 +287,8 @@ def _change_version(spec: str):
# This will also update core Ape.
# NOTE: It is possible plugins may depend on each other and may update in
# an order causing some error codes to pop-up, so we ignore those for now.
from ape.plugins._utils import get_plugin_dists

plugin_retcode = 0
for plugin in get_plugin_dists():
logger.info(f"Updating {plugin} ...")
Expand Down

0 comments on commit 6c44506

Please sign in to comment.