Josh Haberman | e9cf31e | 2015-12-21 15:18:17 -0800 | [diff] [blame] | 1 | var gulp = require('gulp'); |
Nikolai Vavilov | 7ff229f | 2016-04-15 23:23:33 +0200 | [diff] [blame] | 2 | var execFile = require('child_process').execFile; |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 3 | var glob = require('glob'); |
Josh Haberman | e9cf31e | 2015-12-21 15:18:17 -0800 | [diff] [blame] | 4 | |
Nikolai Vavilov | 7ff229f | 2016-04-15 23:23:33 +0200 | [diff] [blame] | 5 | function exec(command, cb) { |
| 6 | execFile('sh', ['-c', command], cb); |
| 7 | } |
| 8 | |
Josh Haberman | 24c5424 | 2016-02-19 11:46:03 -0800 | [diff] [blame] | 9 | var protoc = process.env.PROTOC || '../src/protoc'; |
| 10 | |
Josh Haberman | 1337486 | 2016-06-16 11:59:08 -0700 | [diff] [blame] | 11 | var wellKnownTypes = [ |
| 12 | '../src/google/protobuf/any.proto', |
| 13 | '../src/google/protobuf/api.proto', |
Josh Haberman | 4308cc4 | 2016-06-16 16:05:47 -0700 | [diff] [blame] | 14 | '../src/google/protobuf/compiler/plugin.proto', |
Josh Haberman | 1337486 | 2016-06-16 11:59:08 -0700 | [diff] [blame] | 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 | |
Josh Haberman | 55cc3aa | 2016-02-02 15:18:34 -0800 | [diff] [blame] | 26 | gulp.task('genproto_closure', function (cb) { |
Adam Cozzette | 40f3586 | 2016-11-18 12:32:52 -0800 | [diff] [blame] | 27 | exec(protoc + ' --js_out=library=testproto_libs,binary:. -I ../src -I . *.proto && ' + protoc + ' --js_out=one_output_file_per_input_file,binary:. -I ../src -I . ' + wellKnownTypes.join(' '), |
Josh Haberman | e9cf31e | 2015-12-21 15:18:17 -0800 | [diff] [blame] | 28 | function (err, stdout, stderr) { |
| 29 | console.log(stdout); |
| 30 | console.log(stderr); |
| 31 | cb(err); |
| 32 | }); |
Josh Haberman | 55cc3aa | 2016-02-02 15:18:34 -0800 | [diff] [blame] | 33 | }); |
Josh Haberman | e9cf31e | 2015-12-21 15:18:17 -0800 | [diff] [blame] | 34 | |
Josh Haberman | 55cc3aa | 2016-02-02 15:18:34 -0800 | [diff] [blame] | 35 | gulp.task('genproto_commonjs', function (cb) { |
Adam Cozzette | 40f3586 | 2016-11-18 12:32:52 -0800 | [diff] [blame] | 36 | exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out -I ../src -I commonjs -I . *.proto commonjs/test*/*.proto ' + wellKnownTypes.join(' '), |
Josh Haberman | 104723f | 2016-06-16 18:40:07 -0700 | [diff] [blame] | 37 | function (err, stdout, stderr) { |
| 38 | console.log(stdout); |
| 39 | console.log(stderr); |
| 40 | cb(err); |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | gulp.task('genproto_commonjs_wellknowntypes', function (cb) { |
Adam Cozzette | 40f3586 | 2016-11-18 12:32:52 -0800 | [diff] [blame] | 45 | 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(' '), |
Josh Haberman | 55cc3aa | 2016-02-02 15:18:34 -0800 | [diff] [blame] | 46 | function (err, stdout, stderr) { |
| 47 | console.log(stdout); |
| 48 | console.log(stderr); |
| 49 | cb(err); |
| 50 | }); |
| 51 | }); |
| 52 | |
Josh Haberman | 1337486 | 2016-06-16 11:59:08 -0700 | [diff] [blame] | 53 | gulp.task('genproto_wellknowntypes', function (cb) { |
| 54 | exec(protoc + ' --js_out=import_style=commonjs,binary:. -I ../src ' + wellKnownTypes.join(' '), |
| 55 | function (err, stdout, stderr) { |
| 56 | console.log(stdout); |
| 57 | console.log(stderr); |
| 58 | cb(err); |
| 59 | }); |
| 60 | }); |
| 61 | |
Adam Cozzette | 671e075 | 2017-02-23 11:32:16 -0800 | [diff] [blame^] | 62 | function getClosureBuilderCommand(exportsFile, outputFile) { |
| 63 | return './node_modules/google-closure-library/closure/bin/build/closurebuilder.py ' + |
| 64 | '--root node_modules ' + |
| 65 | '-o compiled ' + |
| 66 | '--compiler_jar node_modules/google-closure-compiler/compiler.jar ' + |
| 67 | '-i ' + exportsFile + ' ' + |
| 68 | 'map.js message.js binary/arith.js binary/constants.js binary/decoder.js ' + |
| 69 | 'binary/encoder.js binary/reader.js binary/utils.js binary/writer.js ' + |
| 70 | exportsFile + ' > ' + outputFile; |
| 71 | } |
| 72 | |
Josh Haberman | 1337486 | 2016-06-16 11:59:08 -0700 | [diff] [blame] | 73 | gulp.task('dist', ['genproto_wellknowntypes'], function (cb) { |
Josh Haberman | 55cc3aa | 2016-02-02 15:18:34 -0800 | [diff] [blame] | 74 | // TODO(haberman): minify this more aggressively. |
| 75 | // Will require proper externs/exports. |
Adam Cozzette | 671e075 | 2017-02-23 11:32:16 -0800 | [diff] [blame^] | 76 | exec(getClosureBuilderCommand('commonjs/export.js', 'google-protobuf.js'), |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 77 | function (err, stdout, stderr) { |
| 78 | console.log(stdout); |
| 79 | console.log(stderr); |
| 80 | cb(err); |
| 81 | }); |
| 82 | }); |
| 83 | |
| 84 | gulp.task('commonjs_asserts', function (cb) { |
Adam Cozzette | 671e075 | 2017-02-23 11:32:16 -0800 | [diff] [blame^] | 85 | exec('mkdir -p commonjs_out/test_node_modules && ' + |
| 86 | getClosureBuilderCommand( |
| 87 | 'commonjs/export_asserts.js', |
| 88 | 'commonjs_out/test_node_modules/closure_asserts_commonjs.js'), |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 89 | function (err, stdout, stderr) { |
| 90 | console.log(stdout); |
| 91 | console.log(stderr); |
| 92 | cb(err); |
| 93 | }); |
| 94 | }); |
| 95 | |
Josh Haberman | 3c4ce52 | 2016-04-11 15:53:25 -0700 | [diff] [blame] | 96 | gulp.task('commonjs_testdeps', function (cb) { |
Adam Cozzette | 671e075 | 2017-02-23 11:32:16 -0800 | [diff] [blame^] | 97 | exec('mkdir -p commonjs_out/test_node_modules && ' + |
| 98 | getClosureBuilderCommand( |
| 99 | 'commonjs/export_testdeps.js', |
| 100 | 'commonjs_out/test_node_modules/testdeps_commonjs.js'), |
Josh Haberman | 3c4ce52 | 2016-04-11 15:53:25 -0700 | [diff] [blame] | 101 | function (err, stdout, stderr) { |
| 102 | console.log(stdout); |
| 103 | console.log(stderr); |
| 104 | cb(err); |
| 105 | }); |
| 106 | }); |
| 107 | |
Josh Haberman | 104723f | 2016-06-16 18:40:07 -0700 | [diff] [blame] | 108 | gulp.task('make_commonjs_out', ['dist', 'genproto_commonjs', 'genproto_commonjs_wellknowntypes', 'commonjs_asserts', 'commonjs_testdeps'], function (cb) { |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 109 | // TODO(haberman): minify this more aggressively. |
| 110 | // Will require proper externs/exports. |
murgatroid99 | a862b6b | 2016-02-24 13:44:57 -0800 | [diff] [blame] | 111 | var cmd = "mkdir -p commonjs_out/binary && mkdir -p commonjs_out/test_node_modules && "; |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 112 | function addTestFile(file) { |
Josh Haberman | 59ea500 | 2016-02-05 15:07:15 -0800 | [diff] [blame] | 113 | cmd += 'node commonjs/rewrite_tests_for_commonjs.js < ' + file + |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 114 | ' > commonjs_out/' + file + '&& '; |
| 115 | } |
| 116 | |
| 117 | glob.sync('*_test.js').forEach(addTestFile); |
| 118 | glob.sync('binary/*_test.js').forEach(addTestFile); |
| 119 | |
| 120 | exec(cmd + |
Josh Haberman | 9e60036 | 2016-02-04 10:38:06 -0800 | [diff] [blame] | 121 | 'cp commonjs/jasmine.json commonjs_out/jasmine.json && ' + |
murgatroid99 | c9f8a1b | 2016-02-24 14:23:08 -0800 | [diff] [blame] | 122 | 'cp google-protobuf.js commonjs_out/test_node_modules && ' + |
| 123 | 'cp commonjs/import_test.js commonjs_out/import_test.js', |
Josh Haberman | 55cc3aa | 2016-02-02 15:18:34 -0800 | [diff] [blame] | 124 | function (err, stdout, stderr) { |
| 125 | console.log(stdout); |
| 126 | console.log(stderr); |
| 127 | cb(err); |
| 128 | }); |
| 129 | }); |
| 130 | |
| 131 | gulp.task('deps', ['genproto_closure'], function (cb) { |
Josh Haberman | e9cf31e | 2015-12-21 15:18:17 -0800 | [diff] [blame] | 132 | exec('./node_modules/google-closure-library/closure/bin/build/depswriter.py *.js binary/*.js > deps.js', |
| 133 | function (err, stdout, stderr) { |
| 134 | console.log(stdout); |
| 135 | console.log(stderr); |
| 136 | cb(err); |
| 137 | }); |
Josh Haberman | 55cc3aa | 2016-02-02 15:18:34 -0800 | [diff] [blame] | 138 | }); |
Josh Haberman | e9cf31e | 2015-12-21 15:18:17 -0800 | [diff] [blame] | 139 | |
Josh Haberman | 55cc3aa | 2016-02-02 15:18:34 -0800 | [diff] [blame] | 140 | gulp.task('test_closure', ['genproto_closure', 'deps'], function (cb) { |
Josh Haberman | e9cf31e | 2015-12-21 15:18:17 -0800 | [diff] [blame] | 141 | exec('JASMINE_CONFIG_PATH=jasmine.json ./node_modules/.bin/jasmine', |
| 142 | function (err, stdout, stderr) { |
| 143 | console.log(stdout); |
| 144 | console.log(stderr); |
| 145 | cb(err); |
| 146 | }); |
| 147 | }); |
Josh Haberman | 55cc3aa | 2016-02-02 15:18:34 -0800 | [diff] [blame] | 148 | |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 149 | gulp.task('test_commonjs', ['make_commonjs_out'], function (cb) { |
murgatroid99 | a862b6b | 2016-02-24 13:44:57 -0800 | [diff] [blame] | 150 | exec('cd commonjs_out && JASMINE_CONFIG_PATH=jasmine.json NODE_PATH=test_node_modules ../node_modules/.bin/jasmine', |
Josh Haberman | 55cc3aa | 2016-02-02 15:18:34 -0800 | [diff] [blame] | 151 | function (err, stdout, stderr) { |
| 152 | console.log(stdout); |
| 153 | console.log(stderr); |
| 154 | cb(err); |
| 155 | }); |
| 156 | }); |
Josh Haberman | 35298f9 | 2016-02-04 17:01:33 -0800 | [diff] [blame] | 157 | |
| 158 | gulp.task('test', ['test_closure', 'test_commonjs'], function(cb) { |
| 159 | cb(); |
| 160 | }); |