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
26gulp.task('genproto_closure', function (cb) {
27  exec(protoc + ' --js_out=library=testproto_libs,binary:.  -I ../src -I . *.proto ../src/google/protobuf/descriptor.proto',
28       function (err, stdout, stderr) {
29    console.log(stdout);
30    console.log(stderr);
31    cb(err);
32  });
33});
34
35gulp.task('genproto_commonjs', function (cb) {
36  exec('mkdir -p commonjs_out && ' + protoc + ' --js_out=import_style=commonjs,binary:commonjs_out -I ../src -I commonjs -I . *.proto commonjs/test*/*.proto ../src/google/protobuf/descriptor.proto',
37       function (err, stdout, stderr) {
38    console.log(stdout);
39    console.log(stderr);
40    cb(err);
41  });
42});
43
44gulp.task('genproto_commonjs_wellknowntypes', function (cb) {
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 ../src/google/protobuf/descriptor.proto',
46       function (err, stdout, stderr) {
47    console.log(stdout);
48    console.log(stderr);
49    cb(err);
50  });
51});
52
53gulp.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
62gulp.task('dist', ['genproto_wellknowntypes'], function (cb) {
63  // TODO(haberman): minify this more aggressively.
64  // Will require proper externs/exports.
65  exec('./node_modules/google-closure-library/closure/bin/calcdeps.py -i message.js -i binary/reader.js -i binary/writer.js -i commonjs/export.js -p . -p node_modules/google-closure-library/closure -o compiled --compiler_jar node_modules/google-closure-compiler/compiler.jar > google-protobuf.js',
66       function (err, stdout, stderr) {
67    console.log(stdout);
68    console.log(stderr);
69    cb(err);
70  });
71});
72
73gulp.task('commonjs_asserts', function (cb) {
74  exec('mkdir -p commonjs_out/test_node_modules && ./node_modules/google-closure-library/closure/bin/calcdeps.py -i commonjs/export_asserts.js -p . -p node_modules/google-closure-library/closure -o compiled --compiler_jar node_modules/google-closure-compiler/compiler.jar > commonjs_out/test_node_modules/closure_asserts_commonjs.js',
75       function (err, stdout, stderr) {
76    console.log(stdout);
77    console.log(stderr);
78    cb(err);
79  });
80});
81
82gulp.task('commonjs_testdeps', function (cb) {
83  exec('mkdir -p commonjs_out/test_node_modules && ./node_modules/google-closure-library/closure/bin/calcdeps.py -i commonjs/export_testdeps.js -p . -p node_modules/google-closure-library/closure -o compiled --compiler_jar node_modules/google-closure-compiler/compiler.jar > commonjs_out/test_node_modules/testdeps_commonjs.js',
84       function (err, stdout, stderr) {
85    console.log(stdout);
86    console.log(stderr);
87    cb(err);
88  });
89});
90
91gulp.task('make_commonjs_out', ['dist', 'genproto_commonjs', 'genproto_commonjs_wellknowntypes', 'commonjs_asserts', 'commonjs_testdeps'], function (cb) {
92  // TODO(haberman): minify this more aggressively.
93  // Will require proper externs/exports.
94  var cmd = "mkdir -p commonjs_out/binary && mkdir -p commonjs_out/test_node_modules && ";
95  function addTestFile(file) {
96    cmd += 'node commonjs/rewrite_tests_for_commonjs.js < ' + file +
97           ' > commonjs_out/' + file + '&& ';
98  }
99
100  glob.sync('*_test.js').forEach(addTestFile);
101  glob.sync('binary/*_test.js').forEach(addTestFile);
102
103  exec(cmd +
104       'cp commonjs/jasmine.json commonjs_out/jasmine.json && ' +
105       'cp google-protobuf.js commonjs_out/test_node_modules && ' +
106       'cp commonjs/import_test.js commonjs_out/import_test.js',
107       function (err, stdout, stderr) {
108    console.log(stdout);
109    console.log(stderr);
110    cb(err);
111  });
112});
113
114gulp.task('deps', ['genproto_closure'], function (cb) {
115  exec('./node_modules/google-closure-library/closure/bin/build/depswriter.py *.js binary/*.js > deps.js',
116       function (err, stdout, stderr) {
117    console.log(stdout);
118    console.log(stderr);
119    cb(err);
120  });
121});
122
123gulp.task('test_closure', ['genproto_closure', 'deps'], function (cb) {
124  exec('JASMINE_CONFIG_PATH=jasmine.json ./node_modules/.bin/jasmine',
125       function (err, stdout, stderr) {
126    console.log(stdout);
127    console.log(stderr);
128    cb(err);
129  });
130});
131
132gulp.task('test_commonjs', ['make_commonjs_out'], function (cb) {
133  exec('cd commonjs_out && JASMINE_CONFIG_PATH=jasmine.json NODE_PATH=test_node_modules ../node_modules/.bin/jasmine',
134       function (err, stdout, stderr) {
135    console.log(stdout);
136    console.log(stderr);
137    cb(err);
138  });
139});
140
141gulp.task('test', ['test_closure', 'test_commonjs'], function(cb) {
142  cb();
143});
144