Skip to content

Commit eb7cf13

Browse files
authored
fix(poly check): verbose output without duplication (#292)
* fix(poly check): verbose output without duplication * fix(poly check): verbose output with brick type color * bump CLI to 1.22.0 * bump Poetry plugin to 1.33.0
1 parent d39f346 commit eb7cf13

File tree

6 files changed

+56
-30
lines changed

6 files changed

+56
-30
lines changed

bases/polylith/cli/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ def check_command(
7474
filtered_projects = filtered_projects_data(only_projects_data, directory)
7575
enriched_projects = enriched_with_lock_files_data(root, filtered_projects, verbose)
7676

77-
results = {commands.check.run(root, ns, p, cli_options) for p in enriched_projects}
77+
result = commands.check.run(root, ns, enriched_projects, cli_options)
7878
libs_result = commands.check.check_libs_versions(
7979
filtered_projects, all_projects_data, cli_options
8080
)
8181

82-
if not all(results) or not libs_result:
82+
if not result or not libs_result:
8383
raise Exit(code=1)
8484

8585

components/polylith/check/report.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,33 @@
77
from rich.console import Console
88

99

10-
def print_brick_imports(brick_imports: dict) -> None:
10+
def _print_imports(bricks: dict, brick_type: str) -> None:
1111
console = Console(theme=theme.poly_theme)
1212

13-
bases = brick_imports["bases"]
14-
components = brick_imports["components"]
13+
items = sorted(bricks.items())
14+
tag = "base" if brick_type == "bases" else "comp"
1515

16-
bricks = {**bases, **components}
16+
for item in items:
17+
key, values = item
1718

18-
for key, values in bricks.items():
1919
imports_in_brick = values.difference({key})
2020

2121
if not imports_in_brick:
2222
continue
2323

24-
joined = ", ".join(imports_in_brick)
25-
message = f":information: [data]{key}[/] is importing [data]{joined}[/]"
24+
joined = ", ".join(sorted(imports_in_brick))
25+
message = f":information: [{tag}]{key}[/] is importing [data]{joined}[/]"
2626
console.print(message)
2727

2828

29+
def print_brick_imports(brick_imports: dict) -> None:
30+
bases = brick_imports["bases"]
31+
components = brick_imports["components"]
32+
33+
_print_imports(bases, "bases")
34+
_print_imports(components, "components")
35+
36+
2937
def print_missing_deps(diff: Set[str], project_name: str) -> None:
3038
if not diff:
3139
return
@@ -37,12 +45,6 @@ def print_missing_deps(diff: Set[str], project_name: str) -> None:
3745
console.print(f":thinking_face: Cannot locate {missing} in {project_name}")
3846

3947

40-
def fetch_brick_imports(root: Path, ns: str, all_imports: dict) -> dict:
41-
extracted = grouping.extract_brick_imports(all_imports, ns)
42-
43-
return collect.with_unknown_components(root, ns, extracted)
44-
45-
4648
def collect_all_imports(root: Path, ns: str, project_data: dict) -> dict:
4749
bases = {b for b in project_data.get("bases", [])}
4850
components = {c for c in project_data.get("components", [])}
@@ -54,8 +56,8 @@ def collect_all_imports(root: Path, ns: str, project_data: dict) -> dict:
5456
all_imports_in_components = imports.fetch_all_imports(components_paths)
5557

5658
brick_imports = {
57-
"bases": fetch_brick_imports(root, ns, all_imports_in_bases),
58-
"components": fetch_brick_imports(root, ns, all_imports_in_components),
59+
"bases": grouping.extract_brick_imports(all_imports_in_bases, ns),
60+
"components": grouping.extract_brick_imports(all_imports_in_components, ns),
5961
}
6062

6163
third_party_imports = {

components/polylith/commands/check.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from functools import reduce
12
from pathlib import Path
2-
from typing import List
3+
from typing import List, Tuple
34

45
from polylith import check, distributions, libs
56

@@ -72,8 +73,9 @@ def check_libs_versions(
7273
return False if libraries else True
7374

7475

75-
def run(root: Path, ns: str, project_data: dict, options: dict) -> bool:
76-
is_verbose = options["verbose"]
76+
def run_each(
77+
root: Path, ns: str, project_data: dict, options: dict
78+
) -> Tuple[bool, dict]:
7779
is_quiet = options["quiet"]
7880
is_strict = options["strict"]
7981

@@ -104,8 +106,32 @@ def run(root: Path, ns: str, project_data: dict, options: dict) -> bool:
104106
check.report.print_missing_deps(details["brick_diff"], name)
105107
check.report.print_missing_deps(details["libs_diff"], name)
106108

107-
if is_verbose:
108-
check.report.print_brick_imports(details["brick_imports"])
109-
check.report.print_brick_imports(details["third_party_imports"])
109+
return res, details
110110

111-
return res
111+
112+
def _merge(data: List[dict], key: str) -> dict:
113+
return reduce(lambda acc, d: {**acc, **d[key]}, data, {})
114+
115+
116+
def _print_brick_imports(all_imports: List[dict]) -> None:
117+
merged_bases = _merge(all_imports, "bases")
118+
merged_components = _merge(all_imports, "components")
119+
120+
merged = {"bases": merged_bases, "components": merged_components}
121+
122+
check.report.print_brick_imports(merged)
123+
124+
125+
def run(root: Path, ns: str, projects_data: List[dict], options: dict) -> bool:
126+
is_verbose = options["verbose"] and not options["quiet"]
127+
res = [run_each(root, ns, p, options) for p in projects_data]
128+
results = [r[0] for r in res]
129+
130+
if is_verbose:
131+
brick_imports = [r[1]["brick_imports"] for r in res]
132+
third_party_imports = [r[1]["third_party_imports"] for r in res]
133+
134+
_print_brick_imports(brick_imports)
135+
_print_brick_imports(third_party_imports)
136+
137+
return all(results)

components/polylith/poetry/commands/check.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,10 @@ def handle(self) -> int:
6363
self.merged_project_data(data) for data in projects_data
6464
]
6565

66-
results = {
67-
commands.check.run(root, ns, data, options) for data in merged_projects_data
68-
}
66+
result = commands.check.run(root, ns, merged_projects_data, options)
6967

7068
libs_result = commands.check.check_libs_versions(
7169
projects_data, all_projects_data, options
7270
)
7371

74-
return 0 if all(results) and libs_result else 1
72+
return 0 if result and libs_result else 1

projects/poetry_polylith_plugin/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "poetry-polylith-plugin"
3-
version = "1.32.0"
3+
version = "1.33.0"
44
description = "A Poetry plugin that adds tooling support for the Polylith Architecture"
55
authors = ["David Vujic"]
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"

projects/polylith_cli/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "polylith-cli"
3-
version = "1.21.0"
3+
version = "1.22.0"
44
description = "Python tooling support for the Polylith Architecture"
55
authors = ['David Vujic']
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"

0 commit comments

Comments
 (0)