forked from protocolbuffers/protobuf-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
231 lines (193 loc) · 8.01 KB
/
gulpfile.js
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
219
220
221
222
223
224
225
226
227
228
229
230
231
var {series} = require('gulp');
var execFile = require('child_process').execFile;
var glob = require('glob');
var fs = require('fs');
function exec(command, cb) {
execFile('sh', ['-c', command], cb);
}
var plugin = '--plugin=protoc-gen-js=bazel-bin/generator/protoc-gen-js';
var protoc = [(process.env.PROTOC || 'protoc'), plugin].join(' ');
var protocInc = process.env.PROTOC_INC || '../src';
var wellKnownTypes = [
'google/protobuf/any.proto',
'google/protobuf/api.proto',
'google/protobuf/compiler/plugin.proto',
'google/protobuf/descriptor.proto',
'google/protobuf/duration.proto',
'google/protobuf/empty.proto',
'google/protobuf/field_mask.proto',
'google/protobuf/source_context.proto',
'google/protobuf/struct.proto',
'google/protobuf/timestamp.proto',
'google/protobuf/type.proto',
'google/protobuf/wrappers.proto',
];
wellKnownTypes.forEach((path, i) => protocInc + '/' + path);
var group1Protos = [
'data.proto',
'test3.proto',
'test5.proto',
'commonjs/test6/test6.proto',
'test8.proto',
'test11.proto',
'test12.proto',
'test13.proto',
'test14.proto',
'test15.proto',
'testbinary.proto',
'testempty.proto',
'test.proto',
'testlargenumbers.proto',
];
var group2Protos = [
'proto3_test.proto',
'test2.proto',
'test4.proto',
'commonjs/test7/test7.proto',
];
var group3Protos = [
'test9.proto',
'test10.proto'
];
function make_exec_logging_callback(cb) {
return (err, stdout, stderr) => {
console.log(stdout);
console.error(stderr);
cb(err);
}
}
function genproto_well_known_types_closure(cb) {
exec(protoc + ' --js_out=one_output_file_per_input_file,binary:. -I ' + protocInc + ' -I . ' + wellKnownTypes.join(' '),
make_exec_logging_callback(cb));
}
function genproto_group1_closure(cb) {
exec(protoc + ' --js_out=library=testproto_libs1,binary:. -I ' + protocInc + ' -I . ' + group1Protos.join(' '),
make_exec_logging_callback(cb));
}
function genproto_group2_closure(cb) {
exec(
protoc +
' --experimental_allow_proto3_optional' +
' --js_out=library=testproto_libs2,binary:. -I ' + protocInc + ' -I . -I commonjs ' +
group2Protos.join(' '),
make_exec_logging_callback(cb));
}
function genproto_well_known_types_commonjs(cb) {
exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out -I ' + protocInc + ' ' + wellKnownTypes.join(' '),
make_exec_logging_callback(cb));
}
function genproto_group1_commonjs(cb) {
exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out -I ' + protocInc + ' -I commonjs -I . ' + group1Protos.join(' '),
make_exec_logging_callback(cb));
}
function genproto_group2_commonjs(cb) {
exec(
'mkdir -p commonjs_out && ' + protoc +
' --experimental_allow_proto3_optional --js_out=import_style=commonjs,binary:commonjs_out -I ' + protocInc + ' -I commonjs -I . ' +
group2Protos.join(' '),
make_exec_logging_callback(cb));
}
function genproto_commonjs_wellknowntypes(cb) {
exec('mkdir -p commonjs_out/node_modules/google-protobuf && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out/node_modules/google-protobuf -I ' + protocInc + ' ' + wellKnownTypes.join(' '),
make_exec_logging_callback(cb));
}
function genproto_wellknowntypes(cb) {
exec(protoc + ' --js_out=import_style=commonjs,binary:. -I ' + protocInc + ' ' + wellKnownTypes.join(' '),
make_exec_logging_callback(cb));
}
function genproto_group3_commonjs_strict(cb) {
exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs_strict,binary:commonjs_out -I ' + protocInc + ' -I commonjs -I . ' + group3Protos.join(' '),
make_exec_logging_callback(cb));
}
function getClosureCompilerCommand(exportsFile, outputFile) {
const closureLib = 'node_modules/google-closure-library';
return [
'node_modules/.bin/google-closure-compiler',
`--js=${closureLib}/closure/goog/**.js`,
`--js=${closureLib}/third_party/closure/goog/**.js`, '--js=map.js',
'--js=message.js', '--js=binary/arith.js', '--js=binary/constants.js',
'--js=binary/decoder.js', '--js=binary/encoder.js', '--js=binary/reader.js',
'--js=binary/utils.js', '--js=binary/writer.js', `--js=${exportsFile}`,
`--entry_point=${exportsFile}`, `> ${outputFile}`
].join(' ');
}
function gen_google_protobuf_js(cb) {
// TODO(haberman): minify this more aggressively.
// Will require proper externs/exports.
exec(
getClosureCompilerCommand('commonjs/export.js', 'google-protobuf.js'),
make_exec_logging_callback(cb));
}
function commonjs_asserts(cb) {
exec(
'mkdir -p commonjs_out/test_node_modules && ' +
getClosureCompilerCommand(
'commonjs/export_asserts.js',
'commonjs_out/test_node_modules/closure_asserts_commonjs.js'),
make_exec_logging_callback(cb));
}
function commonjs_testdeps(cb) {
exec(
'mkdir -p commonjs_out/test_node_modules && ' +
getClosureCompilerCommand(
'commonjs/export_testdeps.js',
'commonjs_out/test_node_modules/testdeps_commonjs.js'),
make_exec_logging_callback(cb));
}
function commonjs_out(cb) {
// TODO(haberman): minify this more aggressively.
// Will require proper externs/exports.
var cmd =
'mkdir -p commonjs_out/binary && mkdir -p commonjs_out/test_node_modules && ';
function addTestFile(file) {
cmd += 'node commonjs/rewrite_tests_for_commonjs.js < ' + file +
' > commonjs_out/' + file + '&& ';
}
glob.sync('*_test.js').forEach(addTestFile);
glob.sync('binary/*_test.js').forEach(addTestFile);
exec(
cmd + 'cp commonjs/jasmine.json commonjs_out/jasmine.json && ' +
'cp google-protobuf.js commonjs_out/test_node_modules && ' +
'cp commonjs/strict_test.js commonjs_out/strict_test.js &&' +
'cp commonjs/import_test.js commonjs_out/import_test.js',
make_exec_logging_callback(cb));
}
function closure_make_deps(cb) {
exec(
'./node_modules/.bin/closure-make-deps --closure-path=. --file=node_modules/google-closure-library/closure/goog/deps.js binary/arith.js binary/constants.js binary/decoder.js binary/encoder.js binary/reader.js binary/utils.js binary/writer.js debug.js map.js message.js node_loader.js test_bootstrap.js > deps.js',
make_exec_logging_callback(cb));
}
function test_closure(cb) {
exec(
'JASMINE_CONFIG_PATH=jasmine.json ./node_modules/.bin/jasmine',
make_exec_logging_callback(cb));
}
function test_commonjs(cb) {
exec('cd commonjs_out && JASMINE_CONFIG_PATH=jasmine.json NODE_PATH=test_node_modules ../node_modules/.bin/jasmine',
make_exec_logging_callback(cb));
}
exports.build_protoc_plugin = function (cb) {
exec('bazel build generator:protoc-gen-js',
make_exec_logging_callback(cb));
}
exports.dist = series(exports.build_protoc_plugin,
genproto_wellknowntypes,
gen_google_protobuf_js);
exports.make_commonjs_out = series(
exports.dist,
genproto_well_known_types_commonjs,
genproto_group1_commonjs, genproto_group2_commonjs,
genproto_commonjs_wellknowntypes, commonjs_asserts,
commonjs_testdeps, genproto_group3_commonjs_strict,
commonjs_out);
exports.deps = series(exports.build_protoc_plugin,
genproto_well_known_types_closure,
genproto_group1_closure,
genproto_group2_closure,
closure_make_deps);
exports.test_closure = series(exports.deps,
test_closure);
exports.test_commonjs = series(exports.make_commonjs_out,
test_commonjs);
exports.test = series(exports.test_closure,
exports.test_commonjs);