webcompy-cli 0.0.14__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- webcompy_cli/__init__.py +20 -0
- webcompy_cli/_argparser.py +232 -0
- webcompy_cli/_browser_probes.py +648 -0
- webcompy_cli/_browser_test_harness.py +554 -0
- webcompy_cli/_build.py +425 -0
- webcompy_cli/_console_types.py +15 -0
- webcompy_cli/_dependency_resolver.py +301 -0
- webcompy_cli/_dualrun_pytest_plugin.py +130 -0
- webcompy_cli/_exception.py +9 -0
- webcompy_cli/_generate.py +224 -0
- webcompy_cli/_init_project.py +54 -0
- webcompy_cli/_inspect.py +741 -0
- webcompy_cli/_inspect_pyexec.py +244 -0
- webcompy_cli/_lock.py +82 -0
- webcompy_cli/_lockfile.py +396 -0
- webcompy_cli/_lockfile_sync.py +343 -0
- webcompy_cli/_output.py +150 -0
- webcompy_cli/_prebuild.py +661 -0
- webcompy_cli/_pwa.py +474 -0
- webcompy_cli/_pyodide_downloader.py +113 -0
- webcompy_cli/_pyodide_lock.py +74 -0
- webcompy_cli/_runtime_downloader.py +222 -0
- webcompy_cli/_server.py +568 -0
- webcompy_cli/_static_files.py +27 -0
- webcompy_cli/_utils.py +72 -0
- webcompy_cli/_version_sweep.py +213 -0
- webcompy_cli/_wheel_builder.py +509 -0
- webcompy_cli/config/__init__.py +23 -0
- webcompy_cli/config/_build_config.py +168 -0
- webcompy_cli/config/_pwa_config.py +203 -0
- webcompy_cli/config/_server_config.py +53 -0
- webcompy_cli/py.typed +0 -0
- webcompy_cli/template_data/app/__init__.py +0 -0
- webcompy_cli/template_data/app/app.py +22 -0
- webcompy_cli/template_data/app/components/__init__.py +0 -0
- webcompy_cli/template_data/app/components/fizzbuzz.py +117 -0
- webcompy_cli/template_data/app/components/home.py +15 -0
- webcompy_cli/template_data/app/components/input.py +34 -0
- webcompy_cli/template_data/app/components/navigation.py +36 -0
- webcompy_cli/template_data/app/components/not_found.py +22 -0
- webcompy_cli/template_data/app/components/root.py +16 -0
- webcompy_cli/template_data/app/router.py +17 -0
- webcompy_cli/template_data/webcompy_config.py +12 -0
- webcompy_cli-0.0.14.dist-info/METADATA +53 -0
- webcompy_cli-0.0.14.dist-info/RECORD +47 -0
- webcompy_cli-0.0.14.dist-info/WHEEL +4 -0
- webcompy_cli-0.0.14.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
"""Synchronization helpers between the lock file and project manifests."""
|
|
2
|
+
|
|
3
|
+
# ruff: noqa: RUF001
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import os
|
|
7
|
+
import pathlib
|
|
8
|
+
import re
|
|
9
|
+
import shutil
|
|
10
|
+
import subprocess
|
|
11
|
+
import sys
|
|
12
|
+
import tomllib
|
|
13
|
+
from typing import TYPE_CHECKING
|
|
14
|
+
|
|
15
|
+
from webcompy_cli._exception import WebComPyCliException
|
|
16
|
+
from webcompy_cli._lockfile import Lockfile
|
|
17
|
+
from webcompy_cli.config._server_config import LockfileSyncConfig
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
from webcompy_cli.config._build_config import WebComPyBuildConfig
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def discover_project_root(app_package_path: pathlib.Path) -> pathlib.Path:
|
|
24
|
+
for dir_path in [app_package_path, *app_package_path.parents]:
|
|
25
|
+
if (dir_path / "pyproject.toml").exists():
|
|
26
|
+
return dir_path
|
|
27
|
+
raise WebComPyCliException(
|
|
28
|
+
f"Could not find pyproject.toml above {app_package_path}. "
|
|
29
|
+
"Set LockfileSyncConfig.requirements_path in webcompy_config.py."
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def discover_requirements_path(
|
|
34
|
+
app_package_path: pathlib.Path,
|
|
35
|
+
lockfile_sync_config: LockfileSyncConfig | None = None,
|
|
36
|
+
) -> pathlib.Path:
|
|
37
|
+
if lockfile_sync_config is not None and lockfile_sync_config.requirements_path is not None:
|
|
38
|
+
path = pathlib.Path(lockfile_sync_config.requirements_path)
|
|
39
|
+
if path.is_absolute():
|
|
40
|
+
return path
|
|
41
|
+
return (app_package_path / path).resolve()
|
|
42
|
+
project_root = discover_project_root(app_package_path)
|
|
43
|
+
return project_root / "requirements.txt"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def export_requirements(
|
|
47
|
+
lockfile: Lockfile,
|
|
48
|
+
path: pathlib.Path,
|
|
49
|
+
) -> None:
|
|
50
|
+
entries: list[tuple[str, str]] = []
|
|
51
|
+
for name, entry in lockfile.pure_python_packages.items():
|
|
52
|
+
entries.append((name, entry.version))
|
|
53
|
+
for name, entry in lockfile.wasm_packages.items():
|
|
54
|
+
entries.append((name, entry.version))
|
|
55
|
+
entries.sort(key=lambda x: x[0].lower())
|
|
56
|
+
lines = ["# Generated by webcompy lock --export", ""]
|
|
57
|
+
for name, version in entries:
|
|
58
|
+
lines.append(f"{name}=={version}")
|
|
59
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
60
|
+
path.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def record_requirements_path(
|
|
64
|
+
app_package_path: pathlib.Path,
|
|
65
|
+
requirements_path: pathlib.Path,
|
|
66
|
+
config_module_path: pathlib.Path | None = None,
|
|
67
|
+
) -> None:
|
|
68
|
+
try:
|
|
69
|
+
rel_path = os.path.relpath(str(requirements_path), str(app_package_path))
|
|
70
|
+
except ValueError:
|
|
71
|
+
return
|
|
72
|
+
|
|
73
|
+
def _find_config_module() -> pathlib.Path | None:
|
|
74
|
+
if config_module_path is not None:
|
|
75
|
+
return config_module_path
|
|
76
|
+
for candidate in [
|
|
77
|
+
app_package_path / "webcompy_config.py",
|
|
78
|
+
]:
|
|
79
|
+
if candidate.exists():
|
|
80
|
+
return candidate
|
|
81
|
+
for dir_path in app_package_path.parents:
|
|
82
|
+
candidate = dir_path / "webcompy_config.py"
|
|
83
|
+
if candidate.exists():
|
|
84
|
+
return candidate
|
|
85
|
+
return None
|
|
86
|
+
|
|
87
|
+
config_path = _find_config_module()
|
|
88
|
+
if config_path is None or not config_path.exists():
|
|
89
|
+
print(
|
|
90
|
+
f"Warning: Could not find webcompy_config.py to record requirements_path. "
|
|
91
|
+
f"Please add: lockfile_sync_config = LockfileSyncConfig(requirements_path='{rel_path}')",
|
|
92
|
+
file=sys.stderr,
|
|
93
|
+
)
|
|
94
|
+
return
|
|
95
|
+
source = config_path.read_text(encoding="utf-8")
|
|
96
|
+
has_lockfile_sync_import = "LockfileSyncConfig" in source
|
|
97
|
+
has_lockfile_sync_config = "lockfile_sync_config" in source
|
|
98
|
+
if has_lockfile_sync_config:
|
|
99
|
+
print(
|
|
100
|
+
f"Note: lockfile_sync_config already exists in {config_path}. "
|
|
101
|
+
f"If requirements_path is not set, consider updating it to: "
|
|
102
|
+
f"LockfileSyncConfig(requirements_path='{rel_path}')",
|
|
103
|
+
file=sys.stderr,
|
|
104
|
+
)
|
|
105
|
+
return
|
|
106
|
+
new_lines = []
|
|
107
|
+
if not has_lockfile_sync_import:
|
|
108
|
+
new_lines.append("from webcompy_cli.config import LockfileSyncConfig")
|
|
109
|
+
new_lines.append(f"lockfile_sync_config = LockfileSyncConfig(requirements_path='{rel_path}')")
|
|
110
|
+
with open(config_path, "a", encoding="utf-8") as f:
|
|
111
|
+
f.write("\n" + "\n".join(new_lines) + "\n")
|
|
112
|
+
print(f"Recorded requirements_path='{rel_path}' in {config_path}")
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def sync_from_requirements_txt(
|
|
116
|
+
lockfile: Lockfile,
|
|
117
|
+
path: pathlib.Path,
|
|
118
|
+
) -> list[str]:
|
|
119
|
+
if not path.exists():
|
|
120
|
+
return [f"ℹ No requirements.txt found at {path}"]
|
|
121
|
+
text = path.read_text(encoding="utf-8")
|
|
122
|
+
entries: dict[str, str] = {}
|
|
123
|
+
unpinned: list[str] = []
|
|
124
|
+
for line in text.splitlines():
|
|
125
|
+
stripped = line.strip()
|
|
126
|
+
if not stripped or stripped.startswith("#"):
|
|
127
|
+
continue
|
|
128
|
+
match = re.match(r"^([a-zA-Z0-9_.-]+)==([a-zA-Z0-9_.+!-]+)$", stripped)
|
|
129
|
+
if match:
|
|
130
|
+
name, version = match.group(1), match.group(2)
|
|
131
|
+
normalized = name.lower().replace("-", "_")
|
|
132
|
+
entries[normalized] = version
|
|
133
|
+
elif re.match(r"^([a-zA-Z0-9_.-]+)", stripped):
|
|
134
|
+
name_match = re.match(r"^([a-zA-Z0-9_.-]+)", stripped)
|
|
135
|
+
if name_match:
|
|
136
|
+
unpinned.append(name_match.group(1))
|
|
137
|
+
lockfile_pkgs: dict[str, tuple[str, str]] = {}
|
|
138
|
+
for name, entry in lockfile.pure_python_packages.items():
|
|
139
|
+
lockfile_pkgs[name.lower().replace("-", "_")] = (name, entry.version)
|
|
140
|
+
for name, entry in lockfile.wasm_packages.items():
|
|
141
|
+
lockfile_pkgs[name.lower().replace("-", "_")] = (name, entry.version)
|
|
142
|
+
wasm_pkgs: set[str] = set()
|
|
143
|
+
for name in lockfile.wasm_packages:
|
|
144
|
+
wasm_pkgs.add(name.lower().replace("-", "_"))
|
|
145
|
+
lines: list[str] = []
|
|
146
|
+
seen: set[str] = set()
|
|
147
|
+
for normalized, version in sorted(entries.items()):
|
|
148
|
+
seen.add(normalized)
|
|
149
|
+
if normalized in lockfile_pkgs:
|
|
150
|
+
lock_name, lock_version = lockfile_pkgs[normalized]
|
|
151
|
+
if version == lock_version:
|
|
152
|
+
lines.append(f"✓ {lock_name}: {version} (matches)")
|
|
153
|
+
else:
|
|
154
|
+
lines.append(
|
|
155
|
+
f"⚠ {lock_name}: lock={lock_version}, requirements.txt={version} (mismatch) "
|
|
156
|
+
f"Suggest: pip install {lock_name}=={lock_version}"
|
|
157
|
+
)
|
|
158
|
+
elif normalized in wasm_pkgs:
|
|
159
|
+
lines.append(f"ℹ {normalized}: WASM package, not applicable for local install")
|
|
160
|
+
else:
|
|
161
|
+
lines.append(f"ℹ {normalized}: not in lock file (non-browser dependency?)")
|
|
162
|
+
for name in sorted(unpinned):
|
|
163
|
+
normalized = name.lower().replace("-", "_")
|
|
164
|
+
if normalized in seen:
|
|
165
|
+
continue
|
|
166
|
+
if normalized in lockfile_pkgs:
|
|
167
|
+
lock_name, lock_version = lockfile_pkgs[normalized]
|
|
168
|
+
lines.append(f"ℹ {lock_name}: not pinned in requirements.txt, lock file has {lock_version}")
|
|
169
|
+
return lines
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def sync_from_pyproject_toml(
|
|
173
|
+
lockfile: Lockfile,
|
|
174
|
+
path: pathlib.Path,
|
|
175
|
+
sync_group: str | None = None,
|
|
176
|
+
) -> list[str]:
|
|
177
|
+
if not path.exists():
|
|
178
|
+
return [f"⚠ No pyproject.toml found at {path}"]
|
|
179
|
+
try:
|
|
180
|
+
with open(path, "rb") as f:
|
|
181
|
+
data = tomllib.load(f)
|
|
182
|
+
except Exception as e:
|
|
183
|
+
return [f"⚠ Error parsing pyproject.toml: {e}"]
|
|
184
|
+
if sync_group is None:
|
|
185
|
+
deps = data.get("project", {}).get("dependencies", [])
|
|
186
|
+
if not deps:
|
|
187
|
+
return ["ℹ No [project.dependencies] found in pyproject.toml"]
|
|
188
|
+
else:
|
|
189
|
+
opt_deps = data.get("project", {}).get("optional-dependencies", None)
|
|
190
|
+
if opt_deps is None:
|
|
191
|
+
return ["⚠ No [project.optional-dependencies] found in pyproject.toml"]
|
|
192
|
+
deps = opt_deps.get(sync_group, None)
|
|
193
|
+
if deps is None:
|
|
194
|
+
return [f"⚠ No [project.optional-dependencies.{sync_group}] found in pyproject.toml"]
|
|
195
|
+
lockfile_pkgs: dict[str, tuple[str, str]] = {}
|
|
196
|
+
for name, entry in lockfile.pure_python_packages.items():
|
|
197
|
+
lockfile_pkgs[name.lower().replace("-", "_")] = (name, entry.version)
|
|
198
|
+
for name, entry in lockfile.wasm_packages.items():
|
|
199
|
+
lockfile_pkgs[name.lower().replace("-", "_")] = (name, entry.version)
|
|
200
|
+
wasm_pkgs: set[str] = set()
|
|
201
|
+
for name in lockfile.wasm_packages:
|
|
202
|
+
wasm_pkgs.add(name.lower().replace("-", "_"))
|
|
203
|
+
lines: list[str] = []
|
|
204
|
+
pinned_re = re.compile(r"^([a-zA-Z0-9_.-]+)==([a-zA-Z0-9_.+!-]+)$")
|
|
205
|
+
range_re = re.compile(r"^([a-zA-Z0-9_.-]+)([><=!~].+)$")
|
|
206
|
+
bare_re = re.compile(r"^([a-zA-Z0-9_.-]+)$")
|
|
207
|
+
for dep in deps:
|
|
208
|
+
pin_match = pinned_re.match(dep)
|
|
209
|
+
range_match = range_re.match(dep)
|
|
210
|
+
bare_match = bare_re.match(dep)
|
|
211
|
+
if pin_match:
|
|
212
|
+
name, version = pin_match.group(1), pin_match.group(2)
|
|
213
|
+
normalized = name.lower().replace("-", "_")
|
|
214
|
+
if normalized in lockfile_pkgs:
|
|
215
|
+
lock_name, lock_version = lockfile_pkgs[normalized]
|
|
216
|
+
if version == lock_version:
|
|
217
|
+
lines.append(f"✓ {lock_name}: {version} (matches)")
|
|
218
|
+
else:
|
|
219
|
+
lines.append(
|
|
220
|
+
f"⚠ {lock_name}: lock={lock_version}, pyproject.toml={version} (mismatch) "
|
|
221
|
+
f"Suggest: pip install {lock_name}=={lock_version}"
|
|
222
|
+
)
|
|
223
|
+
elif normalized in wasm_pkgs:
|
|
224
|
+
lines.append(f"ℹ {name}: WASM package, not applicable for local install")
|
|
225
|
+
else:
|
|
226
|
+
lines.append(f"ℹ {name}: not in lock file (non-browser dependency?)")
|
|
227
|
+
elif range_match:
|
|
228
|
+
name, specifier = range_match.group(1), range_match.group(2)
|
|
229
|
+
normalized = name.lower().replace("-", "_")
|
|
230
|
+
if normalized in lockfile_pkgs:
|
|
231
|
+
lock_name, lock_version = lockfile_pkgs[normalized]
|
|
232
|
+
lines.append(
|
|
233
|
+
f'ℹ {lock_name}: not pinned ("{specifier}"), lock file has {lock_version} Suggest: pin to {lock_name}=={lock_version}'
|
|
234
|
+
)
|
|
235
|
+
elif normalized in wasm_pkgs:
|
|
236
|
+
lines.append(f"ℹ {name}: WASM package, not applicable for local install")
|
|
237
|
+
else:
|
|
238
|
+
lines.append(f'ℹ {name}: not pinned ("{specifier}"), not in lock file')
|
|
239
|
+
elif bare_match:
|
|
240
|
+
name = bare_match.group(1)
|
|
241
|
+
normalized = name.lower().replace("-", "_")
|
|
242
|
+
if normalized in lockfile_pkgs:
|
|
243
|
+
lock_name, lock_version = lockfile_pkgs[normalized]
|
|
244
|
+
lines.append(f"ℹ {lock_name}: no version specifier, lock file has {lock_version}")
|
|
245
|
+
elif normalized in wasm_pkgs:
|
|
246
|
+
lines.append(f"ℹ {name}: WASM package, not applicable for local install")
|
|
247
|
+
else:
|
|
248
|
+
lines.append(f"ℹ {name}: no version specifier, not in lock file")
|
|
249
|
+
else:
|
|
250
|
+
lines.append(f"ℹ Could not parse dependency: {dep}")
|
|
251
|
+
return lines
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def sync(
|
|
255
|
+
lockfile: Lockfile,
|
|
256
|
+
project_root: pathlib.Path,
|
|
257
|
+
sync_group: str | None = None,
|
|
258
|
+
) -> list[str]:
|
|
259
|
+
lines: list[str] = []
|
|
260
|
+
req_path = project_root / "requirements.txt"
|
|
261
|
+
pyproject_path = project_root / "pyproject.toml"
|
|
262
|
+
if req_path.exists():
|
|
263
|
+
lines.append("--- requirements.txt ---")
|
|
264
|
+
lines.extend(sync_from_requirements_txt(lockfile, req_path))
|
|
265
|
+
if pyproject_path.exists():
|
|
266
|
+
if sync_group is None:
|
|
267
|
+
lines.append("--- pyproject.toml [project.dependencies] ---")
|
|
268
|
+
else:
|
|
269
|
+
lines.append(f"--- pyproject.toml [project.optional-dependencies.{sync_group}] ---")
|
|
270
|
+
lines.extend(sync_from_pyproject_toml(lockfile, pyproject_path, sync_group))
|
|
271
|
+
if not req_path.exists() and not pyproject_path.exists():
|
|
272
|
+
lines.append(f"⚠ No requirements.txt or pyproject.toml found at {project_root}")
|
|
273
|
+
return lines
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def install_requirements(
|
|
277
|
+
lockfile: Lockfile,
|
|
278
|
+
requirements_path: pathlib.Path,
|
|
279
|
+
) -> None:
|
|
280
|
+
export_requirements(lockfile, requirements_path)
|
|
281
|
+
if shutil.which("uv"):
|
|
282
|
+
result = subprocess.run(
|
|
283
|
+
["uv", "pip", "install", "-r", str(requirements_path)],
|
|
284
|
+
)
|
|
285
|
+
else:
|
|
286
|
+
result = subprocess.run(
|
|
287
|
+
[sys.executable, "-m", "pip", "install", "-r", str(requirements_path)],
|
|
288
|
+
)
|
|
289
|
+
if result.returncode != 0:
|
|
290
|
+
sys.exit(result.returncode)
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def _parse_dependency_name(dep: str) -> str:
|
|
294
|
+
match = re.match(r"^([a-zA-Z0-9_.-]+)", dep)
|
|
295
|
+
if match:
|
|
296
|
+
return match.group(1)
|
|
297
|
+
return dep
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def resolve_dependencies(build_config: WebComPyBuildConfig) -> None:
|
|
301
|
+
if build_config.dependencies is not None:
|
|
302
|
+
return
|
|
303
|
+
project_root = discover_project_root(build_config.app_package_path)
|
|
304
|
+
pyproject_path = project_root / "pyproject.toml"
|
|
305
|
+
try:
|
|
306
|
+
with open(pyproject_path, "rb") as f:
|
|
307
|
+
data = tomllib.load(f)
|
|
308
|
+
except Exception as e:
|
|
309
|
+
raise WebComPyCliException(f"Error reading pyproject.toml: {e}") from e
|
|
310
|
+
if build_config.dependencies_from is None:
|
|
311
|
+
deps = data.get("project", {}).get("dependencies", None)
|
|
312
|
+
if deps is None:
|
|
313
|
+
raise WebComPyCliException(
|
|
314
|
+
"No [project.dependencies] found in pyproject.toml. "
|
|
315
|
+
"Set WebComPyBuildConfig.dependencies explicitly or add [project.dependencies]."
|
|
316
|
+
)
|
|
317
|
+
else:
|
|
318
|
+
opt_deps = data.get("project", {}).get("optional-dependencies", None)
|
|
319
|
+
if opt_deps is None:
|
|
320
|
+
raise WebComPyCliException(
|
|
321
|
+
f"No [project.optional-dependencies] found in pyproject.toml. "
|
|
322
|
+
f"Set WebComPyBuildConfig.dependencies explicitly or add [project.optional-dependencies.{build_config.dependencies_from}]."
|
|
323
|
+
)
|
|
324
|
+
deps = opt_deps.get(build_config.dependencies_from, None)
|
|
325
|
+
if deps is None:
|
|
326
|
+
raise WebComPyCliException(
|
|
327
|
+
f"No [project.optional-dependencies.{build_config.dependencies_from}] found in pyproject.toml. "
|
|
328
|
+
f"Set WebComPyBuildConfig.dependencies explicitly or add the optional-dependencies group."
|
|
329
|
+
)
|
|
330
|
+
build_config.dependencies = [_parse_dependency_name(dep) for dep in deps]
|
|
331
|
+
if (
|
|
332
|
+
build_config.dependencies_from is not None
|
|
333
|
+
and build_config.lockfile_sync_config is not None
|
|
334
|
+
and build_config.lockfile_sync_config.sync_group is not None
|
|
335
|
+
and build_config.dependencies_from != build_config.lockfile_sync_config.sync_group
|
|
336
|
+
):
|
|
337
|
+
import sys as _sys
|
|
338
|
+
|
|
339
|
+
_sys.stderr.write(
|
|
340
|
+
f'⚠ WebComPyBuildConfig.dependencies_from="{build_config.dependencies_from}" differs from '
|
|
341
|
+
f'LockfileSyncConfig.sync_group="{build_config.lockfile_sync_config.sync_group}". '
|
|
342
|
+
"This may cause inconsistencies between lock file generation and sync comparison.\n"
|
|
343
|
+
)
|
webcompy_cli/_output.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"""Shared output directory materialization helpers for SSG and prebuild."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import shutil
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
from webcompy.ui._styles import get_styles_files
|
|
11
|
+
from webcompy_cli._pwa import (
|
|
12
|
+
MANIFEST_FILENAME,
|
|
13
|
+
SW_FILENAME,
|
|
14
|
+
generate_sw,
|
|
15
|
+
precache_entries_for_artifacts,
|
|
16
|
+
serialize_manifest,
|
|
17
|
+
)
|
|
18
|
+
from webcompy_cli._static_files import get_static_files
|
|
19
|
+
from webcompy_server._html import PYSCRIPT_VERSION, get_runtime_asset_prefix
|
|
20
|
+
|
|
21
|
+
if TYPE_CHECKING:
|
|
22
|
+
from webcompy.app._app import WebComPyApp
|
|
23
|
+
from webcompy_cli._build import BuildArtifacts
|
|
24
|
+
from webcompy_cli.config._build_config import WebComPyBuildConfig
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def copy_static_files(
|
|
28
|
+
static_files_dir: Path,
|
|
29
|
+
out_dir: Path,
|
|
30
|
+
*,
|
|
31
|
+
relative_paths: tuple[str, ...] | None = None,
|
|
32
|
+
verbose: bool = False,
|
|
33
|
+
) -> tuple[str, ...]:
|
|
34
|
+
"""Copy static files from ``static_files_dir`` into ``out_dir``.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
static_files_dir: Source static files root directory.
|
|
38
|
+
out_dir: Target output directory (``dist`` or ``public``).
|
|
39
|
+
relative_paths: Optional pre-enumerated POSIX-relative paths to copy.
|
|
40
|
+
verbose: Whether to print written file paths to stdout.
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
Sorted tuple of copied POSIX-relative file paths.
|
|
44
|
+
|
|
45
|
+
"""
|
|
46
|
+
resolved_paths = tuple(sorted(get_static_files(static_files_dir) if relative_paths is None else relative_paths))
|
|
47
|
+
for relative_path in resolved_paths:
|
|
48
|
+
src = static_files_dir / relative_path
|
|
49
|
+
dst = out_dir / relative_path
|
|
50
|
+
if not (parent := dst.parent).exists():
|
|
51
|
+
os.makedirs(parent, exist_ok=True)
|
|
52
|
+
shutil.copy(src, dst)
|
|
53
|
+
if verbose:
|
|
54
|
+
print(dst)
|
|
55
|
+
return resolved_paths
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def materialize_build_assets(
|
|
59
|
+
artifacts: BuildArtifacts,
|
|
60
|
+
app_package_path: Path,
|
|
61
|
+
out_dir: Path,
|
|
62
|
+
*,
|
|
63
|
+
verbose: bool = False,
|
|
64
|
+
) -> None:
|
|
65
|
+
"""Materialize wheels, local WASM/runtime assets, resources, and UI CSS in sorted order.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
artifacts: Resolved build artifacts from ``resolve_build_artifacts()``.
|
|
69
|
+
app_package_path: Root directory of the application package.
|
|
70
|
+
out_dir: Target output directory (``dist`` or ``public``).
|
|
71
|
+
verbose: Whether to print written file and directory paths to stdout.
|
|
72
|
+
|
|
73
|
+
"""
|
|
74
|
+
scripts_dir = out_dir / "_webcompy-app-package"
|
|
75
|
+
os.makedirs(scripts_dir, exist_ok=True)
|
|
76
|
+
|
|
77
|
+
if artifacts.app_package_files:
|
|
78
|
+
for filename, (content, _media_type) in sorted(artifacts.app_package_files.items()):
|
|
79
|
+
dst = scripts_dir / filename
|
|
80
|
+
dst.write_bytes(content)
|
|
81
|
+
if verbose:
|
|
82
|
+
print(dst)
|
|
83
|
+
|
|
84
|
+
if artifacts.wasm_asset_files:
|
|
85
|
+
wasm_assets_dir = out_dir / "_webcompy-assets" / "packages"
|
|
86
|
+
os.makedirs(wasm_assets_dir, exist_ok=True)
|
|
87
|
+
for file_name, wheel_path in sorted(artifacts.wasm_asset_files.items()):
|
|
88
|
+
dst = wasm_assets_dir / file_name
|
|
89
|
+
shutil.copy(wheel_path, dst)
|
|
90
|
+
if verbose:
|
|
91
|
+
print(dst)
|
|
92
|
+
|
|
93
|
+
if artifacts.runtime_asset_files:
|
|
94
|
+
runtime_prefix = get_runtime_asset_prefix(PYSCRIPT_VERSION)
|
|
95
|
+
for rel_path, src_path in sorted(artifacts.runtime_asset_files.items()):
|
|
96
|
+
dst = out_dir / runtime_prefix / rel_path
|
|
97
|
+
dst.parent.mkdir(parents=True, exist_ok=True)
|
|
98
|
+
shutil.copy2(src_path, dst)
|
|
99
|
+
if verbose:
|
|
100
|
+
print(dst)
|
|
101
|
+
|
|
102
|
+
if artifacts.resource_allow_list:
|
|
103
|
+
for rel_path in sorted(artifacts.resource_allow_list):
|
|
104
|
+
src = app_package_path / rel_path
|
|
105
|
+
dst = out_dir / "_webcompy-resource" / rel_path
|
|
106
|
+
dst.parent.mkdir(parents=True, exist_ok=True)
|
|
107
|
+
shutil.copy2(src, dst)
|
|
108
|
+
if verbose:
|
|
109
|
+
print(dst)
|
|
110
|
+
|
|
111
|
+
framework_ui_dir = out_dir / "_webcompy-ui"
|
|
112
|
+
framework_ui_dir.mkdir(exist_ok=True)
|
|
113
|
+
for filename, content in sorted(get_styles_files().items()):
|
|
114
|
+
if filename == "index.css" and artifacts.framework_ui_index_css is not None:
|
|
115
|
+
content = artifacts.framework_ui_index_css
|
|
116
|
+
(framework_ui_dir / filename).write_bytes(content)
|
|
117
|
+
if verbose:
|
|
118
|
+
print(framework_ui_dir)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def write_pwa_outputs(
|
|
122
|
+
build_config: WebComPyBuildConfig,
|
|
123
|
+
app: WebComPyApp,
|
|
124
|
+
artifacts: BuildArtifacts,
|
|
125
|
+
out_dir: Path,
|
|
126
|
+
*,
|
|
127
|
+
precache_dir: Path | None,
|
|
128
|
+
verbose: bool = False,
|
|
129
|
+
) -> None:
|
|
130
|
+
"""Write ``manifest.webmanifest`` and ``sw.js`` into ``out_dir``.
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
build_config: Application build configuration.
|
|
134
|
+
app: Application instance whose ``base_url`` scopes the manifest.
|
|
135
|
+
artifacts: Resolved build artifacts describing app version and runtime.
|
|
136
|
+
out_dir: Target output directory (``dist`` or ``public``).
|
|
137
|
+
precache_dir: Directory enumerated for precache entries, or ``None``.
|
|
138
|
+
verbose: Whether to print written file paths to stdout.
|
|
139
|
+
|
|
140
|
+
"""
|
|
141
|
+
manifest_path = out_dir / MANIFEST_FILENAME
|
|
142
|
+
manifest_path.write_text(serialize_manifest(build_config.pwa, app.config.base_url), encoding="utf-8")
|
|
143
|
+
if verbose:
|
|
144
|
+
print(manifest_path)
|
|
145
|
+
|
|
146
|
+
precache = precache_entries_for_artifacts(build_config.pwa, artifacts, dist_dir=precache_dir)
|
|
147
|
+
sw_path = out_dir / SW_FILENAME
|
|
148
|
+
sw_path.write_text(generate_sw(build_config.pwa, artifacts.app_version, precache), encoding="utf-8")
|
|
149
|
+
if verbose:
|
|
150
|
+
print(sw_path)
|