Serge Bazanski | 4915425 | 2020-10-24 20:22:08 +0200 | [diff] [blame] | 1 | def _gostatic_tarball_impl(ctx): |
| 2 | out = ctx.actions.declare_directory("out") |
| 3 | tarball_name = ctx.attr.name + ".tar" |
| 4 | tarball = ctx.actions.declare_file(tarball_name) |
| 5 | |
| 6 | config_name = ctx.attr.name + ".config" |
| 7 | config = ctx.actions.declare_file(config_name) |
| 8 | |
| 9 | # Build path to root of sources, based on source_dir |
| 10 | # and location of the instantiating BUILDfile |
| 11 | # (source_dir is defined as relative to the BUILD file). |
| 12 | source_dir = '/'.join(ctx.build_file_path.split('/')[:-1]) + '/' + ctx.attr.source_dir |
| 13 | |
| 14 | # Relative path to go up from generated config to build |
| 15 | # root. This is because gostatic is magical and really |
| 16 | # wants the config to be alongside the source. |
| 17 | up = "../" * (config.path.count("/")) |
| 18 | templates = " ".join([up + f.path for f in ctx.files.templates]) |
| 19 | config_lines = [ |
| 20 | "OUTPUT = {}".format(up + out.path), |
| 21 | "TEMPLATES = {}".format(templates), |
| 22 | "SOURCE = {}".format(up + source_dir), |
| 23 | ] |
| 24 | config_content = "\n".join(config_lines) |
| 25 | config_content += ctx.attr.extra_config |
| 26 | |
| 27 | ctx.actions.write(config, config_content) |
| 28 | |
| 29 | ctx.actions.run( |
| 30 | outputs = [out], |
| 31 | inputs = [config] + ctx.files.templates + ctx.files.srcs, |
| 32 | executable = ctx.file._gostatic, |
| 33 | arguments = [config.path], |
| 34 | ) |
| 35 | ctx.actions.run( |
| 36 | outputs = [tarball], |
| 37 | inputs = [out], |
| 38 | executable = ctx.file._tarify, |
| 39 | arguments = [ |
| 40 | "-site", out.path, |
| 41 | "-tarball", tarball.path, |
| 42 | ], |
| 43 | ) |
| 44 | |
| 45 | return [DefaultInfo(files=depset([tarball]))] |
| 46 | |
| 47 | gostatic_tarball = rule( |
| 48 | implementation = _gostatic_tarball_impl, |
| 49 | attrs = { |
| 50 | "extra_config": attr.string( |
| 51 | mandatory = True, |
| 52 | doc = """ |
| 53 | Gostatic configuration (rules, etc). Do not specify OUTPUT, TEMPLATES |
| 54 | or SOURCES - these are automatically generated. |
| 55 | """, |
| 56 | ), |
| 57 | "source_dir": attr.string( |
| 58 | mandatory = True, |
| 59 | doc = "Root of site sources. Relative to BUILDfile.", |
| 60 | ), |
| 61 | "srcs": attr.label_list( |
| 62 | allow_files = True, |
| 63 | doc = "Site sources, all must be contained within source_dir" |
| 64 | ), |
| 65 | "templates": attr.label_list( |
| 66 | allow_files = True, |
| 67 | doc = "Templates to use (passed to TEMPLATES in gostatic config).", |
| 68 | ), |
| 69 | "_gostatic": attr.label( |
| 70 | default = Label("//tools/gostatic"), |
| 71 | allow_single_file = True, |
| 72 | executable = True, |
| 73 | cfg = "exec", |
| 74 | doc = "Path to gostatic binary.", |
| 75 | ), |
| 76 | "_tarify": attr.label( |
| 77 | default = Label("//tools/gostatic/tarify"), |
| 78 | allow_single_file = True, |
| 79 | executable = True, |
| 80 | cfg = "exec", |
| 81 | doc = "Path to tarify binary.", |
| 82 | ), |
| 83 | }, |
| 84 | ) |