-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathrules.bzl
143 lines (117 loc) · 3.3 KB
/
rules.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
load("@org_pubref_rules_node//node:rules.bzl", "node_module")
load("//cpp:rules.bzl", "cpp_proto_repositories")
load("//node:deps.bzl", "DEPS")
load("//protobuf:rules.bzl",
"proto_compile",
"proto_language_deps",
"proto_repositories")
def node_proto_repositories(
omit_cpp_repositories = False,
lang_deps = DEPS,
lang_requires = [
# "npm_protobuf_stack",
# "npm_grpc",
],
**kwargs):
if not omit_cpp_repositories:
cpp_proto_repositories(**kwargs)
rem = proto_repositories(lang_deps = lang_deps,
lang_requires = lang_requires,
**kwargs)
# Load remaining (special) deps
for dep in rem:
rule = dep.pop("rule")
if "npm_repository" == rule:
fail("Unknown loading rule %s for %s" % (rule, dep))
#npm_repository(**dep)
else:
fail("Unknown loading rule %s for %s" % (rule, dep))
def _get_js_variable_name(file):
name = file.basename.rstrip(".js")
# Deal with special characters here?
return name
def _node_proto_module_impl(ctx):
compilation = ctx.attr.compilation.proto_compile_result
index_js = ctx.new_file("%s/index.js" % ctx.label.name)
exports = {}
for unit in compilation.transitive_units:
for file in unit.outputs:
if file.path.endswith("_pb.js"):
name = _get_js_variable_name(file)
exports[name] = file.short_path
elif file.path.endswith("_grpc_pb.js"):
name = _get_js_variable_name(file)
exports[name] = file.short_path
content = []
content.append("module.exports = {")
for name, path in exports.items():
content.append(" '%s': require('./%s')," % (name, path))
content.append("}")
ctx.file_action(
output = index_js,
content = "\n".join(content)
)
return struct(
files = depset([index_js]),
)
_node_proto_module = rule(
implementation = _node_proto_module_impl,
attrs = {
"compilation": attr.label(
providers = ["proto_compile_result"],
mandatory = True,
)
}
)
def node_proto_compile(langs = [str(Label("//node"))], **kwargs):
proto_compile(langs = langs, **kwargs)
def node_proto_library(
name,
langs = [str(Label("//node"))],
protos = [],
imports = [],
inputs = [],
output_to_workspace = False,
proto_deps = [
],
protoc = None,
pb_plugin = None,
pb_options = [],
proto_compile_args = {},
srcs = [],
deps = [
"@yarn_modules//:google-protobuf",
],
data = [],
verbose = 0,
with_grpc = False,
**kwargs):
proto_compile_args += {
"name": name + ".pb",
"protos": protos,
"deps": [dep + ".pb" for dep in proto_deps],
"langs": langs,
"imports": imports,
"inputs": inputs,
"pb_options": pb_options,
"output_to_workspace": output_to_workspace,
"verbose": verbose,
"with_grpc": with_grpc,
}
if protoc:
proto_compile_args["protoc"] = protoc
if pb_plugin:
proto_compile_args["pb_plugin"] = pb_plugin
proto_compile(**proto_compile_args)
_node_proto_module(
name = name + "_index",
compilation = name + ".pb",
)
node_module(
name = name,
index = name + "_index",
layout = "workspace",
srcs = srcs + [name + ".pb"],
data = data + [dep + ".pb" for dep in proto_deps],
deps = depset(deps + proto_deps).to_list(),
**kwargs)