jakefile.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. let fs = require('fs');
  2. let Q = require('q');
  3. desc('The default t.');
  4. task('default', function () {
  5. console.log('default task');
  6. });
  7. desc('No action.');
  8. task({'noAction': ['default']});
  9. desc('No action, no prereqs.');
  10. task('noActionNoPrereqs');
  11. desc('Top-level zerbofrangazoomy task');
  12. task('zerbofrangazoomy', function () {
  13. console.log('Whaaaaaaaa? Ran the zerbofrangazoomy task!');
  14. });
  15. desc('Task that throws');
  16. task('throwy', function () {
  17. let errorListener = function (err) {
  18. console.log('Emitted');
  19. console.log(err.toString());
  20. jake.removeListener('error', errorListener);
  21. };
  22. jake.on('error', errorListener);
  23. throw new Error('I am bad');
  24. });
  25. desc('Task that rejects a Promise');
  26. task('promiseRejecter', function () {
  27. const originalOption = jake.program.opts['allow-rejection'];
  28. const errorListener = function (err) {
  29. console.log(err.toString());
  30. jake.removeListener('error', errorListener);
  31. jake.program.opts['allow-rejection'] = originalOption; // Restore original 'allow-rejection' option
  32. };
  33. jake.on('error', errorListener);
  34. jake.program.opts['allow-rejection'] = false; // Do not allow rejection so the rejection is passed to error handlers
  35. Promise.reject('<promise rejected on purpose>');
  36. });
  37. desc('Accepts args and env vars.');
  38. task('argsEnvVars', function () {
  39. let res = {
  40. args: arguments
  41. , env: {
  42. foo: process.env.foo
  43. , baz: process.env.baz
  44. }
  45. };
  46. console.log(JSON.stringify(res));
  47. });
  48. namespace('foo', function () {
  49. desc('The foo:bar t.');
  50. task('bar', function () {
  51. if (arguments.length) {
  52. console.log('foo:bar[' +
  53. Array.prototype.join.call(arguments, ',') +
  54. '] task');
  55. }
  56. else {
  57. console.log('foo:bar task');
  58. }
  59. });
  60. desc('The foo:baz task, calls foo:bar as a prerequisite.');
  61. task('baz', ['foo:bar'], function () {
  62. console.log('foo:baz task');
  63. });
  64. desc('The foo:qux task, calls foo:bar with cmdline args as a prerequisite.');
  65. task('qux', ['foo:bar[asdf,qwer]'], function () {
  66. console.log('foo:qux task');
  67. });
  68. desc('The foo:frang task,`invokes` foo:bar with passed args as a prerequisite.');
  69. task('frang', function () {
  70. let t = jake.Task['foo:bar'];
  71. // Do args pass-through
  72. t.invoke.apply(t, arguments);
  73. t.on('complete', () => {
  74. console.log('foo:frang task');
  75. });
  76. });
  77. desc('The foo:zerb task, `executes` foo:bar with passed args as a prerequisite.');
  78. task('zerb', function () {
  79. let t = jake.Task['foo:bar'];
  80. // Do args pass-through
  81. t.execute.apply(t, arguments);
  82. t.on('complete', () => {
  83. console.log('foo:zerb task');
  84. });
  85. });
  86. desc('The foo:zoobie task, has no prerequisites.');
  87. task('zoobie', function () {
  88. console.log('foo:zoobie task');
  89. });
  90. desc('The foo:voom task, run the foo:zoobie task repeatedly.');
  91. task('voom', function () {
  92. let t = jake.Task['foo:bar'];
  93. t.on('complete', function () {
  94. console.log('complete');
  95. });
  96. t.execute.apply(t);
  97. t.execute.apply(t);
  98. });
  99. desc('The foo:asdf task, has the same prereq twice.');
  100. task('asdf', ['foo:bar', 'foo:baz'], function () {
  101. console.log('foo:asdf task');
  102. });
  103. });
  104. namespace('bar', function () {
  105. desc('The bar:foo task, has no prerequisites, is async, returns Promise which resolves.');
  106. task('foo', async function () {
  107. return new Promise((resolve, reject) => {
  108. console.log('bar:foo task');
  109. resolve();
  110. });
  111. });
  112. desc('The bar:promise task has no prerequisites, is async, returns Q-based promise.');
  113. task('promise', function () {
  114. return Q()
  115. .then(function () {
  116. console.log('bar:promise task');
  117. return 123654;
  118. });
  119. });
  120. desc('The bar:dependOnpromise task waits for a promise based async test');
  121. task('dependOnpromise', ['promise'], function () {
  122. console.log('bar:dependOnpromise task saw value', jake.Task["bar:promise"].value);
  123. });
  124. desc('The bar:brokenPromise task is a failing Q-promise based async task.');
  125. task('brokenPromise', function () {
  126. return Q()
  127. .then(function () {
  128. throw new Error("nom nom nom");
  129. });
  130. });
  131. desc('The bar:bar task, has the async bar:foo task as a prerequisite.');
  132. task('bar', ['bar:foo'], function () {
  133. console.log('bar:bar task');
  134. });
  135. });
  136. namespace('hoge', function () {
  137. desc('The hoge:hoge task, has no prerequisites.');
  138. task('hoge', function () {
  139. console.log('hoge:hoge task');
  140. });
  141. desc('The hoge:piyo task, has no prerequisites.');
  142. task('piyo', function () {
  143. console.log('hoge:piyo task');
  144. });
  145. desc('The hoge:fuga task, has hoge:hoge and hoge:piyo as prerequisites.');
  146. task('fuga', ['hoge:hoge', 'hoge:piyo'], function () {
  147. console.log('hoge:fuga task');
  148. });
  149. desc('The hoge:charan task, has hoge:fuga as a prerequisite.');
  150. task('charan', ['hoge:fuga'], function () {
  151. console.log('hoge:charan task');
  152. });
  153. desc('The hoge:gero task, has hoge:fuga as a prerequisite.');
  154. task('gero', ['hoge:fuga'], function () {
  155. console.log('hoge:gero task');
  156. });
  157. desc('The hoge:kira task, has hoge:charan and hoge:gero as prerequisites.');
  158. task('kira', ['hoge:charan', 'hoge:gero'], function () {
  159. console.log('hoge:kira task');
  160. });
  161. });
  162. namespace('fileTest', function () {
  163. directory('foo');
  164. desc('File task, concatenating two files together');
  165. file('foo/concat.txt', ['fileTest:foo', 'fileTest:foo/src1.txt', 'fileTest:foo/src2.txt'], function () {
  166. console.log('fileTest:foo/concat.txt task');
  167. let data1 = fs.readFileSync('foo/src1.txt');
  168. let data2 = fs.readFileSync('foo/src2.txt');
  169. fs.writeFileSync('foo/concat.txt', data1 + data2);
  170. });
  171. desc('File task, async creation with writeFile');
  172. file('foo/src1.txt', function () {
  173. return new Promise(function (resolve, reject) {
  174. fs.writeFile('foo/src1.txt', 'src1', function (err) {
  175. if (err) {
  176. reject(err);
  177. }
  178. else {
  179. console.log('fileTest:foo/src1.txt task');
  180. resolve();
  181. }
  182. });
  183. });
  184. });
  185. desc('File task, sync creation with writeFileSync');
  186. file('foo/src2.txt', ['default'], function () {
  187. fs.writeFileSync('foo/src2.txt', 'src2');
  188. console.log('fileTest:foo/src2.txt task');
  189. });
  190. desc('File task, do not run unless the prereq file changes');
  191. file('foo/from-src1.txt', ['fileTest:foo', 'fileTest:foo/src1.txt'], function () {
  192. let data = fs.readFileSync('foo/src1.txt').toString();
  193. fs.writeFileSync('foo/from-src1.txt', data);
  194. console.log('fileTest:foo/from-src1.txt task');
  195. });
  196. desc('File task, run if the prereq file changes');
  197. task('touch-prereq', function () {
  198. fs.writeFileSync('foo/prereq.txt', 'UPDATED');
  199. });
  200. desc('File task, has a preexisting file (with no associated task) as a prereq');
  201. file('foo/from-prereq.txt', ['fileTest:foo', 'foo/prereq.txt'], function () {
  202. let data = fs.readFileSync('foo/prereq.txt');
  203. fs.writeFileSync('foo/from-prereq.txt', data);
  204. console.log('fileTest:foo/from-prereq.txt task');
  205. });
  206. directory('foo/bar/baz');
  207. desc('Write a file in a nested subdirectory');
  208. file('foo/bar/baz/bamf.txt', ['foo/bar/baz'], function () {
  209. fs.writeFileSync('foo/bar/baz/bamf.txt', 'w00t');
  210. });
  211. file('foo/output1.txt', ['foo'], () => {
  212. fs.writeFileSync('foo/output1.txt', 'Contents of foo/output1.txt');
  213. });
  214. file('foo/output2a.txt', ['foo/output1.txt'], () => {
  215. fs.writeFileSync('foo/output2a.txt', 'Contents of foo/output2a.txt');
  216. });
  217. file('foo/output2b.txt', ['foo/output1.txt'], () => {
  218. fs.writeFileSync('foo/output2b.txt', 'Contents of foo/output2b.txt');
  219. });
  220. file('foo/output3.txt', [ 'foo/output2a.txt', 'foo/output2b.txt'], () => {
  221. fs.writeFileSync('foo/output3.txt', 'w00t');
  222. });
  223. });
  224. task('blammo');
  225. // Define task
  226. task('voom', ['blammo'], function () {
  227. console.log(this.prereqs.length);
  228. });
  229. // Modify, add a prereq
  230. task('voom', ['noActionNoPrereqs']);
  231. namespace('vronk', function () {
  232. task('groo', function () {
  233. let t = jake.Task['vronk:zong'];
  234. t.addListener('error', function (e) {
  235. console.log(e.message);
  236. });
  237. t.invoke();
  238. });
  239. task('zong', function () {
  240. throw new Error('OMFGZONG');
  241. });
  242. });
  243. // define namespace
  244. namespace('one', function () {
  245. task('one', function () {
  246. console.log('one:one');
  247. });
  248. });
  249. // modify namespace (add task)
  250. namespace('one', function () {
  251. task('two', ['one:one'], function () {
  252. console.log('one:two');
  253. });
  254. });
  255. task('selfdepconst', [], function () {
  256. task('selfdep', ['selfdep'], function () {
  257. console.log("I made a task that depends on itself");
  258. });
  259. });
  260. task('selfdepdyn', function () {
  261. task('selfdeppar', [], {concurrency: 2}, function () {
  262. console.log("I will depend on myself and will fail at runtime");
  263. });
  264. task('selfdeppar', ['selfdeppar']);
  265. jake.Task['selfdeppar'].invoke();
  266. });
  267. namespace("large", function () {
  268. task("leaf", function () {
  269. console.log("large:leaf");
  270. });
  271. const same = [];
  272. for (let i = 0; i < 2000; i++) {
  273. same.push("leaf");
  274. }
  275. desc("Task with a large number of same prereqs");
  276. task("same", same, { concurrency: 2 }, function () {
  277. console.log("large:same");
  278. });
  279. const different = [];
  280. for (let i = 0; i < 2000; i++) {
  281. const name = "leaf-" + i;
  282. task(name, function () {
  283. if (name === "leaf-12" || name === "leaf-123") {
  284. console.log(name);
  285. }
  286. });
  287. different.push(name);
  288. }
  289. desc("Task with a large number of different prereqs");
  290. task("different", different, { concurrency: 2 } , function () {
  291. console.log("large:different");
  292. });
  293. });