aos: Clean up the jinja2_template rule a little bit

I made some enhancements for custom filters and generated
replacements. It was easier to implement those after cleaning up the
code a bit first. This patch does that.

Change-Id: Idbdfae27cf1a3bec598eeaeabf7e7b9e36341242
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/tools/build_rules/template.bzl b/tools/build_rules/template.bzl
index d3547d8..28d3c82 100644
--- a/tools/build_rules/template.bzl
+++ b/tools/build_rules/template.bzl
@@ -3,13 +3,28 @@
     parameters = dict(ctx.attr.parameters)
     parameters.update(ctx.attr.list_parameters)
 
-    ctx.actions.run_shell(
+    # For now we don't really want the user to worry about which configuration
+    # to pull the file from. We don't yet have a use case for pulling the same
+    # file from multiple configurations. We point Jinja at all the configuration
+    # roots.
+    include_dirs = depset([
+        file.root.path or "."
+        for file in ctx.files.includes
+    ]).to_list()
+
+    args = ctx.actions.args()
+    args.add(ctx.file.src)
+    args.add(json.encode(parameters))
+    args.add(out)
+    args.add_all(include_dirs, before_each = "--include_dir")
+
+    ctx.actions.run(
         inputs = ctx.files.src + ctx.files.includes,
         tools = [ctx.executable._jinja2],
         progress_message = "Generating " + out.short_path,
         outputs = [out],
-        # TODO(james): Is the genfiles_dir the correct thing?
-        command = ctx.executable._jinja2.path + " " + ctx.files.src[0].path + " '" + str(parameters) + "' " + out.path + " " + ctx.genfiles_dir.path,
+        executable = ctx.executable._jinja2,
+        arguments = [args],
     )
 
     return [DefaultInfo(files = depset([out])), OutputGroupInfo(out = depset([out]))]
@@ -54,4 +69,11 @@
     # differ from `out`, name the rule as the `name` plus a suffix
     rule_name = name + "_rule"
 
-    jinja2_template_rule(name = rule_name, out = name, src = src, parameters = parameters, list_parameters = list_parameters, **kwargs)
+    jinja2_template_rule(
+        name = rule_name,
+        out = name,
+        src = src,
+        parameters = parameters,
+        list_parameters = list_parameters,
+        **kwargs
+    )