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") |
Philipp Schrader | 4187e17 | 2024-04-03 11:45:19 -0700 | [diff] [blame^] | 20 | if ctx.file.parameters_file: |
| 21 | args.add("--replacements_file", ctx.file.parameters_file) |
Philipp Schrader | 75f1c26 | 2024-04-03 11:32:58 -0700 | [diff] [blame] | 22 | |
| 23 | ctx.actions.run( |
Philipp Schrader | 4187e17 | 2024-04-03 11:45:19 -0700 | [diff] [blame^] | 24 | inputs = ctx.files.src + ctx.files.includes + ctx.files.parameters_file, |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 25 | tools = [ctx.executable._jinja2], |
| 26 | progress_message = "Generating " + out.short_path, |
| 27 | outputs = [out], |
Philipp Schrader | 75f1c26 | 2024-04-03 11:32:58 -0700 | [diff] [blame] | 28 | executable = ctx.executable._jinja2, |
| 29 | arguments = [args], |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | return [DefaultInfo(files = depset([out])), OutputGroupInfo(out = depset([out]))] |
| 33 | |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 34 | jinja2_template_rule = rule( |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 35 | attrs = { |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 36 | "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 Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 40 | "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 Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 46 | 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 Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 54 | ), |
Philipp Schrader | 4187e17 | 2024-04-03 11:45:19 -0700 | [diff] [blame^] | 55 | "parameters_file": attr.label( |
| 56 | allow_single_file = True, |
| 57 | doc = """A JSON file whose contents are supplied as parameters to Jinja2.""", |
| 58 | ), |
James Kuszmaul | fe65020 | 2023-02-05 17:31:19 -0800 | [diff] [blame] | 59 | "includes": attr.label_list( |
| 60 | allow_files = True, |
| 61 | doc = """Files which are included by the template.""", |
| 62 | ), |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 63 | "_jinja2": attr.label( |
| 64 | default = "//tools/build_rules:jinja2_generator", |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 65 | cfg = "exec", |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 66 | executable = True, |
| 67 | ), |
| 68 | }, |
| 69 | implementation = _jinja2_template_impl, |
| 70 | doc = """Expands a jinja2 template given parameters.""", |
| 71 | ) |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 72 | |
| 73 | def jinja2_template(name, src, parameters = {}, list_parameters = {}, **kwargs): |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 74 | # 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] | 75 | # differ from `out`, name the rule as the `name` plus a suffix |
| 76 | rule_name = name + "_rule" |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 77 | |
Philipp Schrader | 75f1c26 | 2024-04-03 11:32:58 -0700 | [diff] [blame] | 78 | jinja2_template_rule( |
| 79 | name = rule_name, |
| 80 | out = name, |
| 81 | src = src, |
| 82 | parameters = parameters, |
| 83 | list_parameters = list_parameters, |
| 84 | **kwargs |
| 85 | ) |