|
26 | 26 | from .plugins import pm |
27 | 27 | from .staticfiles import Asset |
28 | 28 | from .staticfiles import AssetType |
29 | | -from .templates import gather_bird_tag_template_usage |
| 29 | +from .templates import find_components_in_template |
30 | 30 | from .templates import get_component_directories |
31 | 31 | from .templates import get_template_names |
32 | 32 | from .templatetags.tags.bird import BirdNode |
33 | 33 | from .templatetags.tags.slot import DEFAULT_SLOT |
34 | 34 | from .templatetags.tags.slot import SlotNode |
35 | | -from .utils import get_files_from_dirs |
36 | 35 |
|
37 | 36 |
|
38 | 37 | @dataclass(frozen=True, slots=True) |
@@ -76,17 +75,34 @@ def source(self): |
76 | 75 | return self.template.template.source |
77 | 76 |
|
78 | 77 | @classmethod |
79 | | - def from_abs_path(cls, path: Path, root: Path) -> Component: |
80 | | - name = str(path.relative_to(root).with_suffix("")).replace("/", ".") |
81 | | - return cls.from_name(name) |
| 78 | + def from_abs_path(cls, path: Path) -> Component: |
| 79 | + template = select_template([str(path)]) |
| 80 | + return cls.from_template(template) |
82 | 81 |
|
83 | 82 | @classmethod |
84 | | - def from_name(cls, name: str): |
| 83 | + def from_name(cls, name: str) -> Component: |
85 | 84 | template_names = get_template_names(name) |
86 | 85 | template = select_template(template_names) |
| 86 | + return cls.from_template(template) |
| 87 | + |
| 88 | + @classmethod |
| 89 | + def from_template(cls, template: DjangoTemplate) -> Component: |
| 90 | + template_path = Path(template.template.origin.name) |
| 91 | + |
| 92 | + for component_dir in get_component_directories(): |
| 93 | + try: |
| 94 | + relative_path = template_path.relative_to(component_dir) |
| 95 | + name = str(relative_path.with_suffix("")).replace("/", ".") |
| 96 | + break |
| 97 | + except ValueError: |
| 98 | + continue |
| 99 | + else: |
| 100 | + name = template_path.stem |
| 101 | + |
87 | 102 | assets: list[Iterable[Asset]] = pm.hook.collect_component_assets( |
88 | 103 | template_path=Path(template.template.origin.name) |
89 | 104 | ) |
| 105 | + |
90 | 106 | return cls( |
91 | 107 | name=name, template=template, assets=frozenset(itertools.chain(*assets)) |
92 | 108 | ) |
@@ -182,24 +198,6 @@ def __init__(self): |
182 | 198 | self._components: dict[str, Component] = {} |
183 | 199 | self._template_usage: dict[Path, set[str]] = defaultdict(set) |
184 | 200 |
|
185 | | - def discover_components(self) -> None: |
186 | | - component_dirs = get_component_directories() |
187 | | - component_paths = get_files_from_dirs(component_dirs) |
188 | | - for component_abs_path, root_abs_path in component_paths: |
189 | | - if component_abs_path.suffix != ".html": |
190 | | - continue |
191 | | - |
192 | | - component = Component.from_abs_path(component_abs_path, root_abs_path) |
193 | | - |
194 | | - if component.name not in self._components: |
195 | | - self._components[component.name] = component |
196 | | - |
197 | | - templates_using_bird_tag = gather_bird_tag_template_usage() |
198 | | - for template_abs_path, component_names in templates_using_bird_tag: |
199 | | - self._template_usage[template_abs_path] = component_names |
200 | | - for component_name in component_names: |
201 | | - self._component_usage[component_name].add(template_abs_path) |
202 | | - |
203 | 201 | def reset(self) -> None: |
204 | 202 | """Reset the registry, used for testing.""" |
205 | 203 | self._component_usage = defaultdict(set) |
@@ -227,8 +225,19 @@ def get_component_names_used_in_template( |
227 | 225 | self, template_path: str | Path |
228 | 226 | ) -> set[str]: |
229 | 227 | """Get names of components used in a template.""" |
230 | | - path = Path(template_path) if isinstance(template_path, str) else template_path |
231 | | - return self._template_usage.get(path, set()) |
| 228 | + |
| 229 | + path = Path(template_path) |
| 230 | + |
| 231 | + if path in self._template_usage: |
| 232 | + return self._template_usage[path] |
| 233 | + |
| 234 | + components = find_components_in_template(template_path) |
| 235 | + |
| 236 | + self._template_usage[path] = components |
| 237 | + for component_name in components: |
| 238 | + self._component_usage[component_name].add(path) |
| 239 | + |
| 240 | + return components |
232 | 241 |
|
233 | 242 | def get_component_usage( |
234 | 243 | self, template_path: str | Path |
|
0 commit comments