Skip to content

InvokeRunner

Base classes

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

⋔ Inheritance diagram

graph TD
  3093893472["_python.InvokeRunner"]
  3225203877["_base.TaskRunner"]
  2751680898["builtins.object"]
  3225203877 --> 3093893472
  2751680898 --> 3225203877

🛈 DocStrings

Bases: TaskRunner

Task runner for Invoke (pyinvoke.org).

Source code in src/anyenv/task_runners/_python.py
@dataclass
class InvokeRunner(TaskRunner):
    """Task runner for Invoke (pyinvoke.org)."""

    id: ClassVar[Literal["invoke"]] = "invoke"
    website: ClassVar[str] = "https://www.pyinvoke.org"
    filenames: ClassVar[list[str]] = [
        "tasks.py",
        "invoke.yaml",
        "invoke.yml",
        "invoke.json",
        "invoke.py",
    ]
    help_cmd: ClassVar[list[str]] = ["invoke", "--list"]
    logo: ClassVar[str | None] = None

    async def get_tasks(self, fs: AsyncFileSystem, cwd: str) -> list[TaskInfo]:
        """Parse tasks.py and return available tasks."""
        # Only parse tasks.py, not the config files
        tasks_file = posixpath.join(cwd, "tasks.py")
        try:
            if not await fs._isfile(tasks_file):  # noqa: SLF001
                return []
        except Exception:  # noqa: BLE001
            return []

        content = await self._read_file(fs, tasks_file)
        return self._parse_tasks(content)

    def _parse_tasks(self, content: str) -> list[TaskInfo]:
        """Parse tasks.py to extract @task decorated functions."""
        try:
            tree = ast.parse(content)
        except SyntaxError:
            return []

        tasks: list[TaskInfo] = []

        for node in ast.walk(tree):
            if not isinstance(node, ast.FunctionDef):
                continue

            # Check if function has @task decorator
            is_task = any(
                (isinstance(d, ast.Name) and d.id == "task")
                or (
                    isinstance(d, ast.Call) and isinstance(d.func, ast.Name) and d.func.id == "task"
                )
                for d in node.decorator_list
            )

            if not is_task:
                continue

            # Extract docstring as description
            description = ast.get_docstring(node)

            # Convert underscores to hyphens (invoke convention)
            task_name = node.name.replace("_", "-")

            tasks.append(TaskInfo(name=task_name, description=description))

        return sorted(tasks, key=lambda t: t.name)

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

get_run_command

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

Get the invoke command for a specific task.

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

get_tasks async

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

Parse tasks.py and return available tasks.

Source code in src/anyenv/task_runners/_python.py
async def get_tasks(self, fs: AsyncFileSystem, cwd: str) -> list[TaskInfo]:
    """Parse tasks.py and return available tasks."""
    # Only parse tasks.py, not the config files
    tasks_file = posixpath.join(cwd, "tasks.py")
    try:
        if not await fs._isfile(tasks_file):  # noqa: SLF001
            return []
    except Exception:  # noqa: BLE001
        return []

    content = await self._read_file(fs, tasks_file)
    return self._parse_tasks(content)

:fa-brands-github: Show source on GitHub