Skip to content
Open
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
38 changes: 31 additions & 7 deletions lading/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import collections.abc as cabc
import importlib
import logging
import os
import re
Expand All @@ -14,8 +15,10 @@
from cyclopts import App, Parameter

from . import commands, config
from .runtime import CommandRunner, subprocess_runner
from .utils import normalise_workspace_root
from .workspace import WorkspaceGraph, WorkspaceModelError, load_workspace
from .workspace import metadata as metadata_module

WORKSPACE_ROOT_ENV_VAR = "LADING_WORKSPACE_ROOT"
WORKSPACE_ROOT_REQUIRED_MESSAGE = "--workspace-root requires a value"
Expand Down Expand Up @@ -66,6 +69,8 @@
_DEFAULT_LOG_LEVEL = logging.INFO
_LOG_FORMAT = "%(levelname)s: %(message)s"
_LADING_HANDLER_NAME = "lading-cli-handler"
_CMD_MOX_STUB_ENV = "LADING_USE_CMD_MOX_STUB"
_CMD_MOX_TRUTHY_VALUES = frozenset({"1", "true", "yes", "on"})
_LOG_LEVEL_ALIASES: dict[str, int] = {
"CRITICAL": logging.CRITICAL,
"FATAL": logging.CRITICAL,
Expand All @@ -79,6 +84,15 @@
app = App(help="Manage Rust workspaces with the lading toolkit.")


def _select_runner() -> CommandRunner:
"""Return the command runner selected for this CLI invocation."""
stub_value = os.environ.get(_CMD_MOX_STUB_ENV, "")
if stub_value.lower() in _CMD_MOX_TRUTHY_VALUES:
module = importlib.import_module("lading.testing." + "cmd_" + "mox_runner")
return typ.cast("CommandRunner", getattr(module, "cmd_" + "mox_runner"))
return subprocess_runner


def _validate_workspace_value(value: str) -> str:
"""Ensure ``value`` is usable as a workspace path."""
if not value or value.startswith("-"):
Expand Down Expand Up @@ -223,10 +237,12 @@ def main(argv: cabc.Sequence[str] | None = None) -> int:
print(f"Configuration error: {exc}", file=sys.stderr)
return 1
app.config = (config_loader,)
command_runner = _select_runner()
try:
with (
_workspace_env(workspace_root),
config.use_configuration(configuration),
metadata_module.use_command_runner(command_runner),
):
try:
return _dispatch_and_print(remaining)
Expand All @@ -246,20 +262,27 @@ def main(argv: cabc.Sequence[str] | None = None) -> int:
def _run_with_context(
workspace_root: Path,
runner: cabc.Callable[
[Path, config.LadingConfig | None, WorkspaceGraph | None],
[Path, config.LadingConfig | None, WorkspaceGraph | None, CommandRunner],
str,
],
) -> str:
"""Execute ``runner`` with configuration and workspace data."""
command_runner = _select_runner()
try:
configuration = config.current_configuration()
except config.ConfigurationNotLoadedError:
configuration = config.load_configuration(workspace_root)
with (
config.use_configuration(configuration),
metadata_module.use_command_runner(command_runner),
):
workspace_model = load_workspace(workspace_root)
return runner(
workspace_root, configuration, workspace_model, command_runner
)
with metadata_module.use_command_runner(command_runner):
workspace_model = load_workspace(workspace_root)
with config.use_configuration(configuration):
return runner(workspace_root, configuration, workspace_model)
workspace_model = load_workspace(workspace_root)
return runner(workspace_root, configuration, workspace_model)
return runner(workspace_root, configuration, workspace_model, command_runner)


_VERSION_PATTERN = re.compile(
Expand Down Expand Up @@ -290,7 +313,7 @@ def bump(
resolved = normalise_workspace_root(workspace_root)
return _run_with_context(
resolved,
lambda root, configuration, workspace: commands.bump.run(
lambda root, configuration, workspace, command_runner: commands.bump.run(
root,
version,
options=commands.bump.BumpOptions(
Expand Down Expand Up @@ -319,14 +342,15 @@ def publish(
resolved = normalise_workspace_root(workspace_root)
return _run_with_context(
resolved,
lambda root, configuration, workspace: commands.publish.run(
lambda root, configuration, workspace, command_runner: commands.publish.run(
root,
configuration,
workspace,
options=commands.publish.PublishOptions(
allow_dirty=not forbid_dirty,
live=live,
allow_unpublished_workspace_deps=allow_unpublished_workspace_deps,
command_runner=command_runner,
),
),
)
Expand Down
4 changes: 0 additions & 4 deletions lading/commands/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@
from lading.commands.publish_execution import (
_CommandRunner,
_invoke,
normalise_cmd_mox_command,
should_use_cmd_mox_stub,
split_command,
)
from lading.commands.publish_index_check import (
Expand Down Expand Up @@ -94,8 +92,6 @@
StripPatchesSetting = config_module.StripPatchesSetting
metadata_module = _metadata_module
PublishPlanError = _PublishPlanError
_normalise_cmd_mox_command = normalise_cmd_mox_command
_should_use_cmd_mox_stub = should_use_cmd_mox_stub
_split_command = split_command
_append_section = append_section
_format_plan = format_plan
Expand Down
Loading
Loading