Skip to content

Commit

Permalink
test: overlay generator modules
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Oct 30, 2023
1 parent 5bac0ec commit 6358a8c
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/bolt_overgen/beet.yml
Original file line number Diff line number Diff line change
@@ -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
60 changes: 60 additions & 0 deletions examples/bolt_overgen/overgen.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 3 additions & 0 deletions examples/bolt_overgen/src/data/demo/functions/foo.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
value = "abc"
print(value)
say this should be left untouched
7 changes: 7 additions & 0 deletions examples/bolt_overgen/src/data/demo/overgen/stuff.bolt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare_overlay_formats(
min_inclusive=18,
max_inclusive=19,
)

function ./foo:
say stuff
49 changes: 49 additions & 0 deletions tests/snapshots/examples__build_bolt_overgen__0.pack.md
Original file line number Diff line number Diff line change
@@ -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`

0 comments on commit 6358a8c

Please sign in to comment.