From 3d8f4f4648a3dc063bbffcfd1392dfdc7c24ea32 Mon Sep 17 00:00:00 2001 From: Per Unneberg Date: Thu, 7 Mar 2024 10:35:08 +0100 Subject: [PATCH] Add command for individual templates --- src/nbis/commands/add.py | 40 ++++++++++++++++++++++++++++++++++++---- src/nbis/templates.py | 12 ++++++++++++ tests/test_add.py | 9 +++++++++ tests/test_commands.py | 5 ++--- 4 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 tests/test_add.py diff --git a/src/nbis/commands/add.py b/src/nbis/commands/add.py index a185d89..d410c5f 100644 --- a/src/nbis/commands/add.py +++ b/src/nbis/commands/add.py @@ -23,7 +23,7 @@ __shortname__ = __name__.rsplit(".", maxsplit=1)[-1] -def _add_doc_and_assets(outdir, ext, template="running-slides", **kw): +def _add_doc_and_assets(outdir, ext, template_name="running-slides", **kw): assets = outdir / kw["assets"] path = outdir / f"index.{ext}" www = assets / "www" @@ -35,7 +35,7 @@ def _add_doc_and_assets(outdir, ext, template="running-slides", **kw): www.mkdir(parents=True) css.mkdir() logos.mkdir() - templates.add_template(path, f"docs/{template}.{ext}.j2", **kw) + templates.add_template(path, f"docs/{template_name}.{ext}.j2", **kw) templates.add_template(www / "title-slide.html", "docs/assets/www/title-slide.html") templates.add_template(www / "tikzfig.tex", "docs/assets/www/tikzfig.tex") templates.add_template( @@ -54,7 +54,7 @@ def main(): @click.option( "slide_type", "--type", type=click.Choice(["quarto", "rmarkdown"]), default="quarto" ) -@click.option("--show", is_flag=True, help="show template") +@click.option("--show", is_flag=True, help="show rendered template") @click.option("--title", help="title", default="title") @click.option("--subtitle", help="subtitle", default="subtitle") @click.option("--author", help="author") @@ -100,7 +100,7 @@ def running_slides(env, slide_type, show, **kw): type=click.Choice(["quarto", "markdown"]), default="quarto", ) -@click.option("--show", is_flag=True, help="show template") +@click.option("--show", is_flag=True, help="show rendered template") @click.option("--title", help="title", default="title") @click.option("--subtitle", help="subtitle", default="subtitle") @click.option("--author", help="author") @@ -151,6 +151,38 @@ def diary(env, doc_type, show, **kw): _add_doc_and_assets(outdir, ext, template="diary", **kw) +@main.command() +@click.option( + "template_name", + "--template", + "-t", + type=click.Choice(templates.INDIVIDUAL_TEMPLATES.keys()), +) +@click.option("--show", is_flag=True, help="show rendered template") +@click.option( + "--name", + help="output template to named directory", + type=click.Path(exists=False), +) +@pass_environment +def template(env, template_name, show, **kw): + """Add individual template""" + logger.info("Adding template %s", template_name) + if template_name is None: + click.echo("No template specified; exiting.") + return + if kw["name"] is None: + kw["name"] = env.home + outdir = pathlib.Path(kw["name"]) + tpl = templates.INDIVIDUAL_TEMPLATES[template_name] + path = outdir / pathlib.Path(tpl).with_suffix("").name + if show: + t = templates.render_template(tpl, **kw) + click.echo(t) + else: + templates.add_template(path, tpl, **kw) + + @main.command(name="command-group") @click.argument("command_group") @click.option("--path", help="output template to path", type=click.Path(exists=False)) diff --git a/src/nbis/templates.py b/src/nbis/templates.py index c4e7ef2..8e2d906 100644 --- a/src/nbis/templates.py +++ b/src/nbis/templates.py @@ -13,6 +13,18 @@ file_loader = FileSystemLoader(template_path) env = Environment(loader=file_loader) +INDIVIDUAL_TEMPLATES = { + "pyproject.toml": "pyproject.toml.j2", + "gitignore": ".gitignore.j2", + "prettierignore": ".prettierignore.j2", + "markdownlint": ".markdownlint.yaml.j2", + "prettierrc": ".prettierrc.yml.j2", + "editorconfig": ".editorconfig.j2", + "readme": "README.md.j2", + "pre-commit-config": ".pre-commit-config.yaml.j2", + "quarto": "docs/_quarto.yml.j2", +} + def add_template(filename, template, **kwargs): """Generic function to render template to filename""" diff --git a/tests/test_add.py b/tests/test_add.py new file mode 100644 index 0000000..38f4de5 --- /dev/null +++ b/tests/test_add.py @@ -0,0 +1,9 @@ +"""Test commands.""" + +from nbis.cli import cli + + +def test_add_template(runner): + """Test adding individual template""" + result = runner.invoke(cli, ["add", "template", "-t", "quarto", "--show"]) + assert not result.exception diff --git a/tests/test_commands.py b/tests/test_commands.py index bda5f22..9521596 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -5,6 +5,5 @@ def test_init_diary(runner): """Test initialization of diary.""" - result = runner.invoke(cli, ["diary"]) - print(result.stdout) - print(result) + result = runner.invoke(cli, ["add", "diary", "--show"]) + assert not result.exception