|
| 1 | +from pathlib import Path |
| 2 | +from typing import List, Set, Tuple, Union |
| 3 | + |
| 4 | +from polylith import bricks, configuration, diff, info, test |
| 5 | + |
| 6 | + |
| 7 | +def get_imported_bricks_in_tests( |
| 8 | + root: Path, ns: str, tag_name: Union[str, None], theme: str |
| 9 | +) -> Set[str]: |
| 10 | + files = test.get_changed_files(root, tag_name) |
| 11 | + imports = test.get_brick_imports_in_tests(root, ns, theme, files) |
| 12 | + |
| 13 | + return set().union(*imports.values()) |
| 14 | + |
| 15 | + |
| 16 | +def extract_brick_names(bricks_data: List[dict], imported_bricks: Set[str]) -> Set[str]: |
| 17 | + return {v for b in bricks_data for v in b.values() if v in imported_bricks} |
| 18 | + |
| 19 | + |
| 20 | +def get_affected_bricks( |
| 21 | + root: Path, ns: str, tag_name: Union[str, None], theme: str |
| 22 | +) -> Tuple[Set[str], Set[str]]: |
| 23 | + found = get_imported_bricks_in_tests(root, ns, tag_name, theme) |
| 24 | + |
| 25 | + bases = extract_brick_names(bricks.get_bases_data(root, ns), found) |
| 26 | + components = extract_brick_names(bricks.get_components_data(root, ns), found) |
| 27 | + |
| 28 | + return bases, components |
| 29 | + |
| 30 | + |
| 31 | +def get_affected_projects( |
| 32 | + root: Path, ns: str, bases: Set[str], components: Set[str] |
| 33 | +) -> List[dict]: |
| 34 | + projects_data = [p for p in info.get_projects_data(root, ns) if info.is_project(p)] |
| 35 | + |
| 36 | + names = diff.collect.get_projects_affected_by_changes( |
| 37 | + projects_data, [], list(bases), list(components) |
| 38 | + ) |
| 39 | + |
| 40 | + return [p for p in projects_data if p["path"].name in names] |
| 41 | + |
| 42 | + |
| 43 | +def run(root: Path, ns: str, tag: str, options: dict) -> None: |
| 44 | + theme = configuration.get_theme_from_config(root) |
| 45 | + |
| 46 | + bases, components = get_affected_bricks(root, ns, tag, theme) |
| 47 | + projects_data = get_affected_projects(root, ns, bases, components) |
| 48 | + |
| 49 | + if options.get("bricks"): |
| 50 | + test.report.print_detected_changes_affecting_bricks(bases, components, options) |
| 51 | + return |
| 52 | + |
| 53 | + if options.get("projects"): |
| 54 | + test.report.print_projects_affected_by_changes(projects_data, options) |
| 55 | + return |
| 56 | + |
| 57 | + test.report.print_report_summary(projects_data, bases, components, tag) |
| 58 | + test.report.print_test_report(projects_data, bases, components, options) |
0 commit comments