Skip to content

Commit

Permalink
feat: render pack name and description with jinja
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Jun 10, 2021
1 parent 6b9d6b5 commit 15bf25c
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 2 deletions.
4 changes: 2 additions & 2 deletions beet/toolchain/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ def bootstrap(self, ctx: Context):
)
)

pack.name = config.name.replace("<version>", ctx.project_version)
pack.description = config.description
pack.name = ctx.template.render_string(config.name)
pack.description = ctx.template.render_json(config.description)
pack.pack_format = config.pack_format
pack.zipped = bool(config.zipped)

Expand Down
12 changes: 12 additions & 0 deletions beet/toolchain/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from .pipeline import FormattedPipelineException, PipelineFallthroughException
from .utils import ensure_builtins

T = TypeVar("T")
TextFileType = TypeVar("TextFileType", bound=TextFileBase[Any])


Expand Down Expand Up @@ -147,6 +148,17 @@ def render_file(self, file: TextFileType, **kwargs: Any) -> TextFileType:
file.text = self.render(str(file.source_path), **kwargs)
return file

def render_json(self, data: T, **kwargs: Any) -> T:
"""Render all strings in a json value."""
if isinstance(data, str):
return self.render_string(data, **kwargs) # type: ignore
elif isinstance(data, list):
return [self.render_json(element) for element in data] # type: ignore
elif isinstance(data, dict):
return {key: self.render_json(value) for key, value in data.items()} # type: ignore
else:
return data

@contextmanager
def error_handler(self, message: str):
"""Handle template errors."""
Expand Down
13 changes: 13 additions & 0 deletions examples/load_render_description/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "thing",
"version": "1.2.3",
"description": "Something cool",
"data_pack": {
"description": [
{ "text": "{{ project_name | capitalize }} ", "color": "#9273ff" },
{ "text": "v{{ project_version }}\n", "color": "#66c546" },
{ "text": "{{ project_description }}", "color": "#00aced" }
],
"load": ["src"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say hello
8 changes: 8 additions & 0 deletions examples/load_render_name/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "thing",
"version": "1.2.3",
"data_pack": {
"name": "{{ project_name | capitalize }} {{ project_version }}",
"load": ["src"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"pack": {
"pack_format": 6,
"description": [
{
"text": "Thing ",
"color": "#9273ff"
},
{
"text": "v1.2.3\n",
"color": "#66c546"
},
{
"text": "Something cool",
"color": "#00aced"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 6,
"description": "Something cool\nVersion: 1.2.3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 6,
"description": "Version: 1.2.3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 6,
"description": "Version: 1.2.3"
}
}

0 comments on commit 15bf25c

Please sign in to comment.