Jakefile 9.0 KB

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