Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 1 | def _jinja2_template_impl(ctx): |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 2 | out = ctx.outputs.out |
| 3 | parameters = dict(ctx.attr.parameters) |
| 4 | parameters.update(ctx.attr.list_parameters) |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 5 | |
Philipp Schrader | 75f1c26 | 2024-04-03 11:32:58 -0700 | [diff] [blame^] | 6 | # 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 Kuszmaul | fe65020 | 2023-02-05 17:31:19 -0800 | [diff] [blame] | 22 | inputs = ctx.files.src + ctx.files.includes, |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 23 | tools = [ctx.executable._jinja2], |
| 24 | progress_message = "Generating " + out.short_path, |
| 25 | outputs = [out], |
Philipp Schrader | 75f1c26 | 2024-04-03 11:32:58 -0700 | [diff] [blame^] | 26 | executable = ctx.executable._jinja2, |
| 27 | arguments = [args], |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | return [DefaultInfo(files = depset([out])), OutputGroupInfo(out = depset([out]))] |
| 31 | |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 32 | jinja2_template_rule = rule( |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 33 | attrs = { |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 34 | "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 Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 38 | "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 Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 44 | 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 Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 52 | ), |
James Kuszmaul | fe65020 | 2023-02-05 17:31:19 -0800 | [diff] [blame] | 53 | "includes": attr.label_list( |
| 54 | allow_files = True, |
| 55 | doc = """Files which are included by the template.""", |
| 56 | ), |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 57 | "_jinja2": attr.label( |
| 58 | default = "//tools/build_rules:jinja2_generator", |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 59 | cfg = "exec", |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 60 | executable = True, |
| 61 | ), |
| 62 | }, |
| 63 | implementation = _jinja2_template_impl, |
| 64 | doc = """Expands a jinja2 template given parameters.""", |
| 65 | ) |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 66 | |
| 67 | def jinja2_template(name, src, parameters = {}, list_parameters = {}, **kwargs): |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 68 | # Since the `out` field will be set to `name`, and the name for the rule must |
Milind Upadhyay | 0ff31aa | 2023-03-28 14:18:41 -0700 | [diff] [blame] | 69 | # differ from `out`, name the rule as the `name` plus a suffix |
| 70 | rule_name = name + "_rule" |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 71 | |
Philipp Schrader | 75f1c26 | 2024-04-03 11:32:58 -0700 | [diff] [blame^] | 72 | jinja2_template_rule( |
| 73 | name = rule_name, |
| 74 | out = name, |
| 75 | src = src, |
| 76 | parameters = parameters, |
| 77 | list_parameters = list_parameters, |
| 78 | **kwargs |
| 79 | ) |