Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Just task runner #175

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 7 additions & 0 deletions docs/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ the command will not be exposed, as we assume that this is an internal target.
A good example of a project using this pattern is
[podman](https://github.com/containers/podman).

## Justfile

If a [justfile](https://github.com/casey/just/tree/master/examples) is found on
hyperupcall marked this conversation as resolved.
Show resolved Hide resolved
the repository root, the tool will expose all its targets that have a trailing
comment with a double hash - `## <description>`. If the comment is not found,
hyperupcall marked this conversation as resolved.
Show resolved Hide resolved
the command will not be exposed, as we assume that this is an internal target.

## npm

If a [package.json](https://docs.npmjs.com/cli/v7/configuring-npm/package-json)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ mk = "mk.__main__:cli"
ansible = "mk.tools.ansible:AnsibleTool"
cmake = "mk.tools.cmake:CMakeTool"
git = "mk.tools.git:GitTool"
just = "mk.tools.just:JustTool"
make = "mk.tools.make:MakeTool"
node = "mk.tools.node:NodeTool"
pypackage = "mk.tools.py_package:PyPackageTool"
Expand Down
50 changes: 50 additions & 0 deletions src/mk/tools/just.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from __future__ import annotations

import os
import re
from pathlib import Path

from mk.exec import run_or_fail
from mk.tools import Action, Tool


class JustTool(Tool):
name = "just"

def __init__(self) -> None:
super().__init__(self)
self.justfile: str | None = None

def run(self, action: Action | None = None) -> None:
cmd = ["just"]
if action:
cmd.append(action.name)
run_or_fail(cmd, tee=True)

def is_present(self, path: Path) -> bool:
for name in ["Justfile", "justfile", "JUSTFILE", ".justfile"]:
justfile = os.path.join(path, name)
hyperupcall marked this conversation as resolved.
Show resolved Hide resolved
if os.path.isfile(justfile):
hyperupcall marked this conversation as resolved.
Show resolved Hide resolved
self.justfile = justfile
return True
return False

def actions(self) -> list[Action]:
actions = []
if not self.justfile:
msg = "Justfile not found"
raise RuntimeError(msg)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ssbarnea, doesn't the CLI code have a better way to handle errors than raising a generic Exception?

# pylint: disable=duplicate-code
with open(self.justfile, encoding="utf-8") as file:
for line in file.readlines():
hyperupcall marked this conversation as resolved.
Show resolved Hide resolved
# Current implementation assumes that descriptions are added
# using double ## after the target name.
# Inspired by https://github.com/containers/podman/blob/master/Makefile#L127
match = re.match(r"^([a-zA-Z_-]+):.*?## (.*)$$", line)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this regex be made a top level constant and re.compile()ed?

if match:
target, description = match.groups()
actions.append(
Action(name=target, tool=self, description=description),
)
# pylint: enable=duplicate-code
return actions
Loading