1+ #
2+ # Copyright (C) 2020 Grakn Labs
3+ #
4+ # This program is free software: you can redistribute it and/or modify
5+ # it under the terms of the GNU Affero General Public License as
6+ # published by the Free Software Foundation, either version 3 of the
7+ # License, or (at your option) any later version.
8+ #
9+ # This program is distributed in the hope that it will be useful,
10+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ # GNU Affero General Public License for more details.
13+ #
14+ # You should have received a copy of the GNU Affero General Public License
15+ # along with this program. If not, see <https://www.gnu.org/licenses/>.
16+ #
17+
18+ def _proto_sources (ctx ):
19+ inputs = []
20+ for dep in ctx .attr .deps :
21+ for src in dep [ProtoInfo ].direct_sources :
22+ inputs .append (src )
23+ return DefaultInfo (files = depset (inputs ))
24+
25+
26+ def _unpack_proto_archive (ctx ):
27+ outputs = []
28+ for dep in ctx .attr .deps :
29+ for src in dep [ProtoInfo ].direct_sources :
30+ outputs .extend ([
31+ ctx .actions .declare_file (
32+ src .path .replace ('.proto' , '_pb.d.ts' )
33+ ),
34+ ctx .actions .declare_file (
35+ src .path .replace ('.proto' , '_pb.js' )
36+ ),
37+ ])
38+
39+ for dep in ctx .attr .grpc_deps :
40+ for src in dep [ProtoInfo ].direct_sources :
41+ outputs .extend ([
42+ ctx .actions .declare_file (
43+ src .path .replace ('.proto' , '_grpc_pb.js' )
44+ ),
45+ ctx .actions .declare_file (
46+ src .path .replace ('.proto' , '_grpc_pb.d.ts' )
47+ ),
48+ ])
49+ ctx .actions .run_shell (
50+ inputs = [ctx .file .archive ],
51+ outputs = outputs ,
52+ mnemonic = 'x' ,
53+ command = "mkdir -p {} && tar -xvf {} -C {}/{}" .format (
54+ ctx .label .package , ctx .file .archive .path ,
55+ ctx .var ["BINDIR" ], ctx .label .package
56+ )
57+ )
58+ return DefaultInfo (files = depset (outputs ))
59+
60+
61+ proto_sources = rule (
62+ attrs = {
63+ "deps" : attr .label_list (
64+ providers = [ProtoInfo ],
65+ )
66+ },
67+ implementation = _proto_sources
68+ )
69+
70+ unpack_proto_archive = rule (
71+ attrs = {
72+ "deps" : attr .label_list (
73+ providers = [ProtoInfo ],
74+ ),
75+ "grpc_deps" : attr .label_list (
76+ providers = [ProtoInfo ],
77+ ),
78+ "archive" : attr .label (
79+ allow_single_file = True
80+ ),
81+ },
82+ implementation = _unpack_proto_archive
83+ )
84+
85+
86+ def ts_grpc_compile (
87+ name ,
88+ deps ,
89+ grpc_deps ):
90+ proto_sources_name = "{}__do_not_reference_1" .format (name )
91+ genrule_name = "{}__do_not_reference_2" .format (name )
92+ proto_sources (
93+ name = proto_sources_name ,
94+ deps = deps
95+ )
96+ native .genrule (
97+ name = genrule_name ,
98+ outs = [
99+ "{}.tar.gz" .format (genrule_name ),
100+ ],
101+ # The below command performs a protoc compilation of our proto files into typescript and javascript. Line by line:
102+ # Run the node.js protoc (protocol compiler) with the following flags:
103+ # Use the gen-ts plugin to generate typescript declaration files in addition to javascript
104+ # Output javascript with commonjs style exports, to the genrule output directory.
105+ # Output services to the genrule output directory (without this line, grakn_grpc_pb is omitted)
106+ # Output typescript to (you guessed it) the genrule output directory
107+ # Set the .proto file relative path to root folder (same as where WORKSPACE resides)
108+ # Use the .proto files found in the :proto-raw-buffers filegroup as inputs.
109+ cmd = "$(execpath //grpc/nodejs:grpc_tools_node_protoc) \
110+ --plugin='protoc-gen-ts=$(rootpath @npm//:node_modules/grpc_tools_node_protoc_ts/bin/protoc-gen-ts)' \
111+ --js_out='import_style=commonjs,binary:./$(@D)/' \
112+ --grpc_out='grpc_js:./$(@D)/' \
113+ --ts_out='grpc_js:./$(@D)/' \
114+ --proto_path=. \
115+ $(execpaths :{}) && tar cvf $@ -C ./$(@D)/ ." .format (proto_sources_name ),
116+ tools = [
117+ "//grpc/nodejs:grpc_tools_node_protoc" ,
118+ "@npm//:node_modules/grpc_tools_node_protoc_ts/bin/protoc-gen-ts" ,
119+ "@npm//grpc_tools_node_protoc_ts" ,
120+ "@npm//google-protobuf" ,
121+ ],
122+ srcs = [proto_sources_name ]
123+ )
124+ unpack_proto_archive (
125+ name = name ,
126+ deps = deps ,
127+ grpc_deps = grpc_deps ,
128+ archive = genrule_name ,
129+ )
0 commit comments