gulpfile.js 7.6 KB
Newer Older
1
var gulp = require('gulp');
2
var execFile = require('child_process').execFile;
3
var glob = require('glob');
4

5 6 7 8
function exec(command, cb) {
  execFile('sh', ['-c', command], cb);
}

9 10
var protoc = process.env.PROTOC || '../src/protoc';

11 12 13
var wellKnownTypes = [
  '../src/google/protobuf/any.proto',
  '../src/google/protobuf/api.proto',
14
  '../src/google/protobuf/compiler/plugin.proto',
15 16 17 18 19 20 21 22 23 24 25
  '../src/google/protobuf/descriptor.proto',
  '../src/google/protobuf/duration.proto',
  '../src/google/protobuf/empty.proto',
  '../src/google/protobuf/field_mask.proto',
  '../src/google/protobuf/source_context.proto',
  '../src/google/protobuf/struct.proto',
  '../src/google/protobuf/timestamp.proto',
  '../src/google/protobuf/type.proto',
  '../src/google/protobuf/wrappers.proto',
];

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
var group1Protos = [
  'data.proto',
  'test3.proto',
  'test5.proto',
  'commonjs/test6/test6.proto',
  'test8.proto',
  'testbinary.proto',
  'testempty.proto',
  'test.proto',
];

var group2Protos = [
  'proto3_test.proto',
  'test2.proto',
  'test4.proto',
  'commonjs/test7/test7.proto',
];

44
var group3Protos = [
45 46
  'test9.proto',
  'test10.proto'
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
gulp.task('genproto_well_known_types_closure', function (cb) {
  exec(protoc + ' --js_out=one_output_file_per_input_file,binary:. -I ../src -I . ' + wellKnownTypes.join(' '),
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});

gulp.task('genproto_group1_closure', function (cb) {
  exec(protoc + ' --js_out=library=testproto_libs1,binary:.  -I ../src -I . ' + group1Protos.join(' '),
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});

gulp.task('genproto_group2_closure', function (cb) {
  exec(protoc + ' --js_out=library=testproto_libs2,binary:.  -I ../src -I . -I commonjs ' + group2Protos.join(' '),
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});

gulp.task('genproto_well_known_types_commonjs', function (cb) {
  exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out -I ../src ' + wellKnownTypes.join(' '),
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});

gulp.task('genproto_group1_commonjs', function (cb) {
  exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out -I ../src -I commonjs -I . ' + group1Protos.join(' '),
88 89 90 91 92
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
Josh Haberman's avatar
Josh Haberman committed
93 94
});

95 96
gulp.task('genproto_group2_commonjs', function (cb) {
  exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out -I ../src -I commonjs -I . ' + group2Protos.join(' '),
97 98 99 100 101 102 103 104
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});

gulp.task('genproto_commonjs_wellknowntypes', function (cb) {
105
  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(' '),
106 107 108 109 110 111 112 113 114
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});

gulp.task('genproto_wellknowntypes', function (cb) {
  exec(protoc + ' --js_out=import_style=commonjs,binary:. -I ../src ' + wellKnownTypes.join(' '),
Josh Haberman's avatar
Josh Haberman committed
115 116 117 118 119 120
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});
121 122 123 124 125 126 127 128 129
gulp.task('genproto_group3_commonjs_strict', function (cb) {
  exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs_strict,binary:commonjs_out -I ../src -I commonjs -I . ' + group3Protos.join(' '),
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});

130

131 132 133 134 135 136 137 138 139 140 141
function getClosureBuilderCommand(exportsFile, outputFile) {
  return './node_modules/google-closure-library/closure/bin/build/closurebuilder.py ' +
  '--root node_modules ' +
  '-o compiled ' +
  '--compiler_jar node_modules/google-closure-compiler/compiler.jar ' +
  '-i ' + exportsFile + ' ' +
  'map.js message.js binary/arith.js binary/constants.js binary/decoder.js ' +
  'binary/encoder.js binary/reader.js binary/utils.js binary/writer.js ' +
  exportsFile  + ' > ' + outputFile;
}

142
gulp.task('dist', ['genproto_wellknowntypes'], function (cb) {
Josh Haberman's avatar
Josh Haberman committed
143 144
  // TODO(haberman): minify this more aggressively.
  // Will require proper externs/exports.
145
  exec(getClosureBuilderCommand('commonjs/export.js', 'google-protobuf.js'),
146 147 148 149 150 151 152 153
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});

gulp.task('commonjs_asserts', function (cb) {
154 155 156 157
  exec('mkdir -p commonjs_out/test_node_modules && ' +
       getClosureBuilderCommand(
           'commonjs/export_asserts.js',
           'commonjs_out/test_node_modules/closure_asserts_commonjs.js'),
158 159 160 161 162 163 164
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});

Josh Haberman's avatar
Josh Haberman committed
165
gulp.task('commonjs_testdeps', function (cb) {
166 167 168 169
  exec('mkdir -p commonjs_out/test_node_modules && ' +
       getClosureBuilderCommand(
           'commonjs/export_testdeps.js',
           'commonjs_out/test_node_modules/testdeps_commonjs.js'),
Josh Haberman's avatar
Josh Haberman committed
170 171 172 173 174 175 176
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});

177
gulp.task('make_commonjs_out', ['dist', 'genproto_well_known_types_commonjs', 'genproto_group1_commonjs', 'genproto_group2_commonjs', 'genproto_commonjs_wellknowntypes', 'commonjs_asserts', 'commonjs_testdeps', 'genproto_group3_commonjs_strict'], function (cb) {
178 179
  // TODO(haberman): minify this more aggressively.
  // Will require proper externs/exports.
180
  var cmd = "mkdir -p commonjs_out/binary && mkdir -p commonjs_out/test_node_modules && ";
181
  function addTestFile(file) {
182
    cmd += 'node commonjs/rewrite_tests_for_commonjs.js < ' + file +
183 184 185 186 187 188 189
           ' > commonjs_out/' + file + '&& ';
  }

  glob.sync('*_test.js').forEach(addTestFile);
  glob.sync('binary/*_test.js').forEach(addTestFile);

  exec(cmd +
190
       'cp commonjs/jasmine.json commonjs_out/jasmine.json && ' +
191
       'cp google-protobuf.js commonjs_out/test_node_modules && ' +
192
       'cp commonjs/strict_test.js commonjs_out/strict_test.js &&' +
193
       'cp commonjs/import_test.js commonjs_out/import_test.js',
Josh Haberman's avatar
Josh Haberman committed
194 195 196 197 198 199 200
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});

201 202
gulp.task('deps', ['genproto_well_known_types_closure', 'genproto_group1_closure', 'genproto_group2_closure'], function (cb) {
  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',
203 204 205 206 207
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
Josh Haberman's avatar
Josh Haberman committed
208
});
209

210
gulp.task('test_closure', ['genproto_well_known_types_closure', 'genproto_group1_closure', 'genproto_group2_closure', 'deps'], function (cb) {
211 212 213 214 215 216 217
  exec('JASMINE_CONFIG_PATH=jasmine.json ./node_modules/.bin/jasmine',
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});
Josh Haberman's avatar
Josh Haberman committed
218

219
gulp.task('test_commonjs', ['make_commonjs_out'], function (cb) {
220
  exec('cd commonjs_out && JASMINE_CONFIG_PATH=jasmine.json NODE_PATH=test_node_modules ../node_modules/.bin/jasmine',
Josh Haberman's avatar
Josh Haberman committed
221 222 223 224 225 226
       function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
});
227 228 229 230

gulp.task('test', ['test_closure', 'test_commonjs'], function(cb) {
  cb();
});