Skip to content

CargoRunner

Base classes

Name Children Inherits
TaskRunner
anyenv.task_runners._base
Base class for task runner configurations.

â‹” Inheritance diagram

graph TD
  3700369045["_cargo.CargoRunner"]
  3225203877["_base.TaskRunner"]
  2751680898["builtins.object"]
  3225203877 --> 3700369045
  2751680898 --> 3225203877

🛈 DocStrings

Bases: TaskRunner

Task runner for Cargo (Rust) aliases and xtask.

Supports: - Aliases defined in .cargo/config.toml or .cargo/config - xtask convention (workspace with xtask crate)

Source code in src/anyenv/task_runners/_cargo.py
@dataclass
class CargoRunner(TaskRunner):
    """Task runner for Cargo (Rust) aliases and xtask.

    Supports:
    - Aliases defined in `.cargo/config.toml` or `.cargo/config`
    - xtask convention (workspace with xtask crate)
    """

    id: ClassVar = "cargo"
    website: ClassVar = "https://doc.rust-lang.org/cargo/"
    filenames: ClassVar = [".cargo/config.toml", ".cargo/config", "xtask/Cargo.toml"]
    help_cmd: ClassVar = ["cargo", "--list"]
    logo: ClassVar[str | None] = "https://www.rust-lang.org/logos/rust-logo-512x512.png"
    # Pattern for TOML alias: name = "command" or name = ["cmd", "args"]

    async def detect(self, fs: AsyncFileSystem, cwd: str) -> bool:
        """Check if this task runner is used in the given directory."""
        # Check for cargo config with aliases
        for config_name in (".cargo/config.toml", ".cargo/config"):
            filepath = posixpath.join(cwd, config_name)
            try:
                if await fs._isfile(filepath):  # noqa: SLF001
                    content = await self._read_file(fs, filepath)
                    if "[alias]" in content:
                        return True
            except Exception:  # noqa: BLE001
                pass

        xtask_cargo = posixpath.join(cwd, "xtask", "Cargo.toml")  # Check for xtask crate
        try:
            if await fs._isfile(xtask_cargo):  # noqa: SLF001
                return True
        except Exception:  # noqa: BLE001
            pass

        return False

    async def get_config_file(self, fs: AsyncFileSystem, cwd: str) -> str | None:
        """Get the cargo config file path if it exists."""
        for config_name in (".cargo/config.toml", ".cargo/config"):
            filepath = posixpath.join(cwd, config_name)
            try:
                if await fs._isfile(filepath):  # noqa: SLF001
                    return filepath
            except Exception:  # noqa: BLE001
                pass
        return None

    async def get_tasks(self, fs: AsyncFileSystem, cwd: str) -> list[TaskInfo]:
        """Parse cargo config and xtask to return available tasks."""
        tasks: list[TaskInfo] = []
        # Get aliases from cargo config
        tasks.extend(await self._get_aliases(fs, cwd))
        # Get xtask commands
        tasks.extend(await self._get_xtask_commands(fs, cwd))
        return sorted(tasks, key=lambda t: t.name)

    async def _get_aliases(self, fs: AsyncFileSystem, cwd: str) -> list[TaskInfo]:
        """Parse aliases from .cargo/config.toml."""
        config_file = await self.get_config_file(fs, cwd)
        if config_file is None:
            return []

        content = await self._read_file(fs, config_file)
        return _parse_cargo_config(content)

    async def _get_xtask_commands(self, fs: AsyncFileSystem, cwd: str) -> list[TaskInfo]:
        """Detect xtask commands from xtask/src/main.rs."""
        xtask_main = posixpath.join(cwd, "xtask", "src", "main.rs")
        try:
            if not await fs._isfile(xtask_main):  # noqa: SLF001
                return []
        except Exception:  # noqa: BLE001
            return []

        content = await self._read_file(fs, xtask_main)
        return _parse_xtask_main(content)

    def get_run_command(self, task_name: str, *args: str) -> list[str]:
        """Get the cargo command for a specific task.

        Args:
            task_name: Name of the task (alias or xtask:command)
            *args: Additional arguments
        """
        if task_name.startswith("xtask:"):
            # xtask command
            cmd = task_name.removeprefix("xtask:")
            return ["cargo", "xtask", cmd, *args]
        return ["cargo", task_name, *args]  # Alias

detect async

detect(fs: AsyncFileSystem, cwd: str) -> bool

Check if this task runner is used in the given directory.

Source code in src/anyenv/task_runners/_cargo.py
async def detect(self, fs: AsyncFileSystem, cwd: str) -> bool:
    """Check if this task runner is used in the given directory."""
    # Check for cargo config with aliases
    for config_name in (".cargo/config.toml", ".cargo/config"):
        filepath = posixpath.join(cwd, config_name)
        try:
            if await fs._isfile(filepath):  # noqa: SLF001
                content = await self._read_file(fs, filepath)
                if "[alias]" in content:
                    return True
        except Exception:  # noqa: BLE001
            pass

    xtask_cargo = posixpath.join(cwd, "xtask", "Cargo.toml")  # Check for xtask crate
    try:
        if await fs._isfile(xtask_cargo):  # noqa: SLF001
            return True
    except Exception:  # noqa: BLE001
        pass

    return False

get_config_file async

get_config_file(fs: AsyncFileSystem, cwd: str) -> str | None

Get the cargo config file path if it exists.

Source code in src/anyenv/task_runners/_cargo.py
async def get_config_file(self, fs: AsyncFileSystem, cwd: str) -> str | None:
    """Get the cargo config file path if it exists."""
    for config_name in (".cargo/config.toml", ".cargo/config"):
        filepath = posixpath.join(cwd, config_name)
        try:
            if await fs._isfile(filepath):  # noqa: SLF001
                return filepath
        except Exception:  # noqa: BLE001
            pass
    return None

get_run_command

get_run_command(task_name: str, *args: str) -> list[str]

Get the cargo command for a specific task.

Parameters:

Name Type Description Default
task_name str

Name of the task (alias or xtask:command)

required
*args str

Additional arguments

()
Source code in src/anyenv/task_runners/_cargo.py
def get_run_command(self, task_name: str, *args: str) -> list[str]:
    """Get the cargo command for a specific task.

    Args:
        task_name: Name of the task (alias or xtask:command)
        *args: Additional arguments
    """
    if task_name.startswith("xtask:"):
        # xtask command
        cmd = task_name.removeprefix("xtask:")
        return ["cargo", "xtask", cmd, *args]
    return ["cargo", task_name, *args]  # Alias

get_tasks async

get_tasks(fs: AsyncFileSystem, cwd: str) -> list[TaskInfo]

Parse cargo config and xtask to return available tasks.

Source code in src/anyenv/task_runners/_cargo.py
async def get_tasks(self, fs: AsyncFileSystem, cwd: str) -> list[TaskInfo]:
    """Parse cargo config and xtask to return available tasks."""
    tasks: list[TaskInfo] = []
    # Get aliases from cargo config
    tasks.extend(await self._get_aliases(fs, cwd))
    # Get xtask commands
    tasks.extend(await self._get_xtask_commands(fs, cwd))
    return sorted(tasks, key=lambda t: t.name)

:fa-brands-github: Show source on GitHub