Skip to content

Commit b633c0f

Browse files
committed
Add bower_repository.
- Demonstration of a possible bower_repository rule. - Example of running an node_binary from an npm module (webpack). - Refactors common attributes for rules into common origin. - Refactor dar (determistic archive) and dson (deterministic json).
1 parent d93a80a commit b633c0f

File tree

13 files changed

+344
-148
lines changed

13 files changed

+344
-148
lines changed

WORKSPACE

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
workspace(name = "org_pubref_rules_node")
22

3-
load("//node:rules.bzl", "node_repositories", "npm_repository")
3+
load("//node:rules.bzl", "node_repositories", "npm_repository", "bower_repository")
44
node_repositories()
55

66
npm_repository(
@@ -19,3 +19,27 @@ npm_repository(
1919
},
2020
sha256 = "dedabd07bf8399ef5bd6032e87a3ea17eef08183d8766ccedaef63d7707283b6",
2121
)
22+
23+
npm_repository(
24+
name = "npm_webpack",
25+
deps = {
26+
"webpack": "1.13.2",
27+
},
28+
sha256 = "705fac8595a57368185ac25f0a54bac475f4646285b4bc4af650ae754ac56e2b",
29+
)
30+
31+
npm_repository(
32+
name = "npm_bower",
33+
deps = {
34+
"bower": "1.7.9",
35+
},
36+
sha256 = "7f85a05c00a86b0f9cfd8d58ad61d0447bd9235d13a743ede87af1ca5509403f",
37+
)
38+
39+
bower_repository(
40+
name = "bower_react_stack",
41+
deps = {
42+
"react": "15.3.2",
43+
},
44+
sha256 = "9779fcd247213b898d53473d4cc884f8b2d64b7d8021f56dd54a6dcd5f1bf845",
45+
)

examples/webpack/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("//node:rules.bzl", "node_binary")
2+
3+
node_binary(
4+
name = "webpack",
5+
main = "@npm_webpack//:bin/webpack",
6+
modules = [
7+
"@npm_webpack//:modules",
8+
],
9+
)

node/internal/bower_repository.bzl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
)

node/internal/dar.bzl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
load("//node:internal/node_utils.bzl", "execute")
2+
3+
dar_attrs = {
4+
"dar": attr.label(
5+
default = Label("//node:tools/dar.py"),
6+
single_file = True,
7+
allow_files = True,
8+
cfg = "host",
9+
),
10+
"dar_filename": attr.string(
11+
default = "node_modules",
12+
),
13+
"dar_root": attr.string(
14+
default = "lib/node_modules",
15+
),
16+
}
17+
18+
def dar_execute(ctx, dar_root=None):
19+
python = ctx.which("python")
20+
if not python:
21+
fail("python not found (is it present in your PATH?)")
22+
23+
dar_filename = ctx.attr.dar_filename
24+
dar_file = "%s.tar" % ctx.attr.dar_filename
25+
dar_py = ctx.path(ctx.attr.dar)
26+
if not dar_root:
27+
dar_root=ctx.attr.dar_root
28+
tarfile = "%s.tar" % dar_filename
29+
30+
cmd = [
31+
python,
32+
dar_py,
33+
"--output", tarfile,
34+
"--file", "%s=%s" % (dar_filename, dar_root),
35+
]
36+
37+
print("dar: %s" % cmd)
38+
return execute(ctx, cmd)

node/internal/dson.bzl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
load("//node:internal/node_utils.bzl", "execute")
2+
3+
dson_attrs = {
4+
"dson": attr.label(
5+
default = Label("//node:tools/dson.py"),
6+
single_file = True,
7+
allow_files = True,
8+
cfg = "host",
9+
),
10+
"dson_path": attr.string(
11+
default = "lib/node_modules",
12+
),
13+
"dson_filenames": attr.string_list(
14+
default = ["package.json"],
15+
),
16+
"dson_exclude_keys": attr.string_list(
17+
default = [
18+
"_args",
19+
"_from",
20+
"_inCache",
21+
"_installable",
22+
"_nodeVersion",
23+
"_npmOperationalInternal",
24+
"_npmUser",
25+
"_npmVersion",
26+
"_phantomChildren",
27+
"_resolved",
28+
"_requested",
29+
"_requiredBy",
30+
"_where",
31+
],
32+
),
33+
}
34+
35+
def dson_execute(ctx, dson_path=None):
36+
python = ctx.which("python")
37+
if not python:
38+
fail("python not found (is it present in your PATH?)")
39+
dson_py = ctx.path(ctx.attr.dson)
40+
if not dson_path:
41+
dson_path = ctx.attr.dson_path
42+
cmd = [
43+
python,
44+
dson_py,
45+
"--path", "%s/%s" % (ctx.path(""), dson_path),
46+
"--verbose", "--verbose",
47+
]
48+
49+
for filename in ctx.attr.dson_filenames:
50+
cmd += ["--filename", filename]
51+
52+
for key in ctx.attr.dson_exclude_keys:
53+
cmd += ["--exclude", key]
54+
55+
#print("dson: %s" % cmd)
56+
57+
result = execute(ctx, cmd)
58+
59+
#print("dson-out: %s" % result.stdout)
60+
61+
return result

node/internal/node_binary.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def _get_node_modules_dir_from_package_json(file):
2727
return d
2828

2929

30-
3130
def _get_node_modules_dir_from_sourcefile(file):
3231
bin = str(file)
3332
parts = bin.partition("[source]]")
@@ -83,7 +82,8 @@ node_binary = rule(
8382
attrs = {
8483
"main": attr.label(
8584
single_file = True,
86-
allow_files = _js_filetype,
85+
allow_files = True,
86+
#allow_files = _js_filetype,
8787
),
8888
"data": attr.label_list(
8989
allow_files = True,

node/internal/node_utils.bzl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
node_attrs = {
2+
"node": attr.label(
3+
default = Label("@org_pubref_rules_node_toolchain//:bin/node"),
4+
single_file = True,
5+
allow_files = True,
6+
executable = True,
7+
cfg = "host",
8+
),
9+
}
10+
11+
def execute(ctx, cmds):
12+
result = ctx.execute(cmds)
13+
if result.return_code:
14+
fail(" ".join(cmds) + "failed: %s" %(result.stderr))
15+
return result

0 commit comments

Comments
 (0)