@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]