blob: d3547d8e57b5db76d524f04735265b17c64862a0 [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],
James Kuszmaulba43b062024-01-14 17:29:21 -080011 # TODO(james): Is the genfiles_dir the correct thing?
12 command = ctx.executable._jinja2.path + " " + ctx.files.src[0].path + " '" + str(parameters) + "' " + out.path + " " + ctx.genfiles_dir.path,
Austin Schuh3a2f4a42020-09-16 14:10:39 -070013 )
14
15 return [DefaultInfo(files = depset([out])), OutputGroupInfo(out = depset([out]))]
16
Milind Upadhyay302296f2023-03-23 16:36:51 -070017jinja2_template_rule = rule(
Austin Schuh3a2f4a42020-09-16 14:10:39 -070018 attrs = {
Milind Upadhyay302296f2023-03-23 16:36:51 -070019 "out": attr.output(
20 mandatory = True,
21 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.""",
22 ),
Austin Schuh3a2f4a42020-09-16 14:10:39 -070023 "src": attr.label(
24 mandatory = True,
25 allow_single_file = True,
26 doc = """The jinja2 template file to expand.""",
27 ),
28 "parameters": attr.string_dict(
Milind Upadhyay302296f2023-03-23 16:36:51 -070029 mandatory = False,
30 default = {},
31 doc = """The string parameters to supply to Jinja2.""",
32 ),
33 "list_parameters": attr.string_list_dict(
34 mandatory = False,
35 default = {},
36 doc = """The string list parameters to supply to Jinja2.""",
Austin Schuh3a2f4a42020-09-16 14:10:39 -070037 ),
James Kuszmaulfe650202023-02-05 17:31:19 -080038 "includes": attr.label_list(
39 allow_files = True,
40 doc = """Files which are included by the template.""",
41 ),
Austin Schuh3a2f4a42020-09-16 14:10:39 -070042 "_jinja2": attr.label(
43 default = "//tools/build_rules:jinja2_generator",
Adam Snaider13d48d92023-08-03 12:20:15 -070044 cfg = "exec",
Austin Schuh3a2f4a42020-09-16 14:10:39 -070045 executable = True,
46 ),
47 },
48 implementation = _jinja2_template_impl,
49 doc = """Expands a jinja2 template given parameters.""",
50)
Milind Upadhyay302296f2023-03-23 16:36:51 -070051
52def jinja2_template(name, src, parameters = {}, list_parameters = {}, **kwargs):
Milind Upadhyay302296f2023-03-23 16:36:51 -070053 # Since the `out` field will be set to `name`, and the name for the rule must
Milind Upadhyay0ff31aa2023-03-28 14:18:41 -070054 # differ from `out`, name the rule as the `name` plus a suffix
55 rule_name = name + "_rule"
Milind Upadhyay302296f2023-03-23 16:36:51 -070056
57 jinja2_template_rule(name = rule_name, out = name, src = src, parameters = parameters, list_parameters = list_parameters, **kwargs)