Skip to content

Commit 408f101

Browse files
move internal plugin back to staticfiles module (#187)
* move internal plugin back to `staticfiles` module * change import
1 parent 1b2b49f commit 408f101

File tree

6 files changed

+48
-68
lines changed

6 files changed

+48
-68
lines changed

src/django_bird/plugins/file_assets.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/django_bird/plugins/manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pm.add_hookspecs(hookspecs)
1111

1212
DEFAULT_PLUGINS: list[str] = [
13-
"django_bird.plugins.file_assets",
13+
"django_bird.staticfiles",
1414
]
1515

1616

src/django_bird/staticfiles.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from django.core.checks import CheckMessage
1717
from django.core.files.storage import FileSystemStorage
1818

19+
from django_bird import hookimpl
20+
1921
from ._typing import override
2022
from .apps import DjangoBirdAppConfig
2123
from .conf import app_settings
@@ -107,6 +109,16 @@ def url(self) -> str | None:
107109
return self.storage.url(str(static_relative_path))
108110

109111

112+
@hookimpl
113+
def collect_component_assets(template_path: Path) -> Iterable[Asset]:
114+
assets: list[Asset] = []
115+
for asset_type in AssetType:
116+
asset_path = template_path.with_suffix(asset_type.ext)
117+
if asset_path.exists():
118+
assets.append(Asset(path=asset_path, type=asset_type))
119+
return assets
120+
121+
110122
@final
111123
class BirdAssetStorage(StaticFilesStorage):
112124
def __init__(self, *args: Any, prefix: str, **kwargs: Any):

tests/plugins/__init__.py

Whitespace-only changes.

tests/plugins/test_file_asset.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

tests/test_staticfiles.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from django_bird.staticfiles import Asset
1515
from django_bird.staticfiles import AssetType
1616
from django_bird.staticfiles import BirdAssetFinder
17+
from django_bird.staticfiles import collect_component_assets
1718

1819
from .utils import TestAsset
1920
from .utils import TestComponent
@@ -199,6 +200,40 @@ def test_url_nonexistent(self, templates_dir):
199200
assert asset.url is None
200201

201202

203+
def test_asset_collection(templates_dir):
204+
button = TestComponent(name="button", content="<button>Click me</button>").create(
205+
templates_dir
206+
)
207+
button_css = TestAsset(
208+
component=button,
209+
content=".button { color: blue; }",
210+
asset_type=AssetType.CSS,
211+
).create()
212+
button_js = TestAsset(
213+
component=button, content="console.log('button');", asset_type=AssetType.JS
214+
).create()
215+
216+
assets = collect_component_assets(button.file)
217+
218+
assert Asset(button_css.file, button_css.asset_type) in assets
219+
assert Asset(button_js.file, button_js.asset_type) in assets
220+
221+
222+
def test_asset_collection_nested(templates_dir):
223+
button = TestComponent(
224+
name="button", content="<button>Click me</button>", sub_dir="nested"
225+
).create(templates_dir)
226+
button_css = TestAsset(
227+
component=button,
228+
content=".button { color: blue; }",
229+
asset_type=AssetType.CSS,
230+
).create()
231+
232+
assets = collect_component_assets(button.file)
233+
234+
assert Asset(button_css.file, button_css.asset_type) in assets
235+
236+
202237
class TestBirdAssetFinder:
203238
def test_check(self):
204239
finder = BirdAssetFinder()

0 commit comments

Comments
 (0)