Skip to content

Commit

Permalink
fix(poly check): verbose output without duplication (#292)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DavidVujic authored Nov 17, 2024
1 parent d39f346 commit eb7cf13
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 30 deletions.
4 changes: 2 additions & 2 deletions bases/polylith/cli/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ def check_command(
filtered_projects = filtered_projects_data(only_projects_data, directory)
enriched_projects = enriched_with_lock_files_data(root, filtered_projects, verbose)

results = {commands.check.run(root, ns, p, cli_options) for p in enriched_projects}
result = commands.check.run(root, ns, enriched_projects, cli_options)
libs_result = commands.check.check_libs_versions(
filtered_projects, all_projects_data, cli_options
)

if not all(results) or not libs_result:
if not result or not libs_result:
raise Exit(code=1)


Expand Down
32 changes: 17 additions & 15 deletions components/polylith/check/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,33 @@
from rich.console import Console


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

bases = brick_imports["bases"]
components = brick_imports["components"]
items = sorted(bricks.items())
tag = "base" if brick_type == "bases" else "comp"

bricks = {**bases, **components}
for item in items:
key, values = item

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

if not imports_in_brick:
continue

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


def print_brick_imports(brick_imports: dict) -> None:
bases = brick_imports["bases"]
components = brick_imports["components"]

_print_imports(bases, "bases")
_print_imports(components, "components")


def print_missing_deps(diff: Set[str], project_name: str) -> None:
if not diff:
return
Expand All @@ -37,12 +45,6 @@ def print_missing_deps(diff: Set[str], project_name: str) -> None:
console.print(f":thinking_face: Cannot locate {missing} in {project_name}")


def fetch_brick_imports(root: Path, ns: str, all_imports: dict) -> dict:
extracted = grouping.extract_brick_imports(all_imports, ns)

return collect.with_unknown_components(root, ns, extracted)


def collect_all_imports(root: Path, ns: str, project_data: dict) -> dict:
bases = {b for b in project_data.get("bases", [])}
components = {c for c in project_data.get("components", [])}
Expand All @@ -54,8 +56,8 @@ def collect_all_imports(root: Path, ns: str, project_data: dict) -> dict:
all_imports_in_components = imports.fetch_all_imports(components_paths)

brick_imports = {
"bases": fetch_brick_imports(root, ns, all_imports_in_bases),
"components": fetch_brick_imports(root, ns, all_imports_in_components),
"bases": grouping.extract_brick_imports(all_imports_in_bases, ns),
"components": grouping.extract_brick_imports(all_imports_in_components, ns),
}

third_party_imports = {
Expand Down
40 changes: 33 additions & 7 deletions components/polylith/commands/check.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from functools import reduce
from pathlib import Path
from typing import List
from typing import List, Tuple

from polylith import check, distributions, libs

Expand Down Expand Up @@ -72,8 +73,9 @@ def check_libs_versions(
return False if libraries else True


def run(root: Path, ns: str, project_data: dict, options: dict) -> bool:
is_verbose = options["verbose"]
def run_each(
root: Path, ns: str, project_data: dict, options: dict
) -> Tuple[bool, dict]:
is_quiet = options["quiet"]
is_strict = options["strict"]

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

if is_verbose:
check.report.print_brick_imports(details["brick_imports"])
check.report.print_brick_imports(details["third_party_imports"])
return res, details

return res

def _merge(data: List[dict], key: str) -> dict:
return reduce(lambda acc, d: {**acc, **d[key]}, data, {})


def _print_brick_imports(all_imports: List[dict]) -> None:
merged_bases = _merge(all_imports, "bases")
merged_components = _merge(all_imports, "components")

merged = {"bases": merged_bases, "components": merged_components}

check.report.print_brick_imports(merged)


def run(root: Path, ns: str, projects_data: List[dict], options: dict) -> bool:
is_verbose = options["verbose"] and not options["quiet"]
res = [run_each(root, ns, p, options) for p in projects_data]
results = [r[0] for r in res]

if is_verbose:
brick_imports = [r[1]["brick_imports"] for r in res]
third_party_imports = [r[1]["third_party_imports"] for r in res]

_print_brick_imports(brick_imports)
_print_brick_imports(third_party_imports)

return all(results)
6 changes: 2 additions & 4 deletions components/polylith/poetry/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@ def handle(self) -> int:
self.merged_project_data(data) for data in projects_data
]

results = {
commands.check.run(root, ns, data, options) for data in merged_projects_data
}
result = commands.check.run(root, ns, merged_projects_data, options)

libs_result = commands.check.check_libs_versions(
projects_data, all_projects_data, options
)

return 0 if all(results) and libs_result else 1
return 0 if result and libs_result else 1
2 changes: 1 addition & 1 deletion projects/poetry_polylith_plugin/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "poetry-polylith-plugin"
version = "1.32.0"
version = "1.33.0"
description = "A Poetry plugin that adds tooling support for the Polylith Architecture"
authors = ["David Vujic"]
homepage = "https://davidvujic.github.io/python-polylith-docs/"
Expand Down
2 changes: 1 addition & 1 deletion projects/polylith_cli/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "polylith-cli"
version = "1.21.0"
version = "1.22.0"
description = "Python tooling support for the Polylith Architecture"
authors = ['David Vujic']
homepage = "https://davidvujic.github.io/python-polylith-docs/"
Expand Down

0 comments on commit eb7cf13

Please sign in to comment.