Skip to content

Commit

Permalink
fix: distinct preludes for overlays
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Oct 15, 2023
1 parent a31aabd commit 5f858e5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
14 changes: 9 additions & 5 deletions bolt/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
cast,
)

from beet import BubbleException, Cache, TextFile, TextFileBase
from beet import BubbleException, Cache, DataPack, TextFile, TextFileBase
from beet.core.utils import JsonDict, extra_field, import_from_string, required_field
from mecha import (
AstCacheBackend,
Expand Down Expand Up @@ -152,7 +152,9 @@ class ModuleManager(Mapping[TextFileBase[Any], CompiledModule]):
)
globals: JsonDict = extra_field(default_factory=dict)
builtins: Set[str] = extra_field(default_factory=set)
prelude: Dict[str, Optional[AstPrelude]] = extra_field(default_factory=dict)
prelude: Dict[str, Dict[Optional[DataPack], AstPrelude]] = extra_field(
default_factory=dict
)

execution_count: int = 0

Expand Down Expand Up @@ -356,7 +358,7 @@ def add_prelude(self, *args: Union[str, Iterable[str]]):
if isinstance(arg, str):
arg = [arg]
for resource_location in arg:
self.prelude.setdefault(resource_location, None)
self.prelude.setdefault(resource_location, {})

def export_prelude(self, module: CompiledModule) -> Optional[AstPrelude]:
"""Export local variables and macros from a given module into a prelude import."""
Expand All @@ -379,11 +381,13 @@ def export_prelude(self, module: CompiledModule) -> Optional[AstPrelude]:

def load_prelude(self) -> List[AstPrelude]:
"""Load the prelude imports."""
current_pack = self.database[self.database.current].pack
current_prelude = self.prelude

prelude_imports: List[AstPrelude] = []

for resource_location, prelude in list(current_prelude.items()):
for resource_location, pack_prelude in list(current_prelude.items()):
prelude = pack_prelude.get(current_pack)
if not prelude:
self.prelude = {}
try:
Expand All @@ -398,7 +402,7 @@ def load_prelude(self) -> List[AstPrelude]:
if not prelude:
prelude = AstPrelude.placeholder(resource_location)

current_prelude[resource_location] = prelude
pack_prelude[current_pack] = prelude
prelude_imports.append(prelude)

return prelude_imports
Expand Down
10 changes: 10 additions & 0 deletions examples/bolt_lectern_prelude/beet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require:
- bolt
pipeline:
- lectern
- mecha
meta:
lectern:
load: "source.txt"
bolt:
prelude: "demo:prelude"
12 changes: 12 additions & 0 deletions examples/bolt_lectern_prelude/source.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@module demo:prelude
stuff = 123

@function demo:foo
say stuff

@overlay dummy
@module demo:prelude
stuff = 456

@function demo:foo
say stuff
36 changes: 36 additions & 0 deletions tests/snapshots/examples__build_bolt_lectern_prelude__0.pack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Lectern snapshot

## Data pack

`@data_pack pack.mcmeta`

```json
{
"pack": {
"pack_format": 18,
"description": ""
}
}
```

### demo

`@function demo:foo`

```mcfunction
say 123
```

## Overlay `dummy`

`@overlay dummy`

### demo

`@function demo:foo`

```mcfunction
say 456
```

`@endoverlay`

0 comments on commit 5f858e5

Please sign in to comment.