Skip to content

Commit

Permalink
fix: share walk_location_hierarchy with mecha
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Jan 17, 2024
1 parent 430d488 commit 7d33f65
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 21 deletions.
19 changes: 3 additions & 16 deletions bolt/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ def from_module_import(self, resource_location: str, *args: str) -> Any:
def get_nested_location(self) -> str:
"""Return the resource location associated with the current level of nesting."""
root, relative_path = NestedLocationResolver.concat_nested_path(
self.walk_location_hierarchy()
NestedLocationResolver.walk_location_hierarchy(
self.spec, reversed(self.nesting)
)
)

if not root:
Expand All @@ -195,21 +197,6 @@ def get_nested_location(self) -> str:
)
return f"{namespace}:{resolved}"

def walk_location_hierarchy(self) -> Iterator[AstResourceLocation]:
"""Yield resource locations contributing to the current nested context."""
for identifier, arguments in reversed(self.nesting):
prototype = self.spec.prototypes[identifier]

for i, argument_node in enumerate(arguments):
node = self.spec.tree.get(prototype.get_argument(i).scope)
if (
node
and node.parser == "minecraft:function"
and isinstance(argument_node, AstResourceLocation)
):
yield argument_node
break

def finalize(self, ctx: Context):
"""Plugin that runs at the end of the build."""
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
x = ""

function ~/{x}
function ~/thing{x}
function ~/../thing{x}

function ref:bar:
function ~/{x}
function ~/thing{x}
function ~/../thing{x}

append function ~/thing{x}:
function #~/aaa{x}

function ./wat:
function ~/{x}
function ~/thing{x}
function ~/../thing{x}

append function ~/thing{x}:
function #~/bbb{x}

function ~/this{x}:
function ~/{x}
function ~/thing{x}
function ~/../thing{x}

append function ~/thing{x}:
function #~/ccc{x}

function ~/../upthis{x}:
function ~/{x}
function ~/thing{x}
function ~/../thing{x}

append function ~/thing{x}:
function #~/ddd{x}

execute if function ~/condition{x} run function ~/action{x}
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ include = ["bolt/py.typed"]
[tool.poetry.dependencies]
python = "^3.10"
beet = ">=0.100.0"
mecha = ">=0.86.0"
mecha = ">=0.86.5"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.3"
Expand Down
67 changes: 67 additions & 0 deletions tests/snapshots/examples__build_bolt_nested_location__0.pack.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,70 @@ schedule function demo:foo/thing1/blob 2s append
```mcfunction
execute if predicate demo:welp run say yes
```

### ref

`@function ref:foo`

```mcfunction
function ref:foo
function ref:foo/thing
function ref:thing
execute if function ref:foo/condition run function ref:foo/action
```

`@function ref:bar/thing`

```mcfunction
function #ref:bar/thing/aaa
```

`@function ref:wat/thing`

```mcfunction
function #ref:wat/thing/bbb
```

`@function ref:foo/this/thing`

```mcfunction
function #ref:foo/this/thing/ccc
```

`@function ref:upthis/thing`

```mcfunction
function #ref:upthis/thing/ddd
```

`@function ref:bar`

```mcfunction
function ref:bar
function ref:bar/thing
function ref:thing
```

`@function ref:wat`

```mcfunction
function ref:wat
function ref:wat/thing
function ref:thing
```

`@function ref:foo/this`

```mcfunction
function ref:foo/this
function ref:foo/this/thing
function ref:foo/thing
```

`@function ref:upthis`

```mcfunction
function ref:upthis
function ref:upthis/thing
function ref:thing
```

0 comments on commit 7d33f65

Please sign in to comment.