Factor out jinja2 template to be generic
This lets us reuse it for many more config setups to avoid duplication.
Change-Id: I9e77e55d460edb3f8037ce164818e45b6ab7a56e
diff --git a/tools/build_rules/template.bzl b/tools/build_rules/template.bzl
new file mode 100644
index 0000000..d4807e8
--- /dev/null
+++ b/tools/build_rules/template.bzl
@@ -0,0 +1,33 @@
+def _jinja2_template_impl(ctx):
+ out = ctx.actions.declare_file(ctx.attr.name)
+
+ ctx.actions.run_shell(
+ inputs = ctx.files.src,
+ tools = [ctx.executable._jinja2],
+ progress_message = "Generating " + out.short_path,
+ outputs = [out],
+ command = ctx.executable._jinja2.path + " " + ctx.files.src[0].path + " '" + str(ctx.attr.parameters) + "' " + out.path,
+ )
+
+ return [DefaultInfo(files = depset([out])), OutputGroupInfo(out = depset([out]))]
+
+jinja2_template = rule(
+ attrs = {
+ "src": attr.label(
+ mandatory = True,
+ allow_single_file = True,
+ doc = """The jinja2 template file to expand.""",
+ ),
+ "parameters": attr.string_dict(
+ mandatory = True,
+ doc = """The parameters to supply to Jinja2.""",
+ ),
+ "_jinja2": attr.label(
+ default = "//tools/build_rules:jinja2_generator",
+ cfg = "host",
+ executable = True,
+ ),
+ },
+ implementation = _jinja2_template_impl,
+ doc = """Expands a jinja2 template given parameters.""",
+)