1var gulp = require('gulp'); 2var execFile = require('child_process').execFile; 3var glob = require('glob'); 4 5function exec(command, cb) { 6 execFile('sh', ['-c', command], cb); 7} 8 9var protoc = process.env.PROTOC || '../src/protoc'; 10 11var wellKnownTypes = [ 12 '../src/google/protobuf/any.proto', 13 '../src/google/protobuf/api.proto', 14 '../src/google/protobuf/compiler/plugin.proto', 15 '../src/google/protobuf/descriptor.proto', 16 '../src/google/protobuf/duration.proto', 17 '../src/google/protobuf/empty.proto', 18 '../src/google/protobuf/field_mask.proto', 19 '../src/google/protobuf/source_context.proto', 20 '../src/google/protobuf/struct.proto', 21 '../src/google/protobuf/timestamp.proto', 22 '../src/google/protobuf/type.proto', 23 '../src/google/protobuf/wrappers.proto', 24]; 25 26var group1Protos = [ 27 'data.proto', 28 'test3.proto', 29 'test5.proto', 30 'commonjs/test6/test6.proto', 31 'test8.proto', 32 'test11.proto', 33 'test12.proto', 34 'test13.proto', 35 'test14.proto', 36 'test15.proto', 37 'testbinary.proto', 38 'testempty.proto', 39 'test.proto', 40 'testlargenumbers.proto', 41]; 42 43var group2Protos = [ 44 'proto3_test.proto', 45 'test2.proto', 46 'test4.proto', 47 'commonjs/test7/test7.proto', 48]; 49 50var group3Protos = [ 51 'test9.proto', 52 'test10.proto' 53]; 54 55 56gulp.task('genproto_well_known_types_closure', function (cb) { 57 exec(protoc + ' --js_out=one_output_file_per_input_file,binary:. -I ../src -I . ' + wellKnownTypes.join(' '), 58 function (err, stdout, stderr) { 59 console.log(stdout); 60 console.log(stderr); 61 cb(err); 62 }); 63}); 64 65gulp.task('genproto_group1_closure', function (cb) { 66 exec(protoc + ' --js_out=library=testproto_libs1,binary:. -I ../src -I . ' + group1Protos.join(' '), 67 function (err, stdout, stderr) { 68 console.log(stdout); 69 console.log(stderr); 70 cb(err); 71 }); 72}); 73 74gulp.task('genproto_group2_closure', function (cb) { 75 exec(protoc + ' --experimental_allow_proto3_optional --js_out=library=testproto_libs2,binary:. -I ../src -I . -I commonjs ' + group2Protos.join(' '), 76 function (err, stdout, stderr) { 77 console.log(stdout); 78 console.log(stderr); 79 cb(err); 80 }); 81}); 82 83gulp.task('genproto_well_known_types_commonjs', function (cb) { 84 exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out -I ../src ' + wellKnownTypes.join(' '), 85 function (err, stdout, stderr) { 86 console.log(stdout); 87 console.log(stderr); 88 cb(err); 89 }); 90}); 91 92gulp.task('genproto_group1_commonjs', function (cb) { 93 exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out -I ../src -I commonjs -I . ' + group1Protos.join(' '), 94 function (err, stdout, stderr) { 95 console.log(stdout); 96 console.log(stderr); 97 cb(err); 98 }); 99}); 100 101gulp.task('genproto_group2_commonjs', function (cb) { 102 exec('mkdir -p commonjs_out && ' + protoc + ' --experimental_allow_proto3_optional --js_out=import_style=commonjs,binary:commonjs_out -I ../src -I commonjs -I . ' + group2Protos.join(' '), 103 function (err, stdout, stderr) { 104 console.log(stdout); 105 console.log(stderr); 106 cb(err); 107 }); 108}); 109 110gulp.task('genproto_commonjs_wellknowntypes', function (cb) { 111 exec('mkdir -p commonjs_out/node_modules/google-protobuf && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out/node_modules/google-protobuf -I ../src ' + wellKnownTypes.join(' '), 112 function (err, stdout, stderr) { 113 console.log(stdout); 114 console.log(stderr); 115 cb(err); 116 }); 117}); 118 119gulp.task('genproto_wellknowntypes', function (cb) { 120 exec(protoc + ' --js_out=import_style=commonjs,binary:. -I ../src ' + wellKnownTypes.join(' '), 121 function (err, stdout, stderr) { 122 console.log(stdout); 123 console.log(stderr); 124 cb(err); 125 }); 126}); 127gulp.task('genproto_group3_commonjs_strict', function (cb) { 128 exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs_strict,binary:commonjs_out -I ../src -I commonjs -I . ' + group3Protos.join(' '), 129 function (err, stdout, stderr) { 130 console.log(stdout); 131 console.log(stderr); 132 cb(err); 133 }); 134}); 135 136 137function getClosureBuilderCommand(exportsFile, outputFile) { 138 return './node_modules/google-closure-library/closure/bin/build/closurebuilder.py ' + 139 '--root node_modules ' + 140 '-o compiled ' + 141 '--compiler_jar node_modules/google-closure-compiler-java/compiler.jar ' + 142 '-i ' + exportsFile + ' ' + 143 'map.js message.js binary/arith.js binary/constants.js binary/decoder.js ' + 144 'binary/encoder.js binary/reader.js binary/utils.js binary/writer.js ' + 145 exportsFile + ' > ' + outputFile; 146} 147 148gulp.task('dist', gulp.series(['genproto_wellknowntypes'], function(cb) { 149 // TODO(haberman): minify this more aggressively. 150 // Will require proper externs/exports. 151 exec(getClosureBuilderCommand('commonjs/export.js', 'google-protobuf.js'), 152 function (err, stdout, stderr) { 153 console.log(stdout); 154 console.log(stderr); 155 cb(err); 156 }); 157})); 158 159gulp.task('commonjs_asserts', function (cb) { 160 exec('mkdir -p commonjs_out/test_node_modules && ' + 161 getClosureBuilderCommand( 162 'commonjs/export_asserts.js', 163 'commonjs_out/test_node_modules/closure_asserts_commonjs.js'), 164 function (err, stdout, stderr) { 165 console.log(stdout); 166 console.log(stderr); 167 cb(err); 168 }); 169}); 170 171gulp.task('commonjs_testdeps', function (cb) { 172 exec('mkdir -p commonjs_out/test_node_modules && ' + 173 getClosureBuilderCommand( 174 'commonjs/export_testdeps.js', 175 'commonjs_out/test_node_modules/testdeps_commonjs.js'), 176 function (err, stdout, stderr) { 177 console.log(stdout); 178 console.log(stderr); 179 cb(err); 180 }); 181}); 182 183gulp.task( 184 'make_commonjs_out', 185 gulp.series( 186 [ 187 'dist', 'genproto_well_known_types_commonjs', 188 'genproto_group1_commonjs', 'genproto_group2_commonjs', 189 'genproto_commonjs_wellknowntypes', 'commonjs_asserts', 190 'commonjs_testdeps', 'genproto_group3_commonjs_strict' 191 ], 192 function(cb) { 193 // TODO(haberman): minify this more aggressively. 194 // Will require proper externs/exports. 195 var cmd = 196 'mkdir -p commonjs_out/binary && mkdir -p commonjs_out/test_node_modules && '; 197 function addTestFile(file) { 198 cmd += 'node commonjs/rewrite_tests_for_commonjs.js < ' + file + 199 ' > commonjs_out/' + file + '&& '; 200 } 201 202 glob.sync('*_test.js').forEach(addTestFile); 203 glob.sync('binary/*_test.js').forEach(addTestFile); 204 205 exec( 206 cmd + 'cp commonjs/jasmine.json commonjs_out/jasmine.json && ' + 207 'cp google-protobuf.js commonjs_out/test_node_modules && ' + 208 'cp commonjs/strict_test.js commonjs_out/strict_test.js &&' + 209 'cp commonjs/import_test.js commonjs_out/import_test.js', 210 function(err, stdout, stderr) { 211 console.log(stdout); 212 console.log(stderr); 213 cb(err); 214 }); 215 })); 216 217gulp.task( 218 'deps', 219 gulp.series( 220 [ 221 'genproto_well_known_types_closure', 'genproto_group1_closure', 222 'genproto_group2_closure' 223 ], 224 function(cb) { 225 exec( 226 './node_modules/google-closure-library/closure/bin/build/depswriter.py 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', 227 function(err, stdout, stderr) { 228 console.log(stdout); 229 console.log(stderr); 230 cb(err); 231 }); 232 })); 233 234gulp.task( 235 'test_closure', 236 gulp.series( 237 [ 238 'genproto_well_known_types_closure', 'genproto_group1_closure', 239 'genproto_group2_closure', 'deps' 240 ], 241 function(cb) { 242 exec( 243 'JASMINE_CONFIG_PATH=jasmine.json ./node_modules/.bin/jasmine', 244 function(err, stdout, stderr) { 245 console.log(stdout); 246 console.log(stderr); 247 cb(err); 248 }); 249 })); 250 251gulp.task('test_commonjs', gulp.series(['make_commonjs_out'], function(cb) { 252 exec('cd commonjs_out && JASMINE_CONFIG_PATH=jasmine.json NODE_PATH=test_node_modules ../node_modules/.bin/jasmine', 253 function (err, stdout, stderr) { 254 console.log(stdout); 255 console.log(stderr); 256 cb(err); 257 }); 258})); 259 260gulp.task('test', gulp.series(['test_closure', 'test_commonjs'], function(cb) { 261 cb(); 262})); 263