aos: Support generated replacements in jinja2_template

I have a rule that generates replacements for a Jinja2 template. I
can't use any of the current mechanisms to do this so I decided to
enhance the rule.

You can now specify a `parameters_file` attribute whose contents will
be added to the available variables inside the template.

This came up when I wanted to copy the spyglass colors into a foxglove
script.

I couldn't figure out a good way to write a build test for this
because the //build_tests package in BRT's vendored copy of AOS
doesn't load properly.

Change-Id: Ib046817e5b698d5358b787e67953503ed2b57f78
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/tools/build_rules/jinja2_generator.py b/tools/build_rules/jinja2_generator.py
index 047a805..7bb9c75 100644
--- a/tools/build_rules/jinja2_generator.py
+++ b/tools/build_rules/jinja2_generator.py
@@ -18,6 +18,13 @@
                         type=Path,
                         help="File to use for template.")
     parser.add_argument(
+        "--replacements_file",
+        type=Path,
+        help=("File containing a dictionary of parameters to replace "
+              "in the template. The behaviour is undefined if keys are "
+              "duplicated between this file and the `replacements` argument."),
+    )
+    parser.add_argument(
         "replacements",
         type=json.loads,
         help="Dictionary of parameters to replace in the template.")
@@ -34,7 +41,12 @@
     env = jinja2.Environment(loader=jinja2.FileSystemLoader(args.include_dir))
     template = env.from_string(args.template.read_text())
 
-    args.output.write_text(template.render(args.replacements))
+    replacements = args.replacements.copy()
+    if args.replacements_file:
+        with args.replacements_file.open() as file:
+            replacements.update(json.load(file))
+
+    args.output.write_text(template.render(replacements))
 
 
 if __name__ == '__main__':
diff --git a/tools/build_rules/template.bzl b/tools/build_rules/template.bzl
index 28d3c82..7174969 100644
--- a/tools/build_rules/template.bzl
+++ b/tools/build_rules/template.bzl
@@ -17,9 +17,11 @@
     args.add(json.encode(parameters))
     args.add(out)
     args.add_all(include_dirs, before_each = "--include_dir")
+    if ctx.file.parameters_file:
+        args.add("--replacements_file", ctx.file.parameters_file)
 
     ctx.actions.run(
-        inputs = ctx.files.src + ctx.files.includes,
+        inputs = ctx.files.src + ctx.files.includes + ctx.files.parameters_file,
         tools = [ctx.executable._jinja2],
         progress_message = "Generating " + out.short_path,
         outputs = [out],
@@ -50,6 +52,10 @@
             default = {},
             doc = """The string list parameters to supply to Jinja2.""",
         ),
+        "parameters_file": attr.label(
+            allow_single_file = True,
+            doc = """A JSON file whose contents are supplied as parameters to Jinja2.""",
+        ),
         "includes": attr.label_list(
             allow_files = True,
             doc = """Files which are included by the template.""",