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
11gulp.task('genproto_closure', function (cb) {
12  exec(protoc + ' --js_out=library=testproto_libs,binary:.  -I ../src -I . *.proto ../src/google/protobuf/descriptor.proto',
13       function (err, stdout, stderr) {
14    console.log(stdout);
15    console.log(stderr);
16    cb(err);
17  });
18});
19
20gulp.task('genproto_commonjs', function (cb) {
21  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',
22       function (err, stdout, stderr) {
23    console.log(stdout);
24    console.log(stderr);
25    cb(err);
26  });
27});
28
29gulp.task('dist', function (cb) {
30  // TODO(haberman): minify this more aggressively.
31  // Will require proper externs/exports.
32  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',
33       function (err, stdout, stderr) {
34    console.log(stdout);
35    console.log(stderr);
36    cb(err);
37  });
38});
39
40gulp.task('commonjs_asserts', function (cb) {
41  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',
42       function (err, stdout, stderr) {
43    console.log(stdout);
44    console.log(stderr);
45    cb(err);
46  });
47});
48
49gulp.task('commonjs_testdeps', function (cb) {
50  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',
51       function (err, stdout, stderr) {
52    console.log(stdout);
53    console.log(stderr);
54    cb(err);
55  });
56});
57
58gulp.task('make_commonjs_out', ['dist', 'genproto_commonjs', 'commonjs_asserts', 'commonjs_testdeps'], function (cb) {
59  // TODO(haberman): minify this more aggressively.
60  // Will require proper externs/exports.
61  var cmd = "mkdir -p commonjs_out/binary && mkdir -p commonjs_out/test_node_modules && ";
62  function addTestFile(file) {
63    cmd += 'node commonjs/rewrite_tests_for_commonjs.js < ' + file +
64           ' > commonjs_out/' + file + '&& ';
65  }
66
67  glob.sync('*_test.js').forEach(addTestFile);
68  glob.sync('binary/*_test.js').forEach(addTestFile);
69
70  exec(cmd +
71       'cp commonjs/jasmine.json commonjs_out/jasmine.json && ' +
72       'cp google-protobuf.js commonjs_out/test_node_modules && ' +
73       'cp commonjs/import_test.js commonjs_out/import_test.js',
74       function (err, stdout, stderr) {
75    console.log(stdout);
76    console.log(stderr);
77    cb(err);
78  });
79});
80
81gulp.task('deps', ['genproto_closure'], function (cb) {
82  exec('./node_modules/google-closure-library/closure/bin/build/depswriter.py *.js binary/*.js > deps.js',
83       function (err, stdout, stderr) {
84    console.log(stdout);
85    console.log(stderr);
86    cb(err);
87  });
88});
89
90gulp.task('test_closure', ['genproto_closure', 'deps'], function (cb) {
91  exec('JASMINE_CONFIG_PATH=jasmine.json ./node_modules/.bin/jasmine',
92       function (err, stdout, stderr) {
93    console.log(stdout);
94    console.log(stderr);
95    cb(err);
96  });
97});
98
99gulp.task('test_commonjs', ['make_commonjs_out'], function (cb) {
100  exec('cd commonjs_out && JASMINE_CONFIG_PATH=jasmine.json NODE_PATH=test_node_modules ../node_modules/.bin/jasmine',
101       function (err, stdout, stderr) {
102    console.log(stdout);
103    console.log(stderr);
104    cb(err);
105  });
106});
107
108gulp.task('test', ['test_closure', 'test_commonjs'], function(cb) {
109  cb();
110});
111