Skip to content

Commit

Permalink
fix: make it possible to use text components and override the descrip…
Browse files Browse the repository at this point in the history
…tion in plugins
  • Loading branch information
vberlier committed Feb 13, 2021
1 parent c5f9d96 commit ad972d0
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 17 deletions.
16 changes: 14 additions & 2 deletions beet/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
"SENTINEL_OBJ",
"dump_json",
"extra_field",
"intersperse",
]


import json
import os
from dataclasses import field
from typing import Any, Dict, Union
from typing import Any, Dict, Iterable, Iterator, List, TypeVar, Union

T = TypeVar("T")

JsonDict = Dict[str, Any]
FileSystemPath = Union[str, os.PathLike]
FileSystemPath = Union[str, "os.PathLike[str]"]
TextComponent = Union[str, List[Any], JsonDict]


class Sentinel:
Expand All @@ -31,3 +35,11 @@ def dump_json(value: Any) -> str:

def extra_field(**kwargs: Any) -> Any:
return field(repr=False, hash=False, compare=False, **kwargs)


def intersperse(iterable: Iterable[T], delimitter: T) -> Iterator[T]:
it = iter(iterable)
yield next(it)
for x in it:
yield delimitter
yield x
4 changes: 2 additions & 2 deletions beet/library/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

from beet.core.container import Container, ContainerProxy, MatchMixin, MergeMixin, Pin
from beet.core.file import File, FileOrigin, JsonFile, PngFile
from beet.core.utils import FileSystemPath, JsonDict, extra_field
from beet.core.utils import FileSystemPath, JsonDict, TextComponent, extra_field

from .utils import list_files

Expand Down Expand Up @@ -286,7 +286,7 @@ class Pack(MatchMixin, MergeMixin, Container[str, NamespaceType]):
mcmeta = PackPin[JsonFile]("pack.mcmeta", default_factory=lambda: JsonFile({}))
image = PackPin[Optional[PngFile]]("pack.png", default=None)

description = McmetaPin[str]("description", default="")
description = McmetaPin[TextComponent]("description", default="")
pack_format = McmetaPin[int]("pack_format", default=0)

namespace_type: ClassVar[Type[NamespaceType]]
Expand Down
6 changes: 3 additions & 3 deletions beet/toolchain/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from pydantic import BaseModel, Field, ValidationError

from beet.core.utils import FileSystemPath, JsonDict
from beet.core.utils import FileSystemPath, JsonDict, TextComponent


class InvalidProjectConfig(Exception):
Expand All @@ -26,7 +26,7 @@ class PackConfig(BaseModel):
"""Data pack and resource pack configuration."""

name: str = ""
description: str = ""
description: TextComponent = ""
pack_format: int = 0
zipped: Optional[bool] = None

Expand Down Expand Up @@ -55,7 +55,7 @@ class ProjectConfig(BaseModel):
"""Beet project configuration."""

name: str = ""
description: str = ""
description: TextComponent = ""
author: str = ""
version: str = ""

Expand Down
4 changes: 2 additions & 2 deletions beet/toolchain/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from beet.core.cache import MultiCache
from beet.core.container import Container
from beet.core.utils import JsonDict, extra_field
from beet.core.utils import JsonDict, TextComponent, extra_field
from beet.library.data_pack import DataPack
from beet.library.resource_pack import ResourcePack

Expand Down Expand Up @@ -48,7 +48,7 @@ class Context:
"""The build context."""

project_name: str
project_description: str
project_description: TextComponent
project_author: str
project_version: str

Expand Down
21 changes: 13 additions & 8 deletions beet/toolchain/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import Iterable, Iterator, List, Optional, Sequence, Union

from beet.core.cache import MultiCache
from beet.core.utils import FileSystemPath, JsonDict
from beet.core.utils import FileSystemPath, JsonDict, intersperse
from beet.core.watch import DirectoryWatcher, FileChanges

from .config import (
Expand Down Expand Up @@ -273,31 +273,36 @@ def bootstrap(self, ctx: Context):
yield

description_parts = [
ctx.project_description,
ctx.project_description if isinstance(ctx.project_description, str) else "",
ctx.project_author and f"Author: {ctx.project_author}",
ctx.project_version and f"Version: {ctx.project_version}",
]

description = "\n".join(filter(None, description_parts))
if not isinstance(ctx.project_description, str):
description = list(
intersperse(filter(None, [ctx.project_description, description]), "\n")
)

for config, suffix, pack in zip(pack_configs, pack_suffixes, ctx.packs):
default_name = ctx.project_name
if ctx.project_version:
default_name += "_" + ctx.project_version
default_name += suffix

options = config.with_defaults(
config = config.with_defaults(
PackConfig(
name=default_name,
description=description,
description=pack.description or description,
pack_format=pack.pack_format,
zipped=pack.zipped,
)
)

pack.name = options.name
pack.description = options.description
pack.pack_format = options.pack_format
pack.zipped = bool(options.zipped)
pack.name = config.name
pack.description = config.description
pack.pack_format = config.pack_format
pack.zipped = bool(config.zipped)

if pack and ctx.output_directory:
pack.save(ctx.output_directory, overwrite=True)
Expand Down
3 changes: 3 additions & 0 deletions tests/examples/code_description/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"pipeline": ["demo"]
}
10 changes: 10 additions & 0 deletions tests/examples/code_description/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from beet import Context


def beet_default(ctx: Context):
ctx.project_name = "something_else"
ctx.project_description = {"text": "bold description", "bold": True}
ctx.project_author = "Fizzy"
ctx.project_version = "1.2.3"

ctx.data.description = ["override for ", {"text": "data pack", "color": "red"}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"pack": {
"pack_format": 6,
"description": [
"override for ",
{
"text": "data pack",
"color": "red"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"pack": {
"pack_format": 6,
"description": [
{
"text": "bold description",
"bold": true
},
"\n",
"Author: Fizzy\nVersion: 1.2.3"
]
}
}

0 comments on commit ad972d0

Please sign in to comment.