blob: 71749694bdefe98e405264143edc2b48943411a8 [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")
Philipp Schrader4187e172024-04-03 11:45:19 -070020 if ctx.file.parameters_file:
21 args.add("--replacements_file", ctx.file.parameters_file)
Philipp Schrader75f1c262024-04-03 11:32:58 -070022
23 ctx.actions.run(
Philipp Schrader4187e172024-04-03 11:45:19 -070024 inputs = ctx.files.src + ctx.files.includes + ctx.files.parameters_file,
Austin Schuh3a2f4a42020-09-16 14:10:39 -070025 tools = [ctx.executable._jinja2],
26 progress_message = "Generating " + out.short_path,
27 outputs = [out],
Philipp Schrader75f1c262024-04-03 11:32:58 -070028 executable = ctx.executable._jinja2,
29 arguments = [args],
Austin Schuh3a2f4a42020-09-16 14:10:39 -070030 )
31
32 return [DefaultInfo(files = depset([out])), OutputGroupInfo(out = depset([out]))]
33
Milind Upadhyay302296f2023-03-23 16:36:51 -070034jinja2_template_rule = rule(
Austin Schuh3a2f4a42020-09-16 14:10:39 -070035 attrs = {
Milind Upadhyay302296f2023-03-23 16:36:51 -070036 "out": attr.output(
37 mandatory = True,
38 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.""",
39 ),
Austin Schuh3a2f4a42020-09-16 14:10:39 -070040 "src": attr.label(
41 mandatory = True,
42 allow_single_file = True,
43 doc = """The jinja2 template file to expand.""",
44 ),
45 "parameters": attr.string_dict(
Milind Upadhyay302296f2023-03-23 16:36:51 -070046 mandatory = False,
47 default = {},
48 doc = """The string parameters to supply to Jinja2.""",
49 ),
50 "list_parameters": attr.string_list_dict(
51 mandatory = False,
52 default = {},
53 doc = """The string list parameters to supply to Jinja2.""",
Austin Schuh3a2f4a42020-09-16 14:10:39 -070054 ),
Philipp Schrader4187e172024-04-03 11:45:19 -070055 "parameters_file": attr.label(
56 allow_single_file = True,
57 doc = """A JSON file whose contents are supplied as parameters to Jinja2.""",
58 ),
James Kuszmaulfe650202023-02-05 17:31:19 -080059 "includes": attr.label_list(
60 allow_files = True,
61 doc = """Files which are included by the template.""",
62 ),
Austin Schuh3a2f4a42020-09-16 14:10:39 -070063 "_jinja2": attr.label(
64 default = "//tools/build_rules:jinja2_generator",
Adam Snaider13d48d92023-08-03 12:20:15 -070065 cfg = "exec",
Austin Schuh3a2f4a42020-09-16 14:10:39 -070066 executable = True,
67 ),
68 },
69 implementation = _jinja2_template_impl,
70 doc = """Expands a jinja2 template given parameters.""",
71)
Milind Upadhyay302296f2023-03-23 16:36:51 -070072
73def jinja2_template(name, src, parameters = {}, list_parameters = {}, **kwargs):
Milind Upadhyay302296f2023-03-23 16:36:51 -070074 # Since the `out` field will be set to `name`, and the name for the rule must
Milind Upadhyay0ff31aa2023-03-28 14:18:41 -070075 # differ from `out`, name the rule as the `name` plus a suffix
76 rule_name = name + "_rule"
Milind Upadhyay302296f2023-03-23 16:36:51 -070077
Philipp Schrader75f1c262024-04-03 11:32:58 -070078 jinja2_template_rule(
79 name = rule_name,
80 out = name,
81 src = src,
82 parameters = parameters,
83 list_parameters = list_parameters,
84 **kwargs
85 )