|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import tomlkit |
| 4 | +from polylith import building, repo, toml |
| 5 | +from polylith.cli import options |
| 6 | +from typer import Exit, Typer |
| 7 | +from typing_extensions import Annotated |
| 8 | + |
| 9 | +app = Typer() |
| 10 | + |
| 11 | + |
| 12 | +def get_work_dir(root: Path, directory: str) -> Path: |
| 13 | + work_dir = building.get_work_dir({}) |
| 14 | + work_path = Path(directory) / work_dir if directory else work_dir |
| 15 | + |
| 16 | + return root / work_path |
| 17 | + |
| 18 | + |
| 19 | +def get_build_dir(root: Path, directory: str) -> Path: |
| 20 | + return root / Path(directory) if directory else root |
| 21 | + |
| 22 | + |
| 23 | +def get_project_data(build_dir: Path) -> tomlkit.TOMLDocument: |
| 24 | + fullpath = build_dir / repo.default_toml |
| 25 | + |
| 26 | + if not fullpath.exists(): |
| 27 | + raise Exit(code=1) |
| 28 | + |
| 29 | + return toml.read_toml_document(fullpath) |
| 30 | + |
| 31 | + |
| 32 | +@app.command("setup") |
| 33 | +def setup_command(directory: Annotated[str, options.directory] = ""): |
| 34 | + """Prepare a project before building a wheel or a source distribution (sdist). |
| 35 | + Run it before the build command of your Package & Dependency Management tool. |
| 36 | +
|
| 37 | + """ |
| 38 | + root = Path.cwd() |
| 39 | + build_dir = get_build_dir(root, directory) |
| 40 | + print(f"Build directory: {build_dir}") |
| 41 | + |
| 42 | + data = get_project_data(build_dir) |
| 43 | + bricks = toml.get_project_packages_from_polylith_section(data) |
| 44 | + |
| 45 | + if not bricks: |
| 46 | + print("No bricks found.") |
| 47 | + return |
| 48 | + |
| 49 | + bricks_with_paths = {build_dir / k: v for k, v in bricks.items()} |
| 50 | + custom_top_ns = toml.get_custom_top_namespace_from_polylith_section(data) |
| 51 | + |
| 52 | + if not custom_top_ns: |
| 53 | + building.copy_bricks_as_is(bricks_with_paths, build_dir) |
| 54 | + else: |
| 55 | + work_dir = get_work_dir(root, directory) |
| 56 | + print(f"Using temporary working directory: {work_dir}") |
| 57 | + |
| 58 | + rewritten = building.copy_and_rewrite_bricks( |
| 59 | + bricks_with_paths, custom_top_ns, work_dir, build_dir |
| 60 | + ) |
| 61 | + |
| 62 | + for item in rewritten: |
| 63 | + print(f"Updated {item} with new top namespace for local imports.") |
| 64 | + |
| 65 | + |
| 66 | +@app.command("teardown") |
| 67 | +def teardown_command(directory: Annotated[str, options.directory] = ""): |
| 68 | + """Clean up temporary directories. Run it after the build command of your Package & Dependency Management tool.""" |
| 69 | + root = Path.cwd() |
| 70 | + |
| 71 | + work_dir = get_work_dir(root, directory) |
| 72 | + build_dir = get_build_dir(root, directory) |
| 73 | + |
| 74 | + data = get_project_data(build_dir) |
| 75 | + bricks = toml.get_project_packages_from_polylith_section(data) |
| 76 | + |
| 77 | + if not bricks: |
| 78 | + return |
| 79 | + |
| 80 | + destination_dir = building.calculate_destination_dir(data) |
| 81 | + |
| 82 | + if work_dir.exists(): |
| 83 | + print(f"Removing temporary working directory: {work_dir}") |
| 84 | + building.cleanup(work_dir) |
| 85 | + |
| 86 | + if destination_dir: |
| 87 | + destination_path = build_dir / destination_dir |
| 88 | + print(f"Removing bricks path used during build: {destination_path}") |
| 89 | + building.cleanup(destination_path) |
0 commit comments