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( |
| 5 | inputs = ctx.files.src, |
| 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 | ), |
| 25 | "_jinja2": attr.label( |
| 26 | default = "//tools/build_rules:jinja2_generator", |
| 27 | cfg = "host", |
| 28 | executable = True, |
| 29 | ), |
| 30 | }, |
| 31 | implementation = _jinja2_template_impl, |
| 32 | doc = """Expands a jinja2 template given parameters.""", |
| 33 | ) |