|
| 1 | +load("//node:internal/dar.bzl", "dar_attrs", "dar_execute") |
| 2 | +load("//node:internal/dson.bzl", "dson_attrs", "dson_execute") |
| 3 | +load("//node:internal/sha256.bzl", "sha256_attrs", "sha256_execute") |
| 4 | +load("//node:internal/node_utils.bzl", "execute", "node_attrs") |
| 5 | + |
| 6 | +BUILD_FILE = """package(default_visibility = ["//visibility:public"]) |
| 7 | +filegroup( |
| 8 | + name = "components", |
| 9 | + srcs = glob(["{components_path}/**/*"]), |
| 10 | +) |
| 11 | +exports_files(["{components_path}"]) |
| 12 | +""" |
| 13 | + |
| 14 | + |
| 15 | +_bower_repository_attrs = node_attrs + dar_attrs + dson_attrs + sha256_attrs + { |
| 16 | + "bower": attr.label( |
| 17 | + default = Label("@npm_bower//:node_modules/bower/bin/bower"), |
| 18 | + single_file = True, |
| 19 | + allow_files = True, |
| 20 | + executable = True, |
| 21 | + cfg = "host", |
| 22 | + ), |
| 23 | + |
| 24 | + # dar_attrs redefines |
| 25 | + "dar_filename": attr.string( |
| 26 | + default = "bower_components", |
| 27 | + ), |
| 28 | + "dar_root": attr.string( |
| 29 | + default = "bower_components", |
| 30 | + ), |
| 31 | + |
| 32 | + # dson_attrs redefines |
| 33 | + "dson_path": attr.string( |
| 34 | + default = "bower_components", |
| 35 | + ), |
| 36 | + "dson_filenames": attr.string_list( |
| 37 | + default = ["bower.json", ".bower.json"], |
| 38 | + ), |
| 39 | + "dson_exclude_keys": attr.string_list( |
| 40 | + default = [ |
| 41 | + "__dummy_entry_to_prevent_empty_list__", |
| 42 | + ], |
| 43 | + ), |
| 44 | + |
| 45 | + "registry": attr.string(), |
| 46 | + "deps": attr.string_dict(mandatory = True), |
| 47 | +} |
| 48 | + |
| 49 | +def _bower_repository_impl(ctx): |
| 50 | + node = ctx.path(ctx.attr.node) |
| 51 | + nodedir = node.dirname.dirname |
| 52 | + bower = ctx.path(ctx.attr.bower) |
| 53 | + |
| 54 | + bower_json = ['{'] |
| 55 | + bower_json.append('"name": "%s"' % ctx.name) |
| 56 | + if ctx.attr.registry: |
| 57 | + bower_json.append('"registry": "%s"' % ctx.attr.registry) |
| 58 | + bower_json.append('}') |
| 59 | + ctx.file("bower.json", "\n".join(bower_json)) |
| 60 | + |
| 61 | + cmd = [ |
| 62 | + node, |
| 63 | + bower, |
| 64 | + "install", |
| 65 | + ] |
| 66 | + |
| 67 | + modules = [] |
| 68 | + for k, v in ctx.attr.deps.items(): |
| 69 | + if v: |
| 70 | + modules.append("%s#%s" % (k, v)) |
| 71 | + else: |
| 72 | + modules.append(k) |
| 73 | + cmd += modules |
| 74 | + |
| 75 | + output = execute(ctx, cmd).stdout |
| 76 | + |
| 77 | + if ctx.attr.sha256: |
| 78 | + dson_execute(ctx, dson_path = "bower_components") |
| 79 | + dar_execute(ctx, dar_root = "bower_components") |
| 80 | + sha256_execute(ctx, "bower_components.tar") |
| 81 | + |
| 82 | + ctx.file("BUILD", BUILD_FILE.format( |
| 83 | + components_path = "bower_components", |
| 84 | + )) |
| 85 | + |
| 86 | +bower_repository = repository_rule( |
| 87 | + implementation = _bower_repository_impl, |
| 88 | + attrs = _bower_repository_attrs, |
| 89 | +) |
0 commit comments