Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 27 additions & 47 deletions nb_cli/cli/commands/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

from nb_cli import _
from nb_cli.config import GLOBAL_CONFIG
from nb_cli.exceptions import NoSelectablePackageError
from nb_cli.cli.utils import find_exact_package, format_package_results
from nb_cli.exceptions import ProcessExecutionError, NoSelectablePackageError
from nb_cli.cli import (
CLI_DEFAULT_STYLE,
ClickAliasedGroup,
Expand All @@ -18,11 +18,9 @@
run_async,
)
from nb_cli.handlers import (
EnvironmentExecutor,
list_adapters,
create_adapter,
call_pip_update,
call_pip_install,
call_pip_uninstall,
list_installed_adapters,
)

Expand Down Expand Up @@ -205,11 +203,13 @@ async def install(
fg="yellow",
)

proc = await call_pip_install(
adapter.as_dependency(extras=extras, versioned=not no_restrict_version),
pip_args,
)
if await proc.wait() != 0:
executor = await EnvironmentExecutor.get()
try:
await executor.install(
adapter.as_requirement(extras=extras, versioned=not no_restrict_version),
extra_args=pip_args or (),
)
except ProcessExecutionError:
click.secho(
_(
"Errors occurred in installing adapter {adapter.name}\n"
Expand All @@ -218,8 +218,7 @@ async def install(
).format(adapter=adapter),
fg="red",
)
assert proc.returncode
ctx.exit(proc.returncode)
ctx.exit(1)

try:
GLOBAL_CONFIG.add_adapter(adapter)
Expand All @@ -230,17 +229,6 @@ async def install(
)
)

try:
GLOBAL_CONFIG.add_dependency(
adapter.as_dependency(extras=extras, versioned=not no_restrict_version)
)
except RuntimeError as e:
click.echo(
_("Failed to add adapter {adapter.name} to dependencies: {e}").format(
adapter=adapter, e=e
)
)


@adapter.command(
context_settings={"ignore_unknown_options": True}, help=_("Update nonebot adapter.")
Expand Down Expand Up @@ -281,8 +269,13 @@ async def update(
fg="yellow",
)

proc = await call_pip_update(adapter.project_link, pip_args)
if await proc.wait() != 0:
executor = await EnvironmentExecutor.get()
try:
await executor.install(
adapter.as_requirement(versioned=False),
extra_args=pip_args or (),
)
except ProcessExecutionError:
click.secho(
_("Errors occurred in updating adapter {adapter.name}. Aborted.").format(
adapter=adapter
Expand All @@ -291,15 +284,6 @@ async def update(
)
return

try:
GLOBAL_CONFIG.update_dependency(adapter)
except RuntimeError as e:
click.echo(
_("Failed to update adapter {adapter.name} to dependencies: {e}").format(
adapter=adapter, e=e
)
)


@adapter.command(
aliases=["remove"],
Expand All @@ -310,6 +294,11 @@ async def update(
@click.argument("pip_args", nargs=-1, default=None)
@run_async
async def uninstall(name: str | None, pip_args: list[str] | None):
extras: str | None = None
if name and "[" in name:
name, extras = name.split("[", 1)
extras = extras.rstrip("]")

try:
adapter = await find_exact_package(
_("Adapter name to uninstall:"),
Expand All @@ -322,20 +311,8 @@ async def uninstall(name: str | None, pip_args: list[str] | None):
click.echo(_("No installed adapter found to uninstall."))
return

extras: str | None = None
if name and "[" in name:
name, extras = name.split("[", 1)
extras = extras.rstrip("]")

try:
if extras is not None:
if not GLOBAL_CONFIG.remove_dependency(
adapter.as_dependency(extras=extras)
):
return
can_uninstall = GLOBAL_CONFIG.remove_adapter(adapter)
if can_uninstall:
GLOBAL_CONFIG.remove_dependency(adapter)
except RuntimeError as e:
click.echo(
_("Failed to remove adapter {adapter.name} from config: {e}").format(
Expand All @@ -345,8 +322,11 @@ async def uninstall(name: str | None, pip_args: list[str] | None):
return

if can_uninstall:
proc = await call_pip_uninstall(adapter.project_link, pip_args)
await proc.wait()
executor = await EnvironmentExecutor.get()
await executor.uninstall(
adapter.as_requirement(extras=extras, versioned=False),
extra_args=pip_args or (),
)


@adapter.command(aliases=["new"], help=_("Create a new nonebot adapter."))
Expand Down
88 changes: 32 additions & 56 deletions nb_cli/cli/commands/driver.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
from typing import cast

import click
from packaging.requirements import Requirement
from noneprompt import Choice, ListPrompt, InputPrompt, CancelledError

from nb_cli import _
from nb_cli.config import GLOBAL_CONFIG
from nb_cli.exceptions import ProcessExecutionError
from nb_cli.handlers import EnvironmentExecutor, list_drivers
from nb_cli.cli.utils import find_exact_package, format_package_results
from nb_cli.handlers import (
list_drivers,
call_pip_update,
call_pip_install,
call_pip_uninstall,
)
from nb_cli.cli import (
CLI_DEFAULT_STYLE,
ClickAliasedGroup,
Expand Down Expand Up @@ -153,25 +149,20 @@ async def install(
fg="yellow",
)

if driver.project_link:
proc = await call_pip_install(driver.project_link, pip_args)
if await proc.wait() != 0:
click.secho(
_(
"Errors occurred in installing driver {driver.name}. Aborted."
).format(driver=driver),
fg="red",
)
return

executor = await EnvironmentExecutor.get()
try:
GLOBAL_CONFIG.add_dependency(driver)
except RuntimeError as e:
click.echo(
_("Failed to add driver {driver.name} to dependencies: {e}").format(
driver=driver, e=e
)
await executor.install(
driver.as_requirement() if driver.project_link else Requirement("nonebot2"),
extra_args=pip_args or (),
)
except ProcessExecutionError:
click.secho(
_("Errors occurred in installing driver {driver.name}. Aborted.").format(
driver=driver
),
fg="red",
)
return


@driver.command(
Expand Down Expand Up @@ -210,25 +201,20 @@ async def update(
fg="yellow",
)

if driver.project_link:
proc = await call_pip_update(driver.project_link, pip_args)
if await proc.wait() != 0:
click.secho(
_("Errors occurred in updating driver {driver.name}. Aborted.").format(
driver=driver
),
fg="red",
)
return

executor = await EnvironmentExecutor.get()
try:
GLOBAL_CONFIG.update_dependency(driver)
except RuntimeError as e:
click.echo(
_("Failed to update driver {driver.name} to dependencies: {e}").format(
driver=driver, e=e
)
await executor.install(
driver.as_requirement() if driver.project_link else Requirement("nonebot2"),
extra_args=pip_args or (),
)
except ProcessExecutionError:
click.secho(
_("Errors occurred in updating driver {driver.name}. Aborted.").format(
driver=driver
),
fg="red",
)
return


@driver.command(
Expand All @@ -251,19 +237,9 @@ async def uninstall(name: str | None, pip_args: list[str] | None):
except CancelledError:
return

try:
GLOBAL_CONFIG.remove_dependency(driver)
GLOBAL_CONFIG.add_dependency("nonebot2") # hold a nonebot2 package
except RuntimeError as e:
click.echo(
_("Failed to remove driver {driver.name} from dependencies: {e}").format(
driver=driver, e=e
)
if driver.project_link:
executor = await EnvironmentExecutor.get()
await executor.uninstall(
driver.as_requirement(versioned=False), extra_args=pip_args or ()
)

if package := driver.project_link:
if package.startswith("nonebot2[") and package.endswith("]"):
package = package[9:-1]

proc = await call_pip_uninstall(package, pip_args)
await proc.wait()
await executor.install(Requirement("nonebot2"), extra_args=pip_args or ())
Loading