forked from improbable-eng/ts-protoc-gen
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdefs.bzl
121 lines (100 loc) · 3.93 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
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_install")
def declare_file(ctx, filename, file_modifications):
f = ctx.actions.declare_file(filename)
# Removes an import that protoc-gen-flow adds that is not needed
for removal in ctx.attr.remove_dependencies:
file_modifications.append("echo \"$(grep -v \"%s\" %s)\" > %s" % (removal, f.path, f.path))
return f
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, outputs, file_modifications):
generated_filenames = ["_pb.d.ts", "_pb.js", "_pb_service.js", "_pb_service.d.ts"]
for f in generated_filenames:
outputs.append(declare_file(ctx, file_name + f, file_modifications))
def _typescript_proto_library_impl(ctx):
outputs = []
proto_inputs = []
file_modifications = []
for src in ctx.attr.proto.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, outputs, file_modifications)
if not file_modifications:
file_modifications.append("echo \"No services generated\"")
inputs = depset([ctx.file._protoc])
inputs += ctx.files._ts_protoc_gen
inputs += ctx.attr.proto.proto.transitive_descriptor_sets
descriptor_sets = [desc.path for desc in ctx.attr.proto.proto.transitive_descriptor_sets]
ts_out = "ts=true,service=true:"
protoc_command = "%s --plugin=protoc-gen-flow=%s --flow_out=%s%s --js_out=import_style=commonjs,binary:%s --descriptor_set_in=%s %s" % (ctx.file._protoc.path, ctx.files._ts_protoc_gen[1].path, ts_out, ctx.var["BINDIR"], ctx.var["BINDIR"], ":".join(descriptor_sets), " ".join(proto_inputs))
ctx.actions.run_shell(
inputs = inputs,
outputs = outputs,
progress_message = "Creating Flowtype pb files %s" % ctx.label,
command = "%s && %s" % (protoc_command, " && ".join(file_modifications)),
)
if ctx.attr.debug:
print("protoc command: ", protoc_command)
print("service file modification: ", file_modifications)
print("ctx.var['BINDIR']: ", ctx.var["BINDIR"])
print("normalized_file: ", normalized_file)
return DefaultInfo(
files = depset(outputs),
)
typescript_proto_library = rule(
attrs = {
"proto": attr.label(
mandatory = True,
allow_files = True,
single_file = True,
providers = ["proto"],
),
"remove_dependencies": attr.string_list(
doc = "Each string given will be grepped and removed from the generated files. This can be useful if your proto files are importing a dependency that the generated Flowtype does not use.",
default = [],
allow_empty = True,
),
"debug": attr.bool(
doc="Set for additional logging",
mandatory = False,
default = False,
),
"_ts_protoc_gen": attr.label(
allow_files = True,
executable = True,
cfg = "host",
default = Label("@ts_protoc_gen//bin:protoc-gen-flow"),
),
"_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():
"""To be run in user's WORKSPACE to install ts-protoc-gen dependencies.
"""
npm_install(
name = "deps",
package_json = "@ts_protoc_gen//:package.json",
package_lock_json = "@ts_protoc_gen//:package-lock.json",
)