Skip to content

Commit

Permalink
churn: Make tools be plugins (#28)
Browse files Browse the repository at this point in the history
- split tools into submodules
- use pluggy to register them as plugins
  • Loading branch information
ssbarnea authored Apr 6, 2021
1 parent 755b8ed commit b5385a7
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 67 deletions.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ gitpython==3.1.14
# via mk (setup.cfg)
importlib-metadata==3.10.0
# via mk (setup.cfg)
pluggy==0.13.1
# via mk (setup.cfg)
pygments==2.8.1
# via
# mk (setup.cfg)
Expand Down
6 changes: 6 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ install_requires =
click-help-colors
colorama # for Windows
importlib-metadata
pluggy
pygments
pyyaml
rich >= 9.0
Expand All @@ -55,6 +56,11 @@ where = src
[options.entry_points]
console_scripts =
mk=mk.__main__:cli
mk_tools =
tox=mk.tools.tox:ToxTool
npm=mk.tools.npm:NpmTool
pre-commit=mk.tools.pre_commit:PreCommitTool
make=mk.tools.make:MakeTool

[flake8]
max-complexity = 22
Expand Down
9 changes: 7 additions & 2 deletions src/mk/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from pathlib import Path
from typing import List

import pluggy
from git import Repo
from git.exc import GitError

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


class Runner:
Expand All @@ -21,7 +22,11 @@ def __init__(self) -> None:
self.root = Path(self.repo.working_dir)
self.actions: List[Action] = []

for c in Tool:
self.pm = pluggy.PluginManager("mk_tools")
self.pm.load_setuptools_entrypoints("mk_tools")
for _, cls_name in self.pm.list_name_plugin():
# for c in Tool:
c = cls_name()
if c.is_present(self.root):
logging.info("Detected %s !", c)
self.actions.extend(c.actions())
Expand Down
64 changes: 0 additions & 64 deletions src/mk/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import os
import subprocess
from configparser import ConfigParser
from typing import Any, List, Optional


Expand Down Expand Up @@ -69,64 +66,3 @@ def __repr__(self):

def __rich__(self):
return f"[magenta]{self.name}[/]"


class PreCommitTool(Tool):
name = "pre-commit"

def run(self, action: Optional[str] = None):
subprocess.run(["pre-commit", "run", "-a"], check=True)

def is_present(self, path: str) -> bool:
if os.path.isfile(os.path.join(path, ".pre-commit-config.yaml")):
return True
return False

def actions(self) -> List[Action]:
return [Action(name="lint", tool=self)]


class ToxTool(Tool):
name = "tox"

def is_present(self, path: str) -> bool:
if os.path.isfile(os.path.join(path, "tox.ini")):
return True
return False

def actions(self) -> List[Action]:
# -a is not supported by tox4!
actions: List[Action] = []
cp = ConfigParser()
tox_cfg = subprocess.check_output(["tox", "--showconfig"], universal_newlines=True)
cp.read_string(tox_cfg)
for section in cp.sections():
if section.startswith("testenv:"):
_, env_name = section.split(":")
# we ignore hidden envs like implicit .pkg:
if not env_name.startswith("."):
actions.append(
Action(
name=env_name,
tool=self,
description=cp[section]["description"],
args=[env_name],
)
)

return actions

def run(self, action: Optional[str] = None) -> None:
if not action:
cmd = ["tox"]
else:
cmd = ["tox", "-e", action]
subprocess.run(cmd, check=True)


class MakeTool(Tool):
name = "make"


class NpmTool(Tool):
name = "npm"
5 changes: 5 additions & 0 deletions src/mk/tools/make.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from mk.tools import Tool


class MakeTool(Tool):
name = "make"
5 changes: 5 additions & 0 deletions src/mk/tools/npm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from mk.tools import Tool


class NpmTool(Tool):
name = "npm"
20 changes: 20 additions & 0 deletions src/mk/tools/pre_commit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import subprocess
from typing import List, Optional

from mk.tools import Action, Tool


class PreCommitTool(Tool):
name = "pre-commit"

def run(self, action: Optional[str] = None):
subprocess.run(["pre-commit", "run", "-a"], check=True)

def is_present(self, path: str) -> bool:
if os.path.isfile(os.path.join(path, ".pre-commit-config.yaml")):
return True
return False

def actions(self) -> List[Action]:
return [Action(name="lint", tool=self)]
44 changes: 44 additions & 0 deletions src/mk/tools/tox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
import subprocess
from configparser import ConfigParser
from typing import List, Optional

from mk.tools import Action, Tool


class ToxTool(Tool):
name = "tox"

def is_present(self, path: str) -> bool:
if os.path.isfile(os.path.join(path, "tox.ini")):
return True
return False

def actions(self) -> List[Action]:
# -a is not supported by tox4!
actions: List[Action] = []
cp = ConfigParser()
tox_cfg = subprocess.check_output(["tox", "--showconfig"], universal_newlines=True)
cp.read_string(tox_cfg)
for section in cp.sections():
if section.startswith("testenv:"):
_, env_name = section.split(":")
# we ignore hidden envs like implicit .pkg:
if not env_name.startswith("."):
actions.append(
Action(
name=env_name,
tool=self,
description=cp[section]["description"],
args=[env_name],
)
)

return actions

def run(self, action: Optional[str] = None) -> None:
if not action:
cmd = ["tox"]
else:
cmd = ["tox", "-e", action]
subprocess.run(cmd, check=True)
4 changes: 3 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ more-itertools==8.7.0
packaging==20.9
# via pytest
pluggy==0.13.1
# via pytest
# via
# mk (setup.cfg)
# pytest
py==1.10.0
# via pytest
pygments==2.8.1
Expand Down

0 comments on commit b5385a7

Please sign in to comment.