Browse Source

Merge pull request #1 from atopile/mawildoer/new-templating

Jinja templating
mawildoer 4 months ago
parent
commit
318a80433f

+ 3 - 0
.vscode/settings.json.j2

@@ -0,0 +1,3 @@
+{
+    "python.defaultInterpreterPath": "{{python_path}}"
+}

+ 0 - 6
ato.yaml

@@ -1,6 +0,0 @@
-ato-version: ^0.1.2
-builds:
-  default:
-    entry: elec/src/template123.ato:template123
-dependencies:
-- generics

+ 6 - 0
ato.yaml.j2

@@ -0,0 +1,6 @@
+ato-version: ^0.1.8
+builds:
+  default:
+    entry: elec/src/{{caseconverter.kebabcase(name)}}.ato:{{caseconverter.pascalcase(name)}}
+dependencies:
+- generics

+ 65 - 0
configure.py

@@ -0,0 +1,65 @@
+import sys
+from pathlib import Path
+
+import caseconverter
+import click
+from jinja2 import Environment, FileSystemLoader
+
+
+MAGIC_WORD = "please"
+
+
+@click.command()
+@click.argument("name")
+@click.argument("magic_word")
+@click.option("--debug", is_flag=True, default=False, help="Enable debug mode.")
+def main(name: str, magic_word: str, debug: bool):
+    """
+    This is a script to configure the project.
+
+    It's intended to be called by the `ato create` command.
+    If you're calling it manually, there's likely something
+    wrong and you should probably stop.
+
+    This script is intended to be run in the same environment
+    as the ato CLI, so it's expecting to have access to the
+    same packages and tools; Jinja, etc...
+    """
+    if magic_word != MAGIC_WORD:
+        raise click.BadArgumentUsage(
+            "This script is intended to be called by the ato CLI."
+        )
+
+    # Common variables
+    extended_globals = {
+        "name": name,
+        "caseconverter": caseconverter,
+        "cwd": Path.cwd(),
+    }
+
+    # Load templates
+    env = Environment(loader=FileSystemLoader("."))
+
+    for template_path in Path(".").glob("**/*.j2"):
+        # Figure out the target path and variables and what not
+        target_path = template_path.parent / template_path.name.replace(
+            ".j2", ""
+        ).replace("__name__", caseconverter.kebabcase(name))
+
+        extended_globals["rel_path"] = target_path
+        extended_globals["python_path"] = sys.executable
+
+        template = env.get_template(str(template_path), globals=extended_globals)
+
+        # Make the noise!
+        with target_path.open("w") as f:
+            for chunk in template.generate():
+                f.write(chunk)
+
+        # Remove the template
+        if not debug:
+            template_path.unlink()
+
+
+if __name__ == "__main__":
+    main()  # pylint: disable=no-value-for-parameter

+ 3 - 3
elec/layout/default/template123.kicad_pcb → elec/layout/default/__name__.kicad_pcb.j2

@@ -1725,13 +1725,13 @@
     (stroke (width 0.2) (type default)) (fill none) (layer "Edge.Cuts") (tstamp e1dec10d-8b7a-4e73-adf9-1aab96be1971))
   (gr_line (start 225.62 117.79) (end 257.62 117.79)
     (stroke (width 0.1) (type solid)) (layer "Edge.Cuts") (tstamp e1f36007-0f9d-4dc5-ae43-6b056652526f))
-  (gr_text "{{GITHASH}} " (at 152.4 139.7) (layer "F.SilkS") (tstamp 46268c7f-4a2b-440e-af15-46add3884cdf)
+  (gr_text "{{"{{GITHASH}}"}}" (at 152.4 139.7) (layer "F.SilkS") (tstamp 46268c7f-4a2b-440e-af15-46add3884cdf)
     (effects (font (size 2 2) (thickness 0.1)) (justify left bottom))
   )
-  (gr_text "{{GITHASH}} " (at 62.23 146.05) (layer "F.SilkS") (tstamp 47ada779-5919-4cca-9f25-e816e5b53339)
+  (gr_text "{{"{{GITHASH}}"}}" (at 62.23 146.05) (layer "F.SilkS") (tstamp 47ada779-5919-4cca-9f25-e816e5b53339)
     (effects (font (size 2 2) (thickness 0.1)) (justify left bottom))
   )
-  (gr_text "{{GITHASH}} " (at 232.508427 115.791573) (layer "F.SilkS") (tstamp d894e23f-c5ed-4336-947e-ac38e533f04c)
+  (gr_text "{{"{{GITHASH}}"}}" (at 232.508427 115.791573) (layer "F.SilkS") (tstamp d894e23f-c5ed-4336-947e-ac38e533f04c)
     (effects (font (size 2 2) (thickness 0.1)) (justify left bottom))
   )
 

+ 1 - 1
elec/layout/default/template123.kicad_pro → elec/layout/default/__name__.kicad_pro.j2

@@ -215,7 +215,7 @@
     "last_paths": {
       "gencad": "",
       "idf": "",
-      "netlist": "../../../build/default/<template-module-name>.net",
+      "netlist": "../../../build/default/{{caseconverter.kebabcase(name)}}.net",
       "specctra_dsn": "",
       "step": "",
       "vrml": ""

+ 0 - 0
elec/layout/default/template123.kicad_sch → elec/layout/default/__name__.kicad_sch.j2


+ 2 - 0
elec/src/__name__.ato.j2

@@ -0,0 +1,2 @@
+module {{caseconverter.pascalcase(name)}}:
+    signal gnd

+ 0 - 2
elec/src/template123.ato

@@ -1,2 +0,0 @@
-module template123:
-    signal gnd