Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 1 | def _jinja2_template_impl(ctx): |
| 2 | out = ctx.actions.declare_file(ctx.attr.name) |
| 3 | |
| 4 | ctx.actions.run_shell( |
James Kuszmaul | fe65020 | 2023-02-05 17:31:19 -0800 | [diff] [blame] | 5 | inputs = ctx.files.src + ctx.files.includes, |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 6 | tools = [ctx.executable._jinja2], |
| 7 | progress_message = "Generating " + out.short_path, |
| 8 | outputs = [out], |
| 9 | command = ctx.executable._jinja2.path + " " + ctx.files.src[0].path + " '" + str(ctx.attr.parameters) + "' " + out.path, |
| 10 | ) |
| 11 | |
| 12 | return [DefaultInfo(files = depset([out])), OutputGroupInfo(out = depset([out]))] |
| 13 | |
| 14 | jinja2_template = rule( |
| 15 | attrs = { |
| 16 | "src": attr.label( |
| 17 | mandatory = True, |
| 18 | allow_single_file = True, |
| 19 | doc = """The jinja2 template file to expand.""", |
| 20 | ), |
| 21 | "parameters": attr.string_dict( |
| 22 | mandatory = True, |
| 23 | doc = """The parameters to supply to Jinja2.""", |
| 24 | ), |
James Kuszmaul | fe65020 | 2023-02-05 17:31:19 -0800 | [diff] [blame] | 25 | "includes": attr.label_list( |
| 26 | allow_files = True, |
| 27 | doc = """Files which are included by the template.""", |
| 28 | ), |
Austin Schuh | 3a2f4a4 | 2020-09-16 14:10:39 -0700 | [diff] [blame] | 29 | "_jinja2": attr.label( |
| 30 | default = "//tools/build_rules:jinja2_generator", |
| 31 | cfg = "host", |
| 32 | executable = True, |
| 33 | ), |
| 34 | }, |
| 35 | implementation = _jinja2_template_impl, |
| 36 | doc = """Expands a jinja2 template given parameters.""", |
| 37 | ) |