diff --git a/examples/bolt_overgen/beet.yml b/examples/bolt_overgen/beet.yml new file mode 100644 index 0000000..64b5c77 --- /dev/null +++ b/examples/bolt_overgen/beet.yml @@ -0,0 +1,10 @@ +require: + - bolt.contrib.sandbox + - overgen.define_custom_resources + - overgen.define_compilation_unit_providers + - overgen.define_module_globals +data_pack: + load: "src" +pipeline: + - mecha + - overgen.remove_custom_resources diff --git a/examples/bolt_overgen/overgen.py b/examples/bolt_overgen/overgen.py new file mode 100644 index 0000000..df9e2ec --- /dev/null +++ b/examples/bolt_overgen/overgen.py @@ -0,0 +1,60 @@ +from dataclasses import dataclass +from typing import Any, ClassVar, Iterable, List, Optional, Tuple, Union + +from beet import Context, DataPack, ResourcePack, TextFile, TextFileBase +from beet.core.utils import FormatsRangeDict, normalize_string +from mecha import CompilationDatabase, CompilationUnit, Mecha + +from bolt import Runtime + + +class Overgen(TextFile): + scope: ClassVar[tuple[str, ...]] = ("overgen",) + extension: ClassVar[str] = ".bolt" + + +def define_custom_resources(ctx: Context): + ctx.data.extend_namespace.append(Overgen) + + +def provide_compilation_units( + pack: Union[ResourcePack, DataPack], + match: Optional[List[str]] = None, +) -> Iterable[Tuple[TextFileBase[Any], CompilationUnit]]: + for resource_location in pack[Overgen].match(*match or ["*"]): + file_instance = pack[Overgen][resource_location] + + yield file_instance, CompilationUnit( + resource_location=resource_location, + pack=pack.overlays[normalize_string(resource_location)], + ) + + +def define_compilation_unit_providers(ctx: Context): + mc = ctx.inject(Mecha) + mc.providers = [provide_compilation_units] + + +@dataclass +class DeclareOverlayFormats: + database: CompilationDatabase + + def __call__(self, min_inclusive: int, max_inclusive: int): + pack = self.database[self.database.current].pack + if pack is not None: + pack.supported_formats = FormatsRangeDict( + min_inclusive=min_inclusive, + max_inclusive=max_inclusive, + ) + + +def define_module_globals(ctx: Context): + mc = ctx.inject(Mecha) + declare_overlay_formats = DeclareOverlayFormats(mc.database) + + runtime = ctx.inject(Runtime) + runtime.expose("declare_overlay_formats", declare_overlay_formats) + + +def remove_custom_resources(ctx: Context): + ctx.data[Overgen].clear() diff --git a/examples/bolt_overgen/src/data/demo/functions/foo.mcfunction b/examples/bolt_overgen/src/data/demo/functions/foo.mcfunction new file mode 100644 index 0000000..5be7a20 --- /dev/null +++ b/examples/bolt_overgen/src/data/demo/functions/foo.mcfunction @@ -0,0 +1,3 @@ +value = "abc" +print(value) +say this should be left untouched diff --git a/examples/bolt_overgen/src/data/demo/overgen/stuff.bolt b/examples/bolt_overgen/src/data/demo/overgen/stuff.bolt new file mode 100644 index 0000000..912be95 --- /dev/null +++ b/examples/bolt_overgen/src/data/demo/overgen/stuff.bolt @@ -0,0 +1,7 @@ +declare_overlay_formats( + min_inclusive=18, + max_inclusive=19, +) + +function ./foo: + say stuff diff --git a/tests/snapshots/examples__build_bolt_overgen__0.pack.md b/tests/snapshots/examples__build_bolt_overgen__0.pack.md new file mode 100644 index 0000000..ee05893 --- /dev/null +++ b/tests/snapshots/examples__build_bolt_overgen__0.pack.md @@ -0,0 +1,49 @@ +# Lectern snapshot + +## Data pack + +`@data_pack pack.mcmeta` + +```json +{ + "pack": { + "pack_format": 18, + "description": "" + }, + "overlays": { + "entries": [ + { + "formats": { + "min_inclusive": 18, + "max_inclusive": 19 + }, + "directory": "demo_stuff" + } + ] + } +} +``` + +### demo + +`@function demo:foo` + +```mcfunction +value = "abc" +print(value) +say this should be left untouched +``` + +## Overlay `demo_stuff` + +`@overlay demo_stuff` + +### demo + +`@function demo:foo` + +```mcfunction +say stuff +``` + +`@endoverlay`