From eb7cf13840d8fa0b0be7d40cfe0361191ccc1207 Mon Sep 17 00:00:00 2001 From: David Vujic Date: Sun, 17 Nov 2024 15:23:56 +0100 Subject: [PATCH] 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 --- bases/polylith/cli/core.py | 4 +- components/polylith/check/report.py | 32 ++++++++------- components/polylith/commands/check.py | 40 +++++++++++++++---- components/polylith/poetry/commands/check.py | 6 +-- .../poetry_polylith_plugin/pyproject.toml | 2 +- projects/polylith_cli/pyproject.toml | 2 +- 6 files changed, 56 insertions(+), 30 deletions(-) diff --git a/bases/polylith/cli/core.py b/bases/polylith/cli/core.py index 8e298a12..9e8f0b4e 100644 --- a/bases/polylith/cli/core.py +++ b/bases/polylith/cli/core.py @@ -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) diff --git a/components/polylith/check/report.py b/components/polylith/check/report.py index fb4108db..39506c8e 100644 --- a/components/polylith/check/report.py +++ b/components/polylith/check/report.py @@ -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 @@ -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", [])} @@ -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 = { diff --git a/components/polylith/commands/check.py b/components/polylith/commands/check.py index 4a1a7be0..f06657fd 100644 --- a/components/polylith/commands/check.py +++ b/components/polylith/commands/check.py @@ -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 @@ -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"] @@ -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) diff --git a/components/polylith/poetry/commands/check.py b/components/polylith/poetry/commands/check.py index d534ec5b..b033832b 100644 --- a/components/polylith/poetry/commands/check.py +++ b/components/polylith/poetry/commands/check.py @@ -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 diff --git a/projects/poetry_polylith_plugin/pyproject.toml b/projects/poetry_polylith_plugin/pyproject.toml index d9f1d193..c43100e3 100644 --- a/projects/poetry_polylith_plugin/pyproject.toml +++ b/projects/poetry_polylith_plugin/pyproject.toml @@ -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/" diff --git a/projects/polylith_cli/pyproject.toml b/projects/polylith_cli/pyproject.toml index db163d82..bfdf9670 100644 --- a/projects/polylith_cli/pyproject.toml +++ b/projects/polylith_cli/pyproject.toml @@ -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/"