gulpfile.js 8.5 KB

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