Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1 | var gulp = require('gulp'); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2 | var execFile = require('child_process').execFile; |
| 3 | var glob = require('glob'); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5 | function exec(command, cb) { |
| 6 | execFile('sh', ['-c', command], cb); |
| 7 | } |
| 8 | |
| 9 | var protoc = process.env.PROTOC || '../src/protoc'; |
| 10 | |
| 11 | var 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 | |
| 26 | var group1Protos = [ |
| 27 | 'data.proto', |
| 28 | 'test3.proto', |
| 29 | 'test5.proto', |
| 30 | 'commonjs/test6/test6.proto', |
| 31 | 'test8.proto', |
| 32 | 'testbinary.proto', |
| 33 | 'testempty.proto', |
| 34 | 'test.proto', |
| 35 | ]; |
| 36 | |
| 37 | var group2Protos = [ |
| 38 | 'proto3_test.proto', |
| 39 | 'test2.proto', |
| 40 | 'test4.proto', |
| 41 | 'commonjs/test7/test7.proto', |
| 42 | ]; |
| 43 | |
| 44 | gulp.task('genproto_well_known_types_closure', function (cb) { |
| 45 | exec(protoc + ' --js_out=one_output_file_per_input_file,binary:. -I ../src -I . ' + wellKnownTypes.join(' '), |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 46 | function (err, stdout, stderr) { |
| 47 | console.log(stdout); |
| 48 | console.log(stderr); |
| 49 | cb(err); |
| 50 | }); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 51 | }); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 52 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 53 | gulp.task('genproto_group1_closure', function (cb) { |
| 54 | exec(protoc + ' --js_out=library=testproto_libs1,binary:. -I ../src -I . ' + group1Protos.join(' '), |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 55 | function (err, stdout, stderr) { |
| 56 | console.log(stdout); |
| 57 | console.log(stderr); |
| 58 | cb(err); |
| 59 | }); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 60 | }); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 61 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 62 | gulp.task('genproto_group2_closure', function (cb) { |
| 63 | exec(protoc + ' --js_out=library=testproto_libs2,binary:. -I ../src -I . -I commonjs ' + group2Protos.join(' '), |
| 64 | function (err, stdout, stderr) { |
| 65 | console.log(stdout); |
| 66 | console.log(stderr); |
| 67 | cb(err); |
| 68 | }); |
| 69 | }); |
| 70 | |
| 71 | gulp.task('genproto_well_known_types_commonjs', function (cb) { |
| 72 | exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out -I ../src ' + wellKnownTypes.join(' '), |
| 73 | function (err, stdout, stderr) { |
| 74 | console.log(stdout); |
| 75 | console.log(stderr); |
| 76 | cb(err); |
| 77 | }); |
| 78 | }); |
| 79 | |
| 80 | gulp.task('genproto_group1_commonjs', function (cb) { |
| 81 | exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out -I ../src -I commonjs -I . ' + group1Protos.join(' '), |
| 82 | function (err, stdout, stderr) { |
| 83 | console.log(stdout); |
| 84 | console.log(stderr); |
| 85 | cb(err); |
| 86 | }); |
| 87 | }); |
| 88 | |
| 89 | gulp.task('genproto_group2_commonjs', function (cb) { |
| 90 | exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out -I ../src -I commonjs -I . ' + group2Protos.join(' '), |
| 91 | function (err, stdout, stderr) { |
| 92 | console.log(stdout); |
| 93 | console.log(stderr); |
| 94 | cb(err); |
| 95 | }); |
| 96 | }); |
| 97 | |
| 98 | gulp.task('genproto_commonjs_wellknowntypes', function (cb) { |
| 99 | 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(' '), |
| 100 | function (err, stdout, stderr) { |
| 101 | console.log(stdout); |
| 102 | console.log(stderr); |
| 103 | cb(err); |
| 104 | }); |
| 105 | }); |
| 106 | |
| 107 | gulp.task('genproto_wellknowntypes', function (cb) { |
| 108 | exec(protoc + ' --js_out=import_style=commonjs,binary:. -I ../src ' + wellKnownTypes.join(' '), |
| 109 | function (err, stdout, stderr) { |
| 110 | console.log(stdout); |
| 111 | console.log(stderr); |
| 112 | cb(err); |
| 113 | }); |
| 114 | }); |
| 115 | |
| 116 | function getClosureBuilderCommand(exportsFile, outputFile) { |
| 117 | return './node_modules/google-closure-library/closure/bin/build/closurebuilder.py ' + |
| 118 | '--root node_modules ' + |
| 119 | '-o compiled ' + |
| 120 | '--compiler_jar node_modules/google-closure-compiler/compiler.jar ' + |
| 121 | '-i ' + exportsFile + ' ' + |
| 122 | 'map.js message.js binary/arith.js binary/constants.js binary/decoder.js ' + |
| 123 | 'binary/encoder.js binary/reader.js binary/utils.js binary/writer.js ' + |
| 124 | exportsFile + ' > ' + outputFile; |
| 125 | } |
| 126 | |
| 127 | gulp.task('dist', ['genproto_wellknowntypes'], function (cb) { |
| 128 | // TODO(haberman): minify this more aggressively. |
| 129 | // Will require proper externs/exports. |
| 130 | exec(getClosureBuilderCommand('commonjs/export.js', 'google-protobuf.js'), |
| 131 | function (err, stdout, stderr) { |
| 132 | console.log(stdout); |
| 133 | console.log(stderr); |
| 134 | cb(err); |
| 135 | }); |
| 136 | }); |
| 137 | |
| 138 | gulp.task('commonjs_asserts', function (cb) { |
| 139 | exec('mkdir -p commonjs_out/test_node_modules && ' + |
| 140 | getClosureBuilderCommand( |
| 141 | 'commonjs/export_asserts.js', |
| 142 | 'commonjs_out/test_node_modules/closure_asserts_commonjs.js'), |
| 143 | function (err, stdout, stderr) { |
| 144 | console.log(stdout); |
| 145 | console.log(stderr); |
| 146 | cb(err); |
| 147 | }); |
| 148 | }); |
| 149 | |
| 150 | gulp.task('commonjs_testdeps', function (cb) { |
| 151 | exec('mkdir -p commonjs_out/test_node_modules && ' + |
| 152 | getClosureBuilderCommand( |
| 153 | 'commonjs/export_testdeps.js', |
| 154 | 'commonjs_out/test_node_modules/testdeps_commonjs.js'), |
| 155 | function (err, stdout, stderr) { |
| 156 | console.log(stdout); |
| 157 | console.log(stderr); |
| 158 | cb(err); |
| 159 | }); |
| 160 | }); |
| 161 | |
| 162 | gulp.task('make_commonjs_out', ['dist', 'genproto_well_known_types_commonjs', 'genproto_group1_commonjs', 'genproto_group2_commonjs', 'genproto_commonjs_wellknowntypes', 'commonjs_asserts', 'commonjs_testdeps'], function (cb) { |
| 163 | // TODO(haberman): minify this more aggressively. |
| 164 | // Will require proper externs/exports. |
| 165 | var cmd = "mkdir -p commonjs_out/binary && mkdir -p commonjs_out/test_node_modules && "; |
| 166 | function addTestFile(file) { |
| 167 | cmd += 'node commonjs/rewrite_tests_for_commonjs.js < ' + file + |
| 168 | ' > commonjs_out/' + file + '&& '; |
| 169 | } |
| 170 | |
| 171 | glob.sync('*_test.js').forEach(addTestFile); |
| 172 | glob.sync('binary/*_test.js').forEach(addTestFile); |
| 173 | |
| 174 | exec(cmd + |
| 175 | 'cp commonjs/jasmine.json commonjs_out/jasmine.json && ' + |
| 176 | 'cp google-protobuf.js commonjs_out/test_node_modules && ' + |
| 177 | 'cp commonjs/import_test.js commonjs_out/import_test.js', |
| 178 | function (err, stdout, stderr) { |
| 179 | console.log(stdout); |
| 180 | console.log(stderr); |
| 181 | cb(err); |
| 182 | }); |
| 183 | }); |
| 184 | |
| 185 | gulp.task('deps', ['genproto_well_known_types_closure', 'genproto_group1_closure', 'genproto_group2_closure'], function (cb) { |
| 186 | exec('./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', |
| 187 | function (err, stdout, stderr) { |
| 188 | console.log(stdout); |
| 189 | console.log(stderr); |
| 190 | cb(err); |
| 191 | }); |
| 192 | }); |
| 193 | |
| 194 | gulp.task('test_closure', ['genproto_well_known_types_closure', 'genproto_group1_closure', 'genproto_group2_closure', 'deps'], function (cb) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 195 | exec('JASMINE_CONFIG_PATH=jasmine.json ./node_modules/.bin/jasmine', |
| 196 | function (err, stdout, stderr) { |
| 197 | console.log(stdout); |
| 198 | console.log(stderr); |
| 199 | cb(err); |
| 200 | }); |
| 201 | }); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 202 | |
| 203 | gulp.task('test_commonjs', ['make_commonjs_out'], function (cb) { |
| 204 | exec('cd commonjs_out && JASMINE_CONFIG_PATH=jasmine.json NODE_PATH=test_node_modules ../node_modules/.bin/jasmine', |
| 205 | function (err, stdout, stderr) { |
| 206 | console.log(stdout); |
| 207 | console.log(stderr); |
| 208 | cb(err); |
| 209 | }); |
| 210 | }); |
| 211 | |
| 212 | gulp.task('test', ['test_closure', 'test_commonjs'], function(cb) { |
| 213 | cb(); |
| 214 | }); |