|
| 1 | +"""The built-in WESL packages as files, and wiring them into a project's `wesl.toml` |
| 2 | +so editor tooling (wgsl-analyzer) resolves `import processing::...` and |
| 3 | +`import lygia::...` the same way `Shader` does at runtime.""" |
| 4 | + |
| 5 | +import json |
| 6 | +import shutil |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from . import processing_py as _native |
| 11 | + |
| 12 | +_BEGIN = "# BEGIN processing_py" |
| 13 | +_END = "# END processing_py" |
| 14 | +_VENDOR_DIR = Path(".processing_py") / "shaders" |
| 15 | + |
| 16 | + |
| 17 | +def _default_dir(base): |
| 18 | + # a virtual environment is per project and replaced along with the install |
| 19 | + if sys.prefix != sys.base_prefix: |
| 20 | + return Path(sys.prefix) / "share" / "processing_py" / "shaders" |
| 21 | + return base / _VENDOR_DIR |
| 22 | + |
| 23 | + |
| 24 | +def shader_packages(dest=None): |
| 25 | + """Write the built-in WESL packages under `dest` and return `{name: directory}`. |
| 26 | +
|
| 27 | + `dest` defaults to the virtual environment, or `.processing_py/shaders` in the |
| 28 | + current directory without one. |
| 29 | + """ |
| 30 | + dest = Path(dest).resolve() if dest is not None else _default_dir(Path.cwd()) |
| 31 | + written = {} |
| 32 | + for name, files in _native._shader_packages().items(): |
| 33 | + target = dest / name |
| 34 | + if target.exists(): |
| 35 | + shutil.rmtree(target) |
| 36 | + for rel, contents in files.items(): |
| 37 | + path = target / rel |
| 38 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 39 | + path.write_text(contents, encoding="utf-8") |
| 40 | + written[name] = target |
| 41 | + return written |
| 42 | + |
| 43 | + |
| 44 | +def _toml_path(path, base): |
| 45 | + try: |
| 46 | + p = path.relative_to(base) |
| 47 | + except ValueError: |
| 48 | + p = path |
| 49 | + return json.dumps(p.as_posix()) |
| 50 | + |
| 51 | + |
| 52 | +def _dependency_block(packages, base): |
| 53 | + lines = [_BEGIN] |
| 54 | + for name, path in packages.items(): |
| 55 | + lines.append(f"{name} = {{ path = {_toml_path(path, base)} }}") |
| 56 | + lines.append(_END) |
| 57 | + return lines |
| 58 | + |
| 59 | + |
| 60 | +def _splice(text, block): |
| 61 | + lines = text.splitlines() |
| 62 | + if _BEGIN in lines: |
| 63 | + start = lines.index(_BEGIN) |
| 64 | + try: |
| 65 | + end = lines.index(_END, start) |
| 66 | + except ValueError: |
| 67 | + raise ValueError(f"`{_BEGIN}` without a matching `{_END}`") from None |
| 68 | + lines[start : end + 1] = block |
| 69 | + else: |
| 70 | + header = next( |
| 71 | + (i for i, l in enumerate(lines) if l.split("#", 1)[0].strip() == "[dependencies]"), |
| 72 | + None, |
| 73 | + ) |
| 74 | + if header is None: |
| 75 | + if lines and lines[-1].strip(): |
| 76 | + lines.append("") |
| 77 | + lines += ["[dependencies]", *block] |
| 78 | + else: |
| 79 | + lines[header + 1 : header + 1] = block |
| 80 | + return "\n".join(lines) + "\n" |
| 81 | + |
| 82 | + |
| 83 | +def _validate(text, path): |
| 84 | + try: |
| 85 | + import tomllib |
| 86 | + except ImportError: |
| 87 | + return |
| 88 | + try: |
| 89 | + doc = tomllib.loads(text) |
| 90 | + except tomllib.TOMLDecodeError as e: |
| 91 | + raise ValueError(f"updating {path} would produce invalid TOML ({e}); edit it by hand") from None |
| 92 | + if not isinstance(doc.get("dependencies"), dict): |
| 93 | + raise ValueError(f"{path}: `dependencies` is not a table") |
| 94 | + |
| 95 | + |
| 96 | +def shader_workspace(path=".", vendor=False): |
| 97 | + """Create or update `wesl.toml` in `path` so the built-in `processing` and |
| 98 | + `lygia` packages resolve in the editor. |
| 99 | +
|
| 100 | + The packages are written into the virtual environment. With `vendor=True`, |
| 101 | + or without an environment, they go to `.processing_py/shaders` in `path`, |
| 102 | + for editors that can't see the environment or for checking them in. Rerun |
| 103 | + after upgrading processing-py. Returns the path of the `wesl.toml`. |
| 104 | + """ |
| 105 | + base = Path(path).resolve() |
| 106 | + base.mkdir(parents=True, exist_ok=True) |
| 107 | + dest = base / _VENDOR_DIR if vendor else _default_dir(base) |
| 108 | + packages = shader_packages(dest) |
| 109 | + |
| 110 | + manifest = base / "wesl.toml" |
| 111 | + block = _dependency_block(packages, base) |
| 112 | + if manifest.exists(): |
| 113 | + text = _splice(manifest.read_text(encoding="utf-8"), block) |
| 114 | + else: |
| 115 | + header = ['edition = "2026_pre"', 'root = "."'] |
| 116 | + # keep the project's own module scan out of the packages |
| 117 | + if dest.is_relative_to(base): |
| 118 | + header.append(f'exclude = ["{dest.relative_to(base).as_posix()}/**"]') |
| 119 | + text = "\n".join([*header, "", "[dependencies]", *block]) + "\n" |
| 120 | + _validate(text, manifest) |
| 121 | + manifest.write_text(text, encoding="utf-8") |
| 122 | + return manifest |
0 commit comments