forked from improbable-eng/ts-protoc-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defs.bzl
218 lines (189 loc) · 7.37 KB
/
defs.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@build_bazel_rules_nodejs//:defs.bzl", "yarn_install")
TypescriptProtoLibraryAspect = provider(
fields = {
"js_outputs": "The JS output files produced directly from the src protos",
"dts_outputs": "Ths TS definition files produced directly from the src protos",
"deps_js": "The transitive JS dependencies",
"deps_dts": "The transitive dependencies' TS definitions",
},
)
def proto_path(proto):
"""
The proto path is not really a file path
It's the path to the proto that was seen when the descriptor file was generated.
"""
path = proto.path
root = proto.root.path
ws = proto.owner.workspace_root
if path.startswith(root):
path = path[len(root):]
if path.startswith("/"):
path = path[1:]
if path.startswith(ws):
path = path[len(ws):]
if path.startswith("/"):
path = path[1:]
return path
def append_to_outputs(ctx, file_name, js_outputs, dts_outputs, file_modifications):
generated_filenames = ["_pb.d.ts", "_pb.js", "_pb_service.js", "_pb_service.d.ts"]
for f in generated_filenames:
output = ctx.actions.declare_file(file_name + f)
if f.endswith(".d.ts"):
dts_outputs.append(output)
else:
js_outputs.append(output)
def _convert_js_files_to_amd_modules(ctx, js_outputs):
"""
Converts the output js into an AMD module, similar to how it is done in rules_typecript:
https://github.com/bazelbuild/rules_typescript/blob/master/internal/protobufjs/typescript_proto_library_aspectrary.bzl#L47
"""
amd_module_conversions = []
for js_file in js_outputs:
file_path = "/".join([p for p in [
ctx.workspace_name,
ctx.label.package,
] if p])
file_name = js_file.basename[:-len(js_file.extension) - 1]
define_code = 'define("%s/%s", function(require, exports, module) {' % (file_path, file_name)
cmd = "sed -i '1s;^;%s;' %s" % (define_code, js_file.path)
cmd += " && echo '});' >> %s" % (js_file.path)
cmd += " && sed -i -E 's/(\.\.\/)+/%s\//' %s" % (ctx.workspace_name, js_file.path)
cmd += " && sed -i -E 's/(require\(.*).js/\\1/' %s" % (js_file.path)
amd_module_conversions.append(cmd)
return amd_module_conversions
def typescript_proto_library_aspect_(target, ctx):
"""
A bazel aspect that is applied on every proto_library rule on the transitive set of dependencies
of a typescript_proto_library rule.
Handles running protoc to produce the generated JS and TS files.
"""
js_outputs = []
dts_outputs = []
proto_inputs = []
file_modifications = []
inputs = depset([ctx.file._protoc])
for src in target.proto.direct_sources:
if src.extension != "proto":
fail("Input must be a proto file")
file_name = src.basename[:-len(src.extension) - 1]
normalized_file = proto_path(src)
proto_inputs.append(normalized_file)
append_to_outputs(ctx, file_name, js_outputs, dts_outputs, file_modifications)
outputs = dts_outputs + js_outputs
inputs += ctx.files._ts_protoc_gen
inputs += target.proto.direct_sources
inputs += target.proto.transitive_descriptor_sets
descriptor_sets_paths = [desc.path for desc in target.proto.transitive_descriptor_sets]
protoc_output_dir = ctx.var["BINDIR"]
protoc_command = "%s" % (ctx.file._protoc.path)
protoc_command += " --plugin=protoc-gen-ts=%s" % (ctx.files._ts_protoc_gen[1].path)
protoc_command += " --ts_out=service=true:%s" % (protoc_output_dir)
protoc_command += " --js_out=import_style=commonjs,binary:%s" % (protoc_output_dir)
protoc_command += " --descriptor_set_in=%s" % (":".join(descriptor_sets_paths))
protoc_command += " %s" % (" ".join(proto_inputs))
amd_module_conversions = _convert_js_files_to_amd_modules(ctx, js_outputs)
commands = [protoc_command] + file_modifications + amd_module_conversions
command = " && ".join(commands)
ctx.actions.run_shell(
inputs = inputs,
outputs = outputs,
progress_message = "Creating Typescript pb files %s" % ctx.label,
command = command,
)
dts_outputs = depset(dts_outputs)
js_outputs = depset(js_outputs)
deps_js = depset([])
deps_dts = depset([])
for dep in ctx.rule.attr.deps:
aspect_data = dep[TypescriptProtoLibraryAspect]
deps_dts += aspect_data.dts_outputs + aspect_data.deps_dts
deps_js += aspect_data.js_outputs + aspect_data.deps_js
return [TypescriptProtoLibraryAspect(
dts_outputs = dts_outputs,
js_outputs = js_outputs,
deps_dts = deps_dts,
deps_js = deps_js,
)]
typescript_proto_library_aspect = aspect(
implementation = typescript_proto_library_aspect_,
attr_aspects = ["deps"],
attrs = {
"_ts_protoc_gen": attr.label(
allow_files = True,
executable = True,
cfg = "host",
default = Label("@ts_protoc_gen//bin:protoc-gen-ts"),
),
"_protoc": attr.label(
allow_files = True,
single_file = True,
executable = True,
cfg = "host",
default = Label("@com_google_protobuf//:protoc"),
),
},
)
def _typescript_proto_library_impl(ctx):
"""
Handles converting the aspect output into a provider compatible with the rules_typescript rules.
"""
aspect_data = ctx.attr.proto[TypescriptProtoLibraryAspect]
dts_outputs = aspect_data.dts_outputs
js_outputs = aspect_data.js_outputs
outputs = js_outputs + dts_outputs
return struct(
typescript = struct(
declarations = dts_outputs,
transitive_declarations = dts_outputs + aspect_data.deps_dts,
type_blacklisted_declarations = depset([]),
es5_sources = js_outputs + aspect_data.deps_js,
es6_sources = js_outputs + aspect_data.deps_js,
transitive_es5_sources = js_outputs + aspect_data.deps_js,
transitive_es6_sources = js_outputs + aspect_data.deps_js,
),
legacy_info = struct(
files = outputs,
),
providers = [
DefaultInfo(files = outputs),
],
)
typescript_proto_library = rule(
attrs = {
"proto": attr.label(
mandatory = True,
allow_files = True,
single_file = True,
providers = ["proto"],
aspects = [typescript_proto_library_aspect],
),
"_ts_protoc_gen": attr.label(
allow_files = True,
executable = True,
cfg = "host",
default = Label("@ts_protoc_gen//bin:protoc-gen-ts"),
),
"_protoc": attr.label(
allow_files = True,
single_file = True,
executable = True,
cfg = "host",
default = Label("@com_google_protobuf//:protoc"),
),
},
implementation = _typescript_proto_library_impl,
)
def typescript_proto_dependencies():
"""
Installs ts-proto-gen dependencies.
Usage:
# WORKSPACE
load("@ts_protoc_gen//:defs.bzl", "typescript_proto_dependencies")
typescript_proto_dependencies()
"""
yarn_install(
name = "ts_protoc_gen_deps",
package_json = "@ts_protoc_gen//:package.json",
yarn_lock = "@ts_protoc_gen//:yarn.lock",
)