Skip to content

Commit fec66cb

Browse files
authored
feat: a new command - poly check (#43)
* dev: project-specific mypy config * feat: a poly check command * update lock-files * feat(poly-check): bump version to 1.1.0 * docs(poly-check): add docs about the check command
1 parent ffcd2ec commit fec66cb

File tree

14 files changed

+1787
-1336
lines changed

14 files changed

+1787
-1336
lines changed

bases/polylith/poetry_plugin/plugin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from poetry.console.application import Application
22
from poetry.plugins.application_plugin import ApplicationPlugin
33
from polylith.poetry.commands import (
4+
CheckCommand,
45
CreateBaseCommand,
56
CreateComponentCommand,
67
CreateProjectCommand,
@@ -10,6 +11,7 @@
1011
)
1112

1213
commands = [
14+
CheckCommand,
1315
CreateBaseCommand,
1416
CreateComponentCommand,
1517
CreateProjectCommand,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from polylith.check import report
2+
3+
__all__ = ["report"]

components/polylith/check/core.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
import subprocess
3+
from pathlib import Path
4+
from typing import List
5+
6+
7+
def navigate_to(path: Path):
8+
os.chdir(str(path))
9+
10+
11+
def run_command(project_path: Path) -> List[str]:
12+
current_dir = Path.cwd()
13+
14+
navigate_to(project_path)
15+
16+
try:
17+
res = subprocess.run(
18+
["poetry", "check-project"], capture_output=True, text=True
19+
)
20+
finally:
21+
navigate_to(current_dir)
22+
23+
res.check_returncode()
24+
25+
return res.stdout.splitlines()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from polylith.check.core import run_command
2+
from rich.console import Console
3+
from rich.theme import Theme
4+
5+
info_theme = Theme(
6+
{
7+
"data": "#999966",
8+
"proj": "#8A2BE2",
9+
"comp": "#32CD32",
10+
"base": "#6495ED",
11+
}
12+
)
13+
14+
15+
def run(project_data: dict) -> bool:
16+
console = Console(theme=info_theme)
17+
18+
project_name = project_data["name"]
19+
project_path = project_data["path"]
20+
21+
with console.status(f"checking [proj]{project_name}[/]", spinner="monkey"):
22+
result = run_command(project_path)
23+
24+
message = ["[proj]", project_name, "[/]", " "]
25+
extra = [":warning:"] if result else [":heavy_check_mark:"]
26+
27+
output = "".join(message + extra)
28+
console.print(output)
29+
30+
for row in result:
31+
console.print(f"[data]{row}[/]")
32+
33+
return True if not result else False

components/polylith/poetry/commands/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from polylith.poetry.commands.check import CheckCommand
12
from polylith.poetry.commands.create_base import CreateBaseCommand
23
from polylith.poetry.commands.create_component import CreateComponentCommand
34
from polylith.poetry.commands.create_project import CreateProjectCommand
@@ -6,6 +7,7 @@
67
from polylith.poetry.commands.info import InfoCommand
78

89
__all__ = [
10+
"CheckCommand",
911
"CreateBaseCommand",
1012
"CreateComponentCommand",
1113
"CreateProjectCommand",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from pathlib import Path
2+
3+
from poetry.console.commands.command import Command
4+
from polylith import check, project, repo
5+
6+
7+
class CheckCommand(Command):
8+
name = "poly check"
9+
description = "Validates the <comment>Polylith</> workspace."
10+
11+
def handle(self) -> int:
12+
root = repo.find_workspace_root(Path.cwd())
13+
if not root:
14+
raise ValueError(
15+
"Didn't find the workspace root. Expected to find a workspace.toml file."
16+
)
17+
18+
projects = project.get_project_names_and_paths(root)
19+
20+
res = [check.report.run(proj) for proj in projects]
21+
22+
return 0 if all(res) else 1
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
from polylith.project.create import create_project
2-
from polylith.project.get import get_packages_for_projects, get_project_names
2+
from polylith.project.get import (
3+
get_packages_for_projects,
4+
get_project_names,
5+
get_project_names_and_paths,
6+
)
37
from polylith.project.parser import parse_package_paths
48

59
__all__ = [
610
"create_project",
711
"get_project_names",
12+
"get_project_names_and_paths",
813
"get_packages_for_projects",
914
"parse_package_paths",
1015
]

components/polylith/project/get.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ def get_packages_for_projects(root: Path) -> List[dict]:
4242
{"name": get_project_name(d), "packages": get_project_package_includes(d)}
4343
for d in tomls
4444
]
45+
46+
47+
def get_project_names_and_paths(root: Path) -> List[dict]:
48+
project_files = get_project_files(root)
49+
50+
return [
51+
{"name": get_project_name(get_toml(p)), "path": p.parent} for p in project_files
52+
]

poetry.lock

Lines changed: 775 additions & 668 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/poetry_polylith_plugin/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Show info about the workspace:
7272
poetry poly info
7373
```
7474

75+
#### Diff
7576
Shows what has changed since the most recent stable point in time:
7677

7778
``` shell
@@ -91,6 +92,16 @@ Useful for CI:
9192
poetry poly diff --short
9293
```
9394

95+
#### Check
96+
Validates the Polylith workspace:
97+
98+
``` shell
99+
poetry poly check
100+
```
101+
102+
**NOTE**: this feature is built on top of the `poetry check-project` command from the [Multiproject](https://github.com/DavidVujic/poetry-multiproject-plugin) plugin.
103+
Make sure that you have the latest version of poetry-multiproject-plugin installed to be able to use the `poly check` command.
104+
94105
#### Testing
95106
The `create` commands will also create corresponding unit tests. It is possible to disable thi behaviour
96107
by setting `enabled = false` in the `workspace.toml` file.

0 commit comments

Comments
 (0)