Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: populate context from config #452

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions beet/toolchain/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,26 +307,6 @@ def bootstrap(self, ctx: Context):
for plugin in plugins:
ctx.require(plugin)

pack_configs = [self.config.resource_pack, self.config.data_pack]
pack_suffixes = ["_resource_pack", "_data_pack"]

ctx.require(
load(
resource_pack=self.config.resource_pack.load,
data_pack=self.config.data_pack.load,
)
)

ctx.require(
render(
resource_pack=self.config.resource_pack.render,
data_pack=self.config.data_pack.render,
)
)

with log_time("Run pipeline."):
yield

description_parts = [
ctx.project_description if isinstance(ctx.project_description, str) else "",
ctx.project_author and f"Author: {ctx.project_author}",
Expand All @@ -339,6 +319,9 @@ def bootstrap(self, ctx: Context):
intersperse(filter(None, [ctx.project_description, description]), "\n")
)

pack_configs = [self.config.resource_pack, self.config.data_pack]
pack_suffixes = ["_resource_pack", "_data_pack"]

for config, suffix, pack in zip(pack_configs, pack_suffixes, ctx.packs):
default_name = ctx.project_id
if ctx.project_version:
Expand Down Expand Up @@ -378,6 +361,23 @@ def bootstrap(self, ctx: Context):
pack.compression = config.compression
pack.compression_level = config.compression_level

ctx.require(
load(
resource_pack=self.config.resource_pack.load,
data_pack=self.config.data_pack.load,
)
)

ctx.require(
render(
resource_pack=self.config.resource_pack.render,
data_pack=self.config.data_pack.render,
)
)

with log_time("Run pipeline."):
yield

def __call__(self, ctx: Context):
"""The builder instance is itself a plugin used for merging subpipelines."""
with self.build() as child_ctx:
Expand Down
18 changes: 18 additions & 0 deletions tests/ctx_config_examples/mcmeta/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"data_pack": {
"name": "My Data Pack",
"description": "a data pack",
"pack_format": 6,
"supported_formats": [0,6]
},
"resource_pack": {
"name": "My Resource Pack",
"description": "a resource pack",
"pack_format": 10,
"supported_formats": {
"min_inclusive": 10,
"max_inclusive": 20
}
},
"pipeline": ["mcmeta"]
}
9 changes: 9 additions & 0 deletions tests/ctx_config_examples/mcmeta/mcmeta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from beet import Context


def beet_default(ctx: Context):
all = [
f"{ctx.data.mcmeta.data}",
f"{ctx.assets.mcmeta.data}",
]
ctx.meta["pytest"] = "\n".join(all) + "\n"
18 changes: 18 additions & 0 deletions tests/ctx_config_examples/pack_info/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"data_pack": {
"name": "My Data Pack",
"description": "a data pack",
"pack_format": 6,
"supported_formats": [0,6]
},
"resource_pack": {
"name": "My Resource Pack",
"description": "a resource pack",
"pack_format": 10,
"supported_formats": {
"min_inclusive": 10,
"max_inclusive": 20
}
},
"pipeline": ["pack_info"]
}
15 changes: 15 additions & 0 deletions tests/ctx_config_examples/pack_info/pack_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from beet import Context


def beet_default(ctx: Context):
all = [
f"{ctx.data.name}",
f"{ctx.data.description}",
f"{ctx.data.pack_format}",
f"{ctx.data.supported_formats}",
f"{ctx.assets.name}",
f"{ctx.assets.description}",
f"{ctx.assets.pack_format}",
f"{ctx.assets.supported_formats}",
]
ctx.meta["pytest"] = "\n".join(all) + "\n"
9 changes: 9 additions & 0 deletions tests/ctx_config_examples/project_info/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"author": "somebody",
"version": "1.2.3",
"name": "BEET",
"id": "beet",
"description": "a beet project",
"minecraft": "1.19",
"pipeline": ["project_info"]
}
13 changes: 13 additions & 0 deletions tests/ctx_config_examples/project_info/project_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from beet import Context


def beet_default(ctx: Context):
all = [
f"{ctx.minecraft_version}",
f"{ctx.project_author}",
f"{ctx.project_description}",
f"{ctx.project_id}",
f"{ctx.project_name}",
f"{ctx.project_version}",
]
ctx.meta["pytest"] = "\n".join(all) + "\n"
2 changes: 2 additions & 0 deletions tests/snapshots/ctx_config__examples_mcmeta__0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{'pack': {'pack_format': 6, 'description': 'a data pack', 'supported_formats': [0, 6]}}
{'pack': {'pack_format': 10, 'description': 'a resource pack', 'supported_formats': {'min_inclusive': 10, 'max_inclusive': 20}}}
8 changes: 8 additions & 0 deletions tests/snapshots/ctx_config__examples_pack_info__0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
My Data Pack
a data pack
6
[0, 6]
My Resource Pack
a resource pack
10
{'min_inclusive': 10, 'max_inclusive': 20}
6 changes: 6 additions & 0 deletions tests/snapshots/ctx_config__examples_project_info__0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1.19
somebody
a beet project
beet
BEET
1.2.3
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
},
"overlays": {
"entries": [
{
"formats": {
"min_inclusive": 18,
"max_inclusive": 19
},
"directory": "creeper"
},
{
"formats": {
"min_inclusive": 17,
Expand All @@ -27,13 +34,6 @@
{
"formats": 19,
"directory": "overlay_missing"
},
{
"formats": {
"min_inclusive": 18,
"max_inclusive": 19
},
"directory": "creeper"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
},
"filter": {
"block": [
{
"namespace": "minecraft"
},
{
"namespace": "foo",
"path": "bar"
},
{
"namespace": "thing"
},
{
"namespace": "minecraft"
}
]
}
Expand Down
12 changes: 12 additions & 0 deletions tests/test_ctx_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os

import pytest
from pytest_insta import SnapshotFixture

from beet import run_beet


@pytest.mark.parametrize("directory", os.listdir("tests/ctx_config_examples"))
def test_examples(snapshot: SnapshotFixture, directory: str):
with run_beet(directory=f"tests/ctx_config_examples/{directory}") as ctx:
assert snapshot() == f'{ctx.meta["pytest"]}'
BPR02 marked this conversation as resolved.
Show resolved Hide resolved
Loading