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 | |
| 6 | ctx.actions.run_shell( |
James Kuszmaul | fe65020 | 2023-02-05 17:31:19 -0800 | [diff] [blame] | 7 | inputs = ctx.files.src + ctx.files.includes, |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 8 | tools = [ctx.executable._jinja2], |
| 9 | progress_message = "Generating " + out.short_path, |
| 10 | outputs = [out], |
James Kuszmaul | ba43b06 | 2024-01-14 17:29:21 -0800 | [diff] [blame] | 11 | # 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 Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | return [DefaultInfo(files = depset([out])), OutputGroupInfo(out = depset([out]))] |
| 16 | |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 17 | jinja2_template_rule = rule( |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 18 | attrs = { |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 19 | "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 Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 23 | "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 Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 29 | 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 Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 37 | ), |
James Kuszmaul | fe65020 | 2023-02-05 17:31:19 -0800 | [diff] [blame] | 38 | "includes": attr.label_list( |
| 39 | allow_files = True, |
| 40 | doc = """Files which are included by the template.""", |
| 41 | ), |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 42 | "_jinja2": attr.label( |
| 43 | default = "//tools/build_rules:jinja2_generator", |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 44 | cfg = "exec", |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 45 | executable = True, |
| 46 | ), |
| 47 | }, |
| 48 | implementation = _jinja2_template_impl, |
| 49 | doc = """Expands a jinja2 template given parameters.""", |
| 50 | ) |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 51 | |
| 52 | def jinja2_template(name, src, parameters = {}, list_parameters = {}, **kwargs): |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 53 | # 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] | 54 | # differ from `out`, name the rule as the `name` plus a suffix |
| 55 | rule_name = name + "_rule" |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 56 | |
| 57 | jinja2_template_rule(name = rule_name, out = name, src = src, parameters = parameters, list_parameters = list_parameters, **kwargs) |