Skip to content

Commit a520633

Browse files
committed
Address review comments
Refactor web static build to remove fragile type inference, apply all marker styles per file and streamline wheel processing
1 parent d1f9f74 commit a520633

1 file changed

Lines changed: 53 additions & 55 deletions

File tree

src/briefcase/platforms/web/static.py

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ def write_inserts(self, app: AppConfig, filename: Path, inserts: dict[str,dict[s
128128
return
129129

130130
# Each insert slot and its package contributions are processed in sorted order
131-
for insert, packages in sorted(inserts.items()):
132-
packages = inserts[insert]
133-
131+
for insert, pkg_contribs in sorted(inserts.items()):
134132
html_banner = (
135133
"<!--------------------------------------------------\n"
136134
" * {package}\n"
@@ -144,6 +142,19 @@ def write_inserts(self, app: AppConfig, filename: Path, inserts: dict[str,dict[s
144142
"{content}"
145143
)
146144

145+
# Build bodies from the same contributions
146+
html_body = "\n".join(
147+
html_banner.format(package=pkg, content=text)
148+
for pkg, text in sorted(pkg_contribs.items())
149+
if text
150+
)
151+
css_body = "\n".join(
152+
css_banner.format(package=pkg, content=text)
153+
for pkg, text in sorted(pkg_contribs.items())
154+
if text
155+
)
156+
body_map = {"html": html_body, "css": css_body}
157+
147158
# Marker patterns for HTML and CSS/JS
148159
marker_styles = [
149160
# HTML
@@ -160,44 +171,31 @@ def write_inserts(self, app: AppConfig, filename: Path, inserts: dict[str,dict[s
160171
),
161172
]
162173

163-
# Sort and build bodies for HTML and CSS inserts
164-
html_body = "\n".join(
165-
html_banner.format(package=pkg, content=pkg_content.get("html", ""))
166-
for pkg, pkg_content in sorted(packages.items())
167-
if pkg_content.get("html")
168-
)
169-
css_body = "\n".join(
170-
css_banner.format(package=pkg, content=pkg_content.get("css", ""))
171-
for pkg, pkg_content in sorted(packages.items())
172-
if pkg_content.get("css")
173-
)
174-
175-
# Find first matching marker in file
176-
matched_style = None
177-
pattern = re.compile(
178-
pattern_tmpl.format(insert=insert),
179-
flags=re.MULTILINE | re.DOTALL,
180-
)
174+
# Apply all matching marker styles
175+
any_match = False
181176
for pattern_tmpl, repl_tmpl, kind in marker_styles:
182-
if matched_style is None and pattern.search(file_text):
183-
matched_style = (pattern, repl_tmpl, kind)
184-
185-
if matched_style is not None:
186-
# Replace marker region with assembled body
187-
pattern, repl_tmpl, kind = matched_style
188-
body = html_body if kind == "html" else css_body
189-
file_text = pattern.sub(
190-
repl_tmpl.format(insert=insert, content=body),
191-
file_text,
177+
pattern = re.compile(
178+
pattern_tmpl.format(insert=insert),
179+
flags=re.MULTILINE | re.DOTALL,
192180
)
193-
else:
181+
if pattern.search(file_text):
182+
file_text = pattern.sub(
183+
repl_tmpl.format(insert=insert, content=body_map.get(kind, "")),
184+
file_text,
185+
)
186+
any_match = True
187+
188+
if not any_match:
194189
self.console.warning(
195190
f" Slot '{insert}' markers not found in {filename}; skipping."
196191
)
192+
197193
# Save modified content
198194
target_path.write_text(file_text, encoding="utf-8")
199195

200-
def _process_wheel(self, wheelfile, inserts, static_path):
196+
def _process_wheel(
197+
self, wheelfile, inserts: dict[str, dict[str, dict[str, str]]], static_path
198+
):
201199
"""Process a wheel, extracting any content that needs to be compiled into the
202200
final project.
203201
@@ -252,28 +250,28 @@ def _process_wheel(self, wheelfile, inserts, static_path):
252250
f"{source}: insert must be UTF-8 encoded"
253251
) from e
254252

255-
# Classify insert as HTML vs CSS/JS
256-
t_ext = Path(target).suffix.lower()
257-
kind = "css" if t_ext in {".css", ".js"} else "html"
253+
# Store raw contribution text per package
254+
pkg_map = (
255+
inserts
256+
.setdefault(target, {})
257+
.setdefault(insert, {})
258+
)
259+
# Append if the same package contributes multiple files for the same slot
260+
if package_key in pkg_map and pkg_map[package_key]:
261+
pkg_map[package_key] += "\n" + text
262+
else:
263+
pkg_map[package_key] = text
258264

259-
# Ensure nested dict structure exists
260-
pkg_entry = (
261-
inserts.setdefault(target, {})
262-
.setdefault(insert, {})
263-
.setdefault(package_key, {"html": "", "css": ""})
264-
)
265-
# Append into the right bucket
266-
if pkg_entry[kind]:
267-
pkg_entry[kind] += "\n"
268-
pkg_entry[kind] += text
269-
270-
# Handle static files under deploy/static
271-
elif parts[:2] == ("deploy", "static"):
272-
rel = Path(*parts[2:])
273-
outfilename = pkg_static_root / rel
274-
outfilename.parent.mkdir(parents=True, exist_ok=True)
275-
with outfilename.open("wb") as f:
276-
f.write(wheel.read(filename))
265+
continue
266+
267+
# Handle static files under deploy/static
268+
if parts[:2] == ("deploy", "static"):
269+
rel = Path(*parts[2:])
270+
outfilename = pkg_static_root / rel
271+
outfilename.parent.mkdir(parents=True, exist_ok=True)
272+
with outfilename.open("wb") as f:
273+
f.write(wheel.read(filename))
274+
continue
277275

278276
def extract_backend_config(self, wheels):
279277
"""Processes multiple wheels to gather a config.toml and a base pyscript.toml
@@ -443,7 +441,7 @@ def build_app(self, app: AppConfig, **kwargs):
443441
sentinel=" ******************* Wheel contributed styles **********************/",
444442
)
445443

446-
inserts: dict[str, dict[str, dict[str, dict[str, str]]]] = {}
444+
inserts: dict[str, dict[str, dict[str, str]]] = {}
447445
static_root = self.static_path(app)
448446

449447
for wheelfile in sorted(self.wheel_path(app).glob("*.whl")):

0 commit comments

Comments
 (0)