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], |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 11 | command = ctx.executable._jinja2.path + " " + ctx.files.src[0].path + " '" + str(parameters) + "' " + out.path, |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | return [DefaultInfo(files = depset([out])), OutputGroupInfo(out = depset([out]))] |
| 15 | |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 16 | jinja2_template_rule = rule( |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 17 | attrs = { |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 18 | "out": attr.output( |
| 19 | mandatory = True, |
| 20 | 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.""", |
| 21 | ), |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 22 | "src": attr.label( |
| 23 | mandatory = True, |
| 24 | allow_single_file = True, |
| 25 | doc = """The jinja2 template file to expand.""", |
| 26 | ), |
| 27 | "parameters": attr.string_dict( |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 28 | mandatory = False, |
| 29 | default = {}, |
| 30 | doc = """The string parameters to supply to Jinja2.""", |
| 31 | ), |
| 32 | "list_parameters": attr.string_list_dict( |
| 33 | mandatory = False, |
| 34 | default = {}, |
| 35 | doc = """The string list parameters to supply to Jinja2.""", |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 36 | ), |
James Kuszmaul | fe65020 | 2023-02-05 17:31:19 -0800 | [diff] [blame] | 37 | "includes": attr.label_list( |
| 38 | allow_files = True, |
| 39 | doc = """Files which are included by the template.""", |
| 40 | ), |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 41 | "_jinja2": attr.label( |
| 42 | default = "//tools/build_rules:jinja2_generator", |
| 43 | cfg = "host", |
| 44 | executable = True, |
| 45 | ), |
| 46 | }, |
| 47 | implementation = _jinja2_template_impl, |
| 48 | doc = """Expands a jinja2 template given parameters.""", |
| 49 | ) |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 50 | |
| 51 | def jinja2_template(name, src, parameters = {}, list_parameters = {}, **kwargs): |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 52 | # 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^] | 53 | # differ from `out`, name the rule as the `name` plus a suffix |
| 54 | rule_name = name + "_rule" |
Milind Upadhyay | 302296f | 2023-03-23 16:36:51 -0700 | [diff] [blame] | 55 | |
| 56 | jinja2_template_rule(name = rule_name, out = name, src = src, parameters = parameters, list_parameters = list_parameters, **kwargs) |