blob: 28d3c824c39e554d22cf3c62e46e6d1cb9ff824c [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
Philipp Schrader75f1c262024-04-03 11:32:58 -07006 # For now we don't really want the user to worry about which configuration
7 # to pull the file from. We don't yet have a use case for pulling the same
8 # file from multiple configurations. We point Jinja at all the configuration
9 # roots.
10 include_dirs = depset([
11 file.root.path or "."
12 for file in ctx.files.includes
13 ]).to_list()
14
15 args = ctx.actions.args()
16 args.add(ctx.file.src)
17 args.add(json.encode(parameters))
18 args.add(out)
19 args.add_all(include_dirs, before_each = "--include_dir")
20
21 ctx.actions.run(
James Kuszmaulfe650202023-02-05 17:31:19 -080022 inputs = ctx.files.src + ctx.files.includes,
Austin Schuh3a2f4a42020-09-16 14:10:39 -070023 tools = [ctx.executable._jinja2],
24 progress_message = "Generating " + out.short_path,
25 outputs = [out],
Philipp Schrader75f1c262024-04-03 11:32:58 -070026 executable = ctx.executable._jinja2,
27 arguments = [args],
Austin Schuh3a2f4a42020-09-16 14:10:39 -070028 )
29
30 return [DefaultInfo(files = depset([out])), OutputGroupInfo(out = depset([out]))]
31
Milind Upadhyay302296f2023-03-23 16:36:51 -070032jinja2_template_rule = rule(
Austin Schuh3a2f4a42020-09-16 14:10:39 -070033 attrs = {
Milind Upadhyay302296f2023-03-23 16:36:51 -070034 "out": attr.output(
35 mandatory = True,
36 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.""",
37 ),
Austin Schuh3a2f4a42020-09-16 14:10:39 -070038 "src": attr.label(
39 mandatory = True,
40 allow_single_file = True,
41 doc = """The jinja2 template file to expand.""",
42 ),
43 "parameters": attr.string_dict(
Milind Upadhyay302296f2023-03-23 16:36:51 -070044 mandatory = False,
45 default = {},
46 doc = """The string parameters to supply to Jinja2.""",
47 ),
48 "list_parameters": attr.string_list_dict(
49 mandatory = False,
50 default = {},
51 doc = """The string list parameters to supply to Jinja2.""",
Austin Schuh3a2f4a42020-09-16 14:10:39 -070052 ),
James Kuszmaulfe650202023-02-05 17:31:19 -080053 "includes": attr.label_list(
54 allow_files = True,
55 doc = """Files which are included by the template.""",
56 ),
Austin Schuh3a2f4a42020-09-16 14:10:39 -070057 "_jinja2": attr.label(
58 default = "//tools/build_rules:jinja2_generator",
Adam Snaider13d48d92023-08-03 12:20:15 -070059 cfg = "exec",
Austin Schuh3a2f4a42020-09-16 14:10:39 -070060 executable = True,
61 ),
62 },
63 implementation = _jinja2_template_impl,
64 doc = """Expands a jinja2 template given parameters.""",
65)
Milind Upadhyay302296f2023-03-23 16:36:51 -070066
67def jinja2_template(name, src, parameters = {}, list_parameters = {}, **kwargs):
Milind Upadhyay302296f2023-03-23 16:36:51 -070068 # Since the `out` field will be set to `name`, and the name for the rule must
Milind Upadhyay0ff31aa2023-03-28 14:18:41 -070069 # differ from `out`, name the rule as the `name` plus a suffix
70 rule_name = name + "_rule"
Milind Upadhyay302296f2023-03-23 16:36:51 -070071
Philipp Schrader75f1c262024-04-03 11:32:58 -070072 jinja2_template_rule(
73 name = rule_name,
74 out = name,
75 src = src,
76 parameters = parameters,
77 list_parameters = list_parameters,
78 **kwargs
79 )