From 3e3fa7d810595f7bc0dc8c1691e54d31c7ecc5aa Mon Sep 17 00:00:00 2001 From: Valentin Berlier Date: Tue, 31 Oct 2023 02:57:33 +0100 Subject: [PATCH] feat: asset module --- bolt/module.py | 8 +++++++ bolt/runtime.py | 18 +++++++++++--- examples/bolt_assets/beet.yml | 9 +++++++ .../src/assets/demo/modules/foo.bolt | 1 + poetry.lock | 8 +++---- pyproject.toml | 2 +- .../examples__build_bolt_assets__0.pack.md | 24 +++++++++++++++++++ 7 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 examples/bolt_assets/beet.yml create mode 100644 examples/bolt_assets/src/assets/demo/modules/foo.bolt create mode 100644 tests/snapshots/examples__build_bolt_assets__0.pack.md diff --git a/bolt/module.py b/bolt/module.py index 5a40b2c..90c263a 100644 --- a/bolt/module.py +++ b/bolt/module.py @@ -1,5 +1,6 @@ __all__ = [ "Module", + "AssetModule", "CompiledModule", "ModuleManager", "ModuleCacheBackend", @@ -85,6 +86,13 @@ class Module(TextFile): extension: ClassVar[str] = ".bolt" +class AssetModule(TextFile): + """Class representing a bolt module in a resource pack.""" + + scope: ClassVar[Tuple[str, ...]] = ("modules",) + extension: ClassVar[str] = ".bolt" + + MacroLibrary = Dict[str, Dict[Tuple[str, AstMacro], Optional[Tuple[str, str]]]] diff --git a/bolt/runtime.py b/bolt/runtime.py index 034d367..219aa4b 100644 --- a/bolt/runtime.py +++ b/bolt/runtime.py @@ -19,6 +19,7 @@ CommandSpec, CommandTree, CompilationDatabase, + CompilationUnitProvider, Diagnostic, FileTypeCompilationUnitProvider, Mecha, @@ -36,7 +37,13 @@ from .helpers import get_bolt_helpers from .loop_info import loop_info from .memo import MemoHandler, MemoRegistry -from .module import CompiledModule, Module, ModuleCacheBackend, ModuleManager +from .module import ( + AssetModule, + CompiledModule, + Module, + ModuleCacheBackend, + ModuleManager, +) from .parse import get_bolt_parsers from .utils import internal @@ -54,6 +61,7 @@ class Runtime(CommandEmitter): evaluate: "Evaluator" spec: CommandSpec + module_provider: CompilationUnitProvider def __init__(self, ctx: Union[Context, Mecha]): super().__init__() @@ -74,6 +82,7 @@ def __init__(self, ctx: Union[Context, Mecha]): ) ctx.data.extend_namespace.append(Module) + ctx.assets.extend_namespace.append(AssetModule) self.globals["ctx"] = ctx @@ -126,7 +135,8 @@ def __init__(self, ctx: Union[Context, Mecha]): self.spec = mc.spec - mc.providers.append(FileTypeCompilationUnitProvider([Module])) + self.module_provider = FileTypeCompilationUnitProvider([Module, AssetModule]) + mc.providers.append(self.module_provider) commands_json = files("bolt.resources").joinpath("commands.json").read_text() command_tree = CommandTree.parse_raw(commands_json) @@ -207,6 +217,8 @@ def finalize(self, ctx: Context): finally: for pack in [ctx.data, *ctx.data.overlays.values()]: pack[Module].clear() + for pack in [ctx.assets, *ctx.assets.overlays.values()]: + pack[AssetModule].clear() self.memo.finalize() @@ -237,7 +249,7 @@ def root(self, node: AstRoot) -> Optional[AstRoot]: compilation_unit, module = self.modules.match_ast(node) if ( - isinstance(self.modules.database.current, Module) + isinstance(self.modules.database.current, (Module, AssetModule)) and not module.executed and module.resource_location and not self.entrypoint_spec.match_file(module.resource_location) diff --git a/examples/bolt_assets/beet.yml b/examples/bolt_assets/beet.yml new file mode 100644 index 0000000..3f4787a --- /dev/null +++ b/examples/bolt_assets/beet.yml @@ -0,0 +1,9 @@ +require: + - bolt +resource_pack: + load: "src" +pipeline: + - mecha +meta: + bolt: + entrypoint: "*" diff --git a/examples/bolt_assets/src/assets/demo/modules/foo.bolt b/examples/bolt_assets/src/assets/demo/modules/foo.bolt new file mode 100644 index 0000000..f56500f --- /dev/null +++ b/examples/bolt_assets/src/assets/demo/modules/foo.bolt @@ -0,0 +1 @@ +model minecraft:block/stone {"parent": "block/cube"} diff --git a/poetry.lock b/poetry.lock index 0d6101b..34d872b 100644 --- a/poetry.lock +++ b/poetry.lock @@ -706,14 +706,14 @@ files = [ [[package]] name = "mecha" -version = "0.81.1" +version = "0.83.0" description = "A powerful Minecraft command library" category = "main" optional = false python-versions = ">=3.10,<4.0" files = [ - {file = "mecha-0.81.1-py3-none-any.whl", hash = "sha256:0a461428881fba0b12d0b610e3b1a3e2b7f768c315474e0ec80f313f35539758"}, - {file = "mecha-0.81.1.tar.gz", hash = "sha256:fb14f61676eb506bd2f72038368be24ae3d1edee84af109f616f15529daa7471"}, + {file = "mecha-0.83.0-py3-none-any.whl", hash = "sha256:eb51a7af975cc81221f625ef24a90db041f2fcc96d6e85cb92385b33272b949c"}, + {file = "mecha-0.83.0.tar.gz", hash = "sha256:639a7bb1b11b7fc5b501ac2b0f7f7a9b2475898c11ea2d028e543e019cadb932"}, ] [package.dependencies] @@ -1488,4 +1488,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "f3a0dae896acc9385475ff31afa5e91fd139beb49f0c3d15ff1c283dd89803bb" +content-hash = "b3b95e7ad92fb54dc71f0da8d57e8fec9dd025f497458519c8c0fce4f3ee6714" diff --git a/pyproject.toml b/pyproject.toml index 6f3f889..8b44b50 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,7 +24,7 @@ include = ["bolt/py.typed"] [tool.poetry.dependencies] python = "^3.10" beet = ">=0.97.0" -mecha = ">=0.81.1" +mecha = ">=0.83.0" [tool.poetry.group.dev.dependencies] pytest = "^7.4.2" diff --git a/tests/snapshots/examples__build_bolt_assets__0.pack.md b/tests/snapshots/examples__build_bolt_assets__0.pack.md new file mode 100644 index 0000000..f3a9803 --- /dev/null +++ b/tests/snapshots/examples__build_bolt_assets__0.pack.md @@ -0,0 +1,24 @@ +# Lectern snapshot + +## Resource pack + +`@resource_pack pack.mcmeta` + +```json +{ + "pack": { + "pack_format": 18, + "description": "" + } +} +``` + +### minecraft + +`@model minecraft:block/stone` + +```json +{ + "parent": "block/cube" +} +```