blob: 1ad06d8f12c33d9864fa15b9fc65a6d2a439f309 [file] [log] [blame]
Austin Schuh3a2f4a42020-09-16 14:10:39 -07001def _jinja2_template_impl(ctx):
Milind Upadhyay302296f2023-03-23 16:36:51 -07002 out = ctx.outputs.out
3 parameters = dict(ctx.attr.parameters)
4 parameters.update(ctx.attr.list_parameters)
Austin Schuh3a2f4a42020-09-16 14:10:39 -07005
6 ctx.actions.run_shell(
James Kuszmaulfe650202023-02-05 17:31:19 -08007 inputs = ctx.files.src + ctx.files.includes,
Austin Schuh3a2f4a42020-09-16 14:10:39 -07008 tools = [ctx.executable._jinja2],
9 progress_message = "Generating " + out.short_path,
10 outputs = [out],
Milind Upadhyay302296f2023-03-23 16:36:51 -070011 command = ctx.executable._jinja2.path + " " + ctx.files.src[0].path + " '" + str(parameters) + "' " + out.path,
Austin Schuh3a2f4a42020-09-16 14:10:39 -070012 )
13
14 return [DefaultInfo(files = depset([out])), OutputGroupInfo(out = depset([out]))]
15
Milind Upadhyay302296f2023-03-23 16:36:51 -070016jinja2_template_rule = rule(
Austin Schuh3a2f4a42020-09-16 14:10:39 -070017 attrs = {
Milind Upadhyay302296f2023-03-23 16:36:51 -070018 "out": attr.output(
19 mandatory = True,
20 doc = """The file to generate using the template. If using the jinja2_template macro below, this will automatically be populated with the contents of the `name` parameter.""",
21 ),
Austin Schuh3a2f4a42020-09-16 14:10:39 -070022 "src": attr.label(
23 mandatory = True,
24 allow_single_file = True,
25 doc = """The jinja2 template file to expand.""",
26 ),
27 "parameters": attr.string_dict(
Milind Upadhyay302296f2023-03-23 16:36:51 -070028 mandatory = False,
29 default = {},
30 doc = """The string parameters to supply to Jinja2.""",
31 ),
32 "list_parameters": attr.string_list_dict(
33 mandatory = False,
34 default = {},
35 doc = """The string list parameters to supply to Jinja2.""",
Austin Schuh3a2f4a42020-09-16 14:10:39 -070036 ),
James Kuszmaulfe650202023-02-05 17:31:19 -080037 "includes": attr.label_list(
38 allow_files = True,
39 doc = """Files which are included by the template.""",
40 ),
Austin Schuh3a2f4a42020-09-16 14:10:39 -070041 "_jinja2": attr.label(
42 default = "//tools/build_rules:jinja2_generator",
Adam Snaider13d48d92023-08-03 12:20:15 -070043 cfg = "exec",
Austin Schuh3a2f4a42020-09-16 14:10:39 -070044 executable = True,
45 ),
46 },
47 implementation = _jinja2_template_impl,
48 doc = """Expands a jinja2 template given parameters.""",
49)
Milind Upadhyay302296f2023-03-23 16:36:51 -070050
51def jinja2_template(name, src, parameters = {}, list_parameters = {}, **kwargs):
Milind Upadhyay302296f2023-03-23 16:36:51 -070052 # Since the `out` field will be set to `name`, and the name for the rule must
Milind Upadhyay0ff31aa2023-03-28 14:18:41 -070053 # differ from `out`, name the rule as the `name` plus a suffix
54 rule_name = name + "_rule"
Milind Upadhyay302296f2023-03-23 16:36:51 -070055
56 jinja2_template_rule(name = rule_name, out = name, src = src, parameters = parameters, list_parameters = list_parameters, **kwargs)