Repository navigation
Extract built-in shader packages for editor tooling. #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import argparse | ||
|
|
||
| from ._shaders import shader_packages, shader_workspace | ||
|
|
||
|
|
||
| def main(argv=None): | ||
| parser = argparse.ArgumentParser(prog="python -m processing_py") | ||
| commands = parser.add_subparsers(dest="command", required=True) | ||
|
|
||
| shaders = commands.add_parser("shaders", help="built-in WESL packages") | ||
| shaders_commands = shaders.add_subparsers(dest="shaders_command", required=True) | ||
|
|
||
| init = shaders_commands.add_parser( | ||
| "init", help="create or update wesl.toml so editors resolve processing:: and lygia::" | ||
| ) | ||
| init.add_argument("dir", nargs="?", default=".") | ||
| init.add_argument( | ||
| "--vendor", | ||
| action="store_true", | ||
| help="write the packages to DIR/.processing_py/shaders instead of the virtual environment", | ||
| ) | ||
|
|
||
| shaders_commands.add_parser("path", help="write the packages and print where they are") | ||
|
|
||
| args = parser.parse_args(argv) | ||
| if args.shaders_command == "init": | ||
| print(f"wrote {shader_workspace(args.dir, vendor=args.vendor)}") | ||
| else: | ||
| for name, path in shader_packages().items(): | ||
| print(f"{name}\t{path}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| """The built-in WESL packages as files, and wiring them into a project's `wesl.toml` | ||
| so editor tooling (wgsl-analyzer) resolves `import processing::...` and | ||
| `import lygia::...` the same way `Shader` does at runtime.""" | ||
|
|
||
| import json | ||
| import shutil | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| from . import processing_py as _native | ||
|
|
||
| _BEGIN = "# BEGIN processing_py" | ||
| _END = "# END processing_py" | ||
| _VENDOR_DIR = Path(".processing_py") / "shaders" | ||
|
|
||
|
Comment on lines
+12
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the project getting serious |
||
|
|
||
| def _default_dir(base): | ||
| # a virtual environment is per project and replaced along with the install | ||
| if sys.prefix != sys.base_prefix: | ||
| return Path(sys.prefix) / "share" / "processing_py" / "shaders" | ||
| return base / _VENDOR_DIR | ||
|
|
||
|
|
||
| def shader_packages(dest=None): | ||
| """Write the built-in WESL packages under `dest` and return `{name: directory}`. | ||
|
|
||
| `dest` defaults to the virtual environment, or `.processing_py/shaders` in the | ||
| current directory without one. | ||
| """ | ||
| dest = Path(dest).resolve() if dest is not None else _default_dir(Path.cwd()) | ||
| written = {} | ||
| for name, files in _native._shader_packages().items(): | ||
| target = dest / name | ||
| if target.exists(): | ||
| shutil.rmtree(target) | ||
| for rel, contents in files.items(): | ||
| path = target / rel | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| path.write_text(contents, encoding="utf-8") | ||
| written[name] = target | ||
| return written | ||
|
|
||
|
|
||
| def _toml_path(path, base): | ||
| try: | ||
| p = path.relative_to(base) | ||
| except ValueError: | ||
| p = path | ||
| return json.dumps(p.as_posix()) | ||
|
|
||
|
|
||
| def _dependency_block(packages, base): | ||
| lines = [_BEGIN] | ||
| for name, path in packages.items(): | ||
| lines.append(f"{name} = {{ path = {_toml_path(path, base)} }}") | ||
| lines.append(_END) | ||
| return lines | ||
|
|
||
|
|
||
| def _splice(text, block): | ||
| lines = text.splitlines() | ||
| if _BEGIN in lines: | ||
| start = lines.index(_BEGIN) | ||
| try: | ||
| end = lines.index(_END, start) | ||
| except ValueError: | ||
| raise ValueError(f"`{_BEGIN}` without a matching `{_END}`") from None | ||
| lines[start : end + 1] = block | ||
| else: | ||
| header = next( | ||
| (i for i, l in enumerate(lines) if l.split("#", 1)[0].strip() == "[dependencies]"), | ||
| None, | ||
| ) | ||
| if header is None: | ||
| if lines and lines[-1].strip(): | ||
| lines.append("") | ||
| lines += ["[dependencies]", *block] | ||
| else: | ||
| lines[header + 1 : header + 1] = block | ||
| return "\n".join(lines) + "\n" | ||
|
|
||
|
|
||
| def _validate(text, path): | ||
| try: | ||
| import tomllib | ||
| except ImportError: | ||
| return | ||
| try: | ||
| doc = tomllib.loads(text) | ||
| except tomllib.TOMLDecodeError as e: | ||
| raise ValueError(f"updating {path} would produce invalid TOML ({e}); edit it by hand") from None | ||
| if not isinstance(doc.get("dependencies"), dict): | ||
| raise ValueError(f"{path}: `dependencies` is not a table") | ||
|
|
||
|
|
||
| def shader_workspace(path=".", vendor=False): | ||
| """Create or update `wesl.toml` in `path` so the built-in `processing` and | ||
| `lygia` packages resolve in the editor. | ||
|
|
||
| The packages are written into the virtual environment. With `vendor=True`, | ||
| or without an environment, they go to `.processing_py/shaders` in `path`, | ||
| for editors that can't see the environment or for checking them in. Rerun | ||
| after upgrading processing-py. Returns the path of the `wesl.toml`. | ||
| """ | ||
| base = Path(path).resolve() | ||
| base.mkdir(parents=True, exist_ok=True) | ||
| dest = base / _VENDOR_DIR if vendor else _default_dir(base) | ||
| packages = shader_packages(dest) | ||
|
|
||
| manifest = base / "wesl.toml" | ||
| block = _dependency_block(packages, base) | ||
| if manifest.exists(): | ||
| text = _splice(manifest.read_text(encoding="utf-8"), block) | ||
| else: | ||
| header = ['edition = "2026_pre"', 'root = "."'] | ||
| # keep the project's own module scan out of the packages | ||
| if dest.is_relative_to(base): | ||
| header.append(f'exclude = ["{dest.relative_to(base).as_posix()}/**"]') | ||
| text = "\n".join([*header, "", "[dependencies]", *block]) + "\n" | ||
| _validate(text, manifest) | ||
| manifest.write_text(text, encoding="utf-8") | ||
| return manifest | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL #noqa