|
| 1 | +from pathlib import Path |
| 2 | +from typing import List, Set |
| 3 | + |
| 4 | +from polylith import configuration, info, repo, sync |
| 5 | +from polylith.reporting import theme |
| 6 | +from rich.console import Console |
| 7 | +from rich.padding import Padding |
| 8 | +from rich.prompt import Confirm, Prompt |
| 9 | + |
| 10 | +console = Console(theme=theme.poly_theme) |
| 11 | + |
| 12 | + |
| 13 | +def create_added_brick_message(bricks: Set[str], tag: str, project_name: str) -> str: |
| 14 | + number_of_bricks = len(bricks) |
| 15 | + plural = "s" if number_of_bricks > 1 else "" |
| 16 | + |
| 17 | + if tag == "base": |
| 18 | + grammar = f"base{plural}" |
| 19 | + else: |
| 20 | + grammar = f"component{plural}" |
| 21 | + |
| 22 | + return f"[data]Added {number_of_bricks} [{tag}]{grammar}[/] to the [proj]{project_name}[/] project.[/]" |
| 23 | + |
| 24 | + |
| 25 | +def confirmation(diff: dict, project_name: str) -> None: |
| 26 | + pad = (1, 0, 0, 0) |
| 27 | + |
| 28 | + if not diff: |
| 29 | + nothing_added_message = f"[data]No bricks added to [proj]{project_name}[/][/]." |
| 30 | + console.print(Padding(nothing_added_message, pad)) |
| 31 | + |
| 32 | + return |
| 33 | + |
| 34 | + bases = diff["bases"] |
| 35 | + components = diff["components"] |
| 36 | + |
| 37 | + bases_message = create_added_brick_message(bases, "base", project_name) |
| 38 | + console.print(Padding(bases_message, pad)) |
| 39 | + |
| 40 | + if len(components) == 0: |
| 41 | + return |
| 42 | + |
| 43 | + components_message = create_added_brick_message(components, "comp", project_name) |
| 44 | + console.print(components_message) |
| 45 | + |
| 46 | + |
| 47 | +def add_bricks_to_project( |
| 48 | + root: Path, |
| 49 | + ns: str, |
| 50 | + project_name: str, |
| 51 | + possible_bases: List[str], |
| 52 | +) -> None: |
| 53 | + projects_data = info.get_projects_data(root, ns) |
| 54 | + project_data = next((p for p in projects_data if p["name"] == project_name), None) |
| 55 | + |
| 56 | + if not project_data: |
| 57 | + return |
| 58 | + |
| 59 | + message = f"[data]Project [proj]{project_name}[/] created.[/]" |
| 60 | + console.print(Padding(message, (0, 0, 1, 0))) |
| 61 | + |
| 62 | + first, *_ = possible_bases |
| 63 | + |
| 64 | + if not Confirm.ask( |
| 65 | + prompt=f"[data]Do you want to add bricks to the [proj]{project_name}[/] project?[/]", |
| 66 | + console=console, |
| 67 | + ): |
| 68 | + return |
| 69 | + |
| 70 | + question = "[data]What's the name of the Polylith [base]base[/] to add?[/]" |
| 71 | + |
| 72 | + base = Prompt.ask( |
| 73 | + prompt=question, |
| 74 | + console=console, |
| 75 | + default=first, |
| 76 | + show_default=True, |
| 77 | + case_sensitive=False, |
| 78 | + ) |
| 79 | + |
| 80 | + all_bases = info.get_bases(root, ns) |
| 81 | + found_base = next((b for b in all_bases if str.lower(b) == str.lower(base)), None) |
| 82 | + |
| 83 | + if not found_base: |
| 84 | + confirmation({}, project_name) |
| 85 | + return |
| 86 | + |
| 87 | + diff = sync.calculate_needed_bricks(root, ns, project_data, found_base) |
| 88 | + |
| 89 | + sync.update_project(root, ns, diff) |
| 90 | + |
| 91 | + confirmation(diff, project_name) |
| 92 | + |
| 93 | + |
| 94 | +def run(project_name: str) -> None: |
| 95 | + root = repo.get_workspace_root(Path.cwd()) |
| 96 | + ns = configuration.get_namespace_from_config(root) |
| 97 | + |
| 98 | + possible_bases = sorted(info.find_unused_bases(root, ns)) |
| 99 | + |
| 100 | + if not possible_bases: |
| 101 | + return |
| 102 | + |
| 103 | + add_bricks_to_project(root, ns, project_name, possible_bases) |
0 commit comments