11import shutil
22from pathlib import Path
3- from typing import Any , Dict , List
3+ from typing import Any , Dict , List , Set
44
55from hatchling .builders .hooks .plugin .interface import BuildHookInterface
66from polylith import parsing , repo , toml
@@ -43,6 +43,21 @@ def collect_configured_exclude_patterns(data: dict, target_name: str) -> set:
4343 return set (exclude )
4444
4545
46+ def copy_bricks (bricks : dict , work_dir : Path , exclude_patterns : Set [str ]) -> List [Path ]:
47+ return [
48+ parsing .copy_brick (source , brick , work_dir , exclude_patterns )
49+ for source , brick in bricks .items ()
50+ ]
51+
52+
53+ def rewrite_modules (paths : List [Path ], ns : str , top_ns : str ) -> None :
54+ for path in paths :
55+ rewritten_bricks = parsing .rewrite_modules (path , ns , top_ns )
56+
57+ for item in rewritten_bricks :
58+ print (f"Updated { item } with new top namespace for local imports." )
59+
60+
4661class PolylithBricksHook (BuildHookInterface ):
4762 PLUGIN_NAME = "polylith-bricks"
4863
@@ -58,24 +73,24 @@ def initialize(self, version: str, build_data: Dict[str, Any]) -> None:
5873 if not bricks or not found_bricks :
5974 return
6075
76+ ns = parsing .parse_brick_namespace_from_path (bricks )
6177 top_ns = core .get_top_namespace (data , self .config )
6278 work_dir = core .get_work_dir (self .config )
79+ exclude_patterns = collect_configured_exclude_patterns (data , self .target_name )
6380
64- if not top_ns :
81+ if not top_ns and not exclude_patterns :
6582 build_data [include_key ] = bricks
6683 return
6784
68- ns = parsing . parse_brick_namespace_from_path ( bricks )
69- exclude_patterns = collect_configured_exclude_patterns ( data , self . target_name )
85+ key = work_dir . as_posix ( )
86+ paths = copy_bricks ( bricks , work_dir , exclude_patterns )
7087
71- for source , brick in bricks . items () :
72- path = parsing . copy_brick ( source , brick , work_dir , exclude_patterns )
73- rewritten_bricks = parsing . rewrite_modules ( path , ns , top_ns )
88+ if not top_ns :
89+ build_data [ include_key ] = { f" { key } / { ns } " : ns }
90+ return
7491
75- for item in rewritten_bricks :
76- print (f"Updated { item } with new top namespace for local imports." )
92+ rewrite_modules (paths , ns , top_ns )
7793
78- key = work_dir .as_posix ()
7994 build_data [include_key ][key ] = top_ns
8095
8196 def finalize (self , * args , ** kwargs ) -> None :
0 commit comments