From 860fd61bde2a8eefab4a724ff80a96c918e172ce Mon Sep 17 00:00:00 2001 From: BPR02 Date: Mon, 25 Nov 2024 02:46:59 -0800 Subject: [PATCH 1/3] fix: populate context from config - populate ctx.data and ctx.assets with the following info from the config: - name - description - pack_format - supported_formats --- beet/toolchain/project.py | 20 +++++++++++++++++++ tests/ctx_config_examples/mcmeta/beet.json | 18 +++++++++++++++++ tests/ctx_config_examples/mcmeta/mcmeta.py | 9 +++++++++ tests/ctx_config_examples/pack_info/beet.json | 18 +++++++++++++++++ .../pack_info/pack_info.py | 15 ++++++++++++++ .../project_info/beet.json | 9 +++++++++ .../project_info/project_info.py | 13 ++++++++++++ .../ctx_config__examples_mcmeta__0.txt | 2 ++ .../ctx_config__examples_pack_info__0.txt | 8 ++++++++ .../ctx_config__examples_project_info__0.txt | 6 ++++++ tests/test_ctx_config.py | 12 +++++++++++ 11 files changed, 130 insertions(+) create mode 100644 tests/ctx_config_examples/mcmeta/beet.json create mode 100644 tests/ctx_config_examples/mcmeta/mcmeta.py create mode 100644 tests/ctx_config_examples/pack_info/beet.json create mode 100644 tests/ctx_config_examples/pack_info/pack_info.py create mode 100644 tests/ctx_config_examples/project_info/beet.json create mode 100644 tests/ctx_config_examples/project_info/project_info.py create mode 100644 tests/snapshots/ctx_config__examples_mcmeta__0.txt create mode 100644 tests/snapshots/ctx_config__examples_pack_info__0.txt create mode 100644 tests/snapshots/ctx_config__examples_project_info__0.txt create mode 100644 tests/test_ctx_config.py diff --git a/beet/toolchain/project.py b/beet/toolchain/project.py index 3f5fee5f..96d9dc2c 100644 --- a/beet/toolchain/project.py +++ b/beet/toolchain/project.py @@ -274,6 +274,26 @@ def build(self) -> Iterator[Context]: whitelist=self.config.whitelist, ) + # populate ctx.pack with config info + if self.config.data_pack.pack_format: + ctx.data.pack_format = self.config.data_pack.pack_format + if self.config.data_pack.name: + ctx.data.name = self.config.data_pack.name + if self.config.data_pack.description: + ctx.data.description = self.config.data_pack.description + if self.config.data_pack.supported_formats: + ctx.data.supported_formats = self.config.data_pack.supported_formats + if self.config.resource_pack.pack_format: + ctx.assets.pack_format = self.config.resource_pack.pack_format + if self.config.resource_pack.name: + ctx.assets.name = self.config.resource_pack.name + if self.config.resource_pack.description: + ctx.assets.description = self.config.resource_pack.description + if self.config.resource_pack.supported_formats: + ctx.assets.supported_formats = ( + self.config.resource_pack.supported_formats + ) + plugins: List[PluginSpec] = [self.bootstrap] plugins.extend( ( diff --git a/tests/ctx_config_examples/mcmeta/beet.json b/tests/ctx_config_examples/mcmeta/beet.json new file mode 100644 index 00000000..25eb4acd --- /dev/null +++ b/tests/ctx_config_examples/mcmeta/beet.json @@ -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"] +} diff --git a/tests/ctx_config_examples/mcmeta/mcmeta.py b/tests/ctx_config_examples/mcmeta/mcmeta.py new file mode 100644 index 00000000..9d1e9a41 --- /dev/null +++ b/tests/ctx_config_examples/mcmeta/mcmeta.py @@ -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" diff --git a/tests/ctx_config_examples/pack_info/beet.json b/tests/ctx_config_examples/pack_info/beet.json new file mode 100644 index 00000000..dafa94b9 --- /dev/null +++ b/tests/ctx_config_examples/pack_info/beet.json @@ -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"] +} diff --git a/tests/ctx_config_examples/pack_info/pack_info.py b/tests/ctx_config_examples/pack_info/pack_info.py new file mode 100644 index 00000000..7c981f53 --- /dev/null +++ b/tests/ctx_config_examples/pack_info/pack_info.py @@ -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" diff --git a/tests/ctx_config_examples/project_info/beet.json b/tests/ctx_config_examples/project_info/beet.json new file mode 100644 index 00000000..c8c68b0c --- /dev/null +++ b/tests/ctx_config_examples/project_info/beet.json @@ -0,0 +1,9 @@ +{ + "author": "somebody", + "version": "1.2.3", + "name": "BEET", + "id": "beet", + "description": "a beet project", + "minecraft": "1.19", + "pipeline": ["project_info"] +} diff --git a/tests/ctx_config_examples/project_info/project_info.py b/tests/ctx_config_examples/project_info/project_info.py new file mode 100644 index 00000000..fc402bb6 --- /dev/null +++ b/tests/ctx_config_examples/project_info/project_info.py @@ -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" diff --git a/tests/snapshots/ctx_config__examples_mcmeta__0.txt b/tests/snapshots/ctx_config__examples_mcmeta__0.txt new file mode 100644 index 00000000..86767620 --- /dev/null +++ b/tests/snapshots/ctx_config__examples_mcmeta__0.txt @@ -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}}} diff --git a/tests/snapshots/ctx_config__examples_pack_info__0.txt b/tests/snapshots/ctx_config__examples_pack_info__0.txt new file mode 100644 index 00000000..f199c225 --- /dev/null +++ b/tests/snapshots/ctx_config__examples_pack_info__0.txt @@ -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} diff --git a/tests/snapshots/ctx_config__examples_project_info__0.txt b/tests/snapshots/ctx_config__examples_project_info__0.txt new file mode 100644 index 00000000..9f0f6f7c --- /dev/null +++ b/tests/snapshots/ctx_config__examples_project_info__0.txt @@ -0,0 +1,6 @@ +1.19 +somebody +a beet project +beet +BEET +1.2.3 diff --git a/tests/test_ctx_config.py b/tests/test_ctx_config.py new file mode 100644 index 00000000..4e148d4b --- /dev/null +++ b/tests/test_ctx_config.py @@ -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"]}' From 80ddb492630ae445e28c849f4954467e5241276b Mon Sep 17 00:00:00 2001 From: Misode Date: Tue, 21 Jan 2025 23:25:12 +0100 Subject: [PATCH 2/3] move copying logic to bootstrap --- beet/toolchain/project.py | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/beet/toolchain/project.py b/beet/toolchain/project.py index 96d9dc2c..3695f788 100644 --- a/beet/toolchain/project.py +++ b/beet/toolchain/project.py @@ -274,26 +274,6 @@ def build(self) -> Iterator[Context]: whitelist=self.config.whitelist, ) - # populate ctx.pack with config info - if self.config.data_pack.pack_format: - ctx.data.pack_format = self.config.data_pack.pack_format - if self.config.data_pack.name: - ctx.data.name = self.config.data_pack.name - if self.config.data_pack.description: - ctx.data.description = self.config.data_pack.description - if self.config.data_pack.supported_formats: - ctx.data.supported_formats = self.config.data_pack.supported_formats - if self.config.resource_pack.pack_format: - ctx.assets.pack_format = self.config.resource_pack.pack_format - if self.config.resource_pack.name: - ctx.assets.name = self.config.resource_pack.name - if self.config.resource_pack.description: - ctx.assets.description = self.config.resource_pack.description - if self.config.resource_pack.supported_formats: - ctx.assets.supported_formats = ( - self.config.resource_pack.supported_formats - ) - plugins: List[PluginSpec] = [self.bootstrap] plugins.extend( ( @@ -330,6 +310,16 @@ def bootstrap(self, ctx: Context): pack_configs = [self.config.resource_pack, self.config.data_pack] pack_suffixes = ["_resource_pack", "_data_pack"] + for config, pack in zip(pack_configs, ctx.packs): + if config.name: + pack.name = config.name + if config.description: + pack.description = config.description + if config.pack_format: + pack.pack_format = config.pack_format + if config.supported_formats: + pack.supported_formats = config.supported_formats + ctx.require( load( resource_pack=self.config.resource_pack.load, From 7c43033d6e198f78ca1b7e47e00cf31502dc2e5c Mon Sep 17 00:00:00 2001 From: Misode Date: Wed, 22 Jan 2025 01:07:55 +0100 Subject: [PATCH 3/3] move all pack meta population before loading and plugins --- beet/toolchain/project.py | 50 ++++++++----------- .../pack.mcmeta | 14 +++--- .../pack.mcmeta | 6 +-- 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/beet/toolchain/project.py b/beet/toolchain/project.py index 3695f788..fd5cc766 100644 --- a/beet/toolchain/project.py +++ b/beet/toolchain/project.py @@ -307,36 +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"] - - for config, pack in zip(pack_configs, ctx.packs): - if config.name: - pack.name = config.name - if config.description: - pack.description = config.description - if config.pack_format: - pack.pack_format = config.pack_format - if config.supported_formats: - pack.supported_formats = config.supported_formats - - 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}", @@ -349,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: @@ -388,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: diff --git a/tests/snapshots/examples__build_load_overlay__0.data_pack/pack.mcmeta b/tests/snapshots/examples__build_load_overlay__0.data_pack/pack.mcmeta index 1aa5c97c..3d9aea9b 100644 --- a/tests/snapshots/examples__build_load_overlay__0.data_pack/pack.mcmeta +++ b/tests/snapshots/examples__build_load_overlay__0.data_pack/pack.mcmeta @@ -10,6 +10,13 @@ }, "overlays": { "entries": [ + { + "formats": { + "min_inclusive": 18, + "max_inclusive": 19 + }, + "directory": "creeper" + }, { "formats": { "min_inclusive": 17, @@ -27,13 +34,6 @@ { "formats": 19, "directory": "overlay_missing" - }, - { - "formats": { - "min_inclusive": 18, - "max_inclusive": 19 - }, - "directory": "creeper" } ] } diff --git a/tests/snapshots/examples__build_load_pack_filter__0.data_pack/pack.mcmeta b/tests/snapshots/examples__build_load_pack_filter__0.data_pack/pack.mcmeta index c5a1babe..4a80de39 100644 --- a/tests/snapshots/examples__build_load_pack_filter__0.data_pack/pack.mcmeta +++ b/tests/snapshots/examples__build_load_pack_filter__0.data_pack/pack.mcmeta @@ -5,15 +5,15 @@ }, "filter": { "block": [ + { + "namespace": "minecraft" + }, { "namespace": "foo", "path": "bar" }, { "namespace": "thing" - }, - { - "namespace": "minecraft" } ] }