Jakefile.rule 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. var exec = require('child_process').exec
  2. , fs = require('fs')
  3. , util = require('util')
  4. , utils = require('utilities');
  5. task('default', ['tmp']);
  6. directory('tmpsrc');
  7. directory('tmpbin');
  8. ////////////////////////////////////////////////////////////
  9. // Simple Suffix Rule
  10. file('tmp', ['tmp_init', 'tmp_dep1.o', 'tmp_dep2.o'], function (params) {
  11. console.log('tmp task');
  12. var data1 = fs.readFileSync('tmp_dep1.o');
  13. var data2 = fs.readFileSync('tmp_dep2.o');
  14. fs.writeFileSync('tmp', data1 + data2);
  15. });
  16. rule('.o', '.c', function() {
  17. var cmd = util.format('cp %s %s', this.source, this.name);
  18. console.log(cmd + ' task');
  19. jake.exec([cmd], function () {
  20. complete();
  21. });
  22. }, { async : true });
  23. file('tmp_dep1.c', function () {
  24. fs.writeFile('tmp_dep1.c', 'src_1', function (err) {
  25. if (err) {
  26. throw err;
  27. }
  28. console.log('tmp_dep1.c task');
  29. complete();
  30. });
  31. }, {async: true});
  32. // note that tmp_dep2.o depends on tmp_dep2.c, which is a
  33. // static file.
  34. task('tmp_init', function () {
  35. fs.writeFile('tmp_dep2.c', 'src_2', function (err) {
  36. if (err) {
  37. throw err;
  38. }
  39. console.log('tmp_dep2.c task');
  40. complete();
  41. });
  42. }, {async: true});
  43. ////////////////////////////////////////////////////////////
  44. ////////////////////////////////////////////////////////////
  45. // Pattern Rule
  46. file('tmp_p', ['tmp_init', 'tmp_dep1.oo', 'tmp_dep2.oo'], function (params) {
  47. console.log('tmp pattern task');
  48. var data1 = fs.readFileSync('tmp_dep1.oo');
  49. var data2 = fs.readFileSync('tmp_dep2.oo');
  50. fs.writeFileSync('tmp_p', data1 + data2 + ' pattern');
  51. });
  52. rule('%.oo', '%.c', function() {
  53. var cmd = util.format('cp %s %s', this.source, this.name);
  54. console.log(cmd + ' task');
  55. jake.exec([cmd], function () {
  56. complete();
  57. });
  58. }, { async : true });
  59. ////////////////////////////////////////////////////////////
  60. ////////////////////////////////////////////////////////////
  61. // Pattern Rule with Folder
  62. // i.e. rule('tmpbin/%.oo', 'tmpsrc/%.c', ...
  63. file('tmp_pf', [
  64. 'tmp_src_init'
  65. , 'tmpbin'
  66. , 'tmpbin/tmp_dep1.oo'
  67. , 'tmpbin/tmp_dep2.oo' ], function (params) {
  68. console.log('tmp pattern folder task');
  69. var data1 = fs.readFileSync('tmpbin/tmp_dep1.oo');
  70. var data2 = fs.readFileSync('tmpbin/tmp_dep2.oo');
  71. fs.writeFileSync('tmp_pf', data1 + data2 + ' pattern folder');
  72. });
  73. rule('tmpbin/%.oo', 'tmpsrc/%.c', function() {
  74. var cmd = util.format('cp %s %s', this.source, this.name);
  75. console.log(cmd + ' task');
  76. jake.exec([cmd], function () {
  77. complete();
  78. });
  79. }, { async : true });
  80. file('tmpsrc/tmp_dep2.c',['tmpsrc'], function () {
  81. fs.writeFile('tmpsrc/tmp_dep2.c', 'src/src_2', function (err) {
  82. if (err) {
  83. throw err;
  84. }
  85. console.log('tmpsrc/tmp_dep2.c task');
  86. complete();
  87. });
  88. }, {async: true});
  89. // Create static files in folder tmpsrc.
  90. task('tmp_src_init', ['tmpsrc'], function () {
  91. fs.writeFile('tmpsrc/tmp_dep1.c', 'src/src_1', function (err) {
  92. if (err) {
  93. throw err;
  94. }
  95. console.log('tmpsrc/tmp_dep1.c task');
  96. complete();
  97. });
  98. }, {async: true});
  99. ////////////////////////////////////////////////////////////
  100. ////////////////////////////////////////////////////////////
  101. // Namespace Test. This is a Mixed Test.
  102. // Test for
  103. // - rules belonging to different namespace.
  104. // - rules with folder and pattern
  105. task('tmp_ns', [
  106. 'tmpbin'
  107. , 'rule:init'
  108. , 'tmpbin/tmp_dep2.oo' // *** This relies on a rule defined before.
  109. , 'rule:tmpbin/dep1.oo'
  110. , 'rule:tmpbin/file2.oo' ], function () {
  111. console.log('tmp pattern folder namespace task');
  112. var data1 = fs.readFileSync('tmpbin/dep1.oo');
  113. var data2 = fs.readFileSync('tmpbin/tmp_dep2.oo');
  114. var data3 = fs.readFileSync('tmpbin/file2.oo');
  115. fs.writeFileSync('tmp_ns', data1 + data2 + data3 + ' pattern folder namespace');
  116. });
  117. namespace('rule', function() {
  118. task('init', ['tmpsrc'], function () {
  119. fs.writeFile('tmpsrc/file2.c', 'src/src_3', function (err) {
  120. if (err) {
  121. throw err;
  122. }
  123. console.log('tmpsrc/file2.c init task');
  124. complete();
  125. });
  126. }, {async: true});
  127. file('tmpsrc/dep1.c',['tmpsrc'], function () {
  128. fs.writeFile('tmpsrc/dep1.c', 'src/src_1', function (err) {
  129. if (err) {
  130. throw err;
  131. }
  132. console.log('tmpsrc/dep1.c task');
  133. complete();
  134. });
  135. }, {async: true});
  136. rule('tmpbin/%.oo', 'tmpsrc/%.c', function() {
  137. var cmd = util.format('cp %s %s', this.source, this.name);
  138. console.log(cmd + ' ns task');
  139. jake.exec([cmd], function () {
  140. complete();
  141. });
  142. }, { async : true });
  143. });
  144. ////////////////////////////////////////////////////////////
  145. ////////////////////////////////////////////////////////////
  146. // Chain rule
  147. // rule('tmpbin/%.pdf', 'tmpbin/%.dvi', function() { ...
  148. // rule('tmpbin/%.dvi', 'tmpsrc/%.tex', ['tmpbin'], function() { ...
  149. task('tmp_cr', [
  150. 'chainrule:init'
  151. , 'chainrule:tmpbin/file1.pdf'
  152. , 'chainrule:tmpbin/file2.pdf' ], function () {
  153. console.log('tmp chainrule namespace task');
  154. var data1 = fs.readFileSync('tmpbin/file1.pdf');
  155. var data2 = fs.readFileSync('tmpbin/file2.pdf');
  156. fs.writeFileSync('tmp_cr', data1 + data2 + ' chainrule namespace');
  157. });
  158. namespace('chainrule', function() {
  159. task('init', ['tmpsrc', 'tmpbin'], function () {
  160. fs.writeFileSync('tmpsrc/file1.tex', 'tex1 ');
  161. fs.writeFileSync('tmpsrc/file2.tex', 'tex2 ');
  162. console.log('chainrule init task');
  163. });
  164. rule('tmpbin/%.pdf', 'tmpbin/%.dvi', function() {
  165. var cmd = util.format('cp %s %s', this.source, this.name);
  166. console.log(cmd + ' dvi->pdf task');
  167. jake.exec([cmd], function () {
  168. complete();
  169. });
  170. }, { async : true });
  171. rule('tmpbin/%.dvi', 'tmpsrc/%.tex', ['tmpbin'], function() {
  172. var cmd = util.format('cp %s %s', this.source, this.name);
  173. console.log(cmd + ' tex->dvi task');
  174. jake.exec([cmd], function () {
  175. complete();
  176. });
  177. }, { async : true });
  178. });
  179. ////////////////////////////////////////////////////////////
  180. namespace('precedence', function () {
  181. task('test', ['foo.html'], function () {
  182. console.log('ran test');
  183. });
  184. rule('.html', '.txt', function () {
  185. console.log('created html');
  186. var data = fs.readFileSync(this.source);
  187. fs.writeFileSync(this.name, data.toString());
  188. });
  189. });
  190. namespace('regexPattern', function () {
  191. task('test', ['foo.html'], function () {
  192. console.log('ran test');
  193. });
  194. rule(/\.html$/, '.txt', function () {
  195. console.log('created html');
  196. var data = fs.readFileSync(this.source);
  197. fs.writeFileSync(this.name, data.toString());
  198. });
  199. });
  200. namespace('sourceFunction', function () {
  201. var srcFunc = function (taskName) {
  202. return taskName.replace(/\.[^.]+$/, '.txt');
  203. };
  204. task('test', ['foo.html'], function () {
  205. console.log('ran test');
  206. });
  207. rule('.html', srcFunc, function () {
  208. console.log('created html');
  209. var data = fs.readFileSync(this.source);
  210. fs.writeFileSync(this.name, data.toString());
  211. });
  212. });
  213. ////////////////////////////////////////////////////////////
  214. task('clean', function () {
  215. utils.file.rmRf('./foo');
  216. utils.file.rmRf('./tmp');
  217. });