rule.jake.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Jake JavaScript build tool
  3. * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. const PROJECT_DIR = process.env.PROJECT_DIR;
  19. let exec = require('child_process').execSync;
  20. let fs = require('fs');
  21. let util = require('util');
  22. let { rule, rmRf } = require(`${PROJECT_DIR}/lib/jake`);
  23. directory('tmpsrc');
  24. directory('tmpbin');
  25. ////////////////////////////////////////////////////////////
  26. // Simple Suffix Rule
  27. file('tmp', ['tmp_init', 'tmp_dep1.o', 'tmp_dep2.o'], function (params) {
  28. console.log('tmp task');
  29. let data1 = fs.readFileSync('tmp_dep1.o');
  30. let data2 = fs.readFileSync('tmp_dep2.o');
  31. fs.writeFileSync('tmp', data1 + data2);
  32. });
  33. rule('.o', '.c', function () {
  34. let cmd = util.format('cp %s %s', this.source, this.name);
  35. console.log(cmd + ' task');
  36. exec(cmd);
  37. });
  38. file('tmp_dep1.c', function () {
  39. fs.writeFileSync('tmp_dep1.c', 'src_1');
  40. console.log('tmp_dep1.c task');
  41. });
  42. // note that tmp_dep2.o depends on tmp_dep2.c, which is a
  43. // static file.
  44. task('tmp_init', function () {
  45. fs.writeFileSync('tmp_dep2.c', 'src_2');
  46. console.log('tmp_dep2.c task');
  47. });
  48. ////////////////////////////////////////////////////////////
  49. ////////////////////////////////////////////////////////////
  50. // Pattern Rule
  51. file('tmp_p', ['tmp_init', 'tmp_dep1.oo', 'tmp_dep2.oo'], function (params) {
  52. console.log('tmp pattern task');
  53. let data1 = fs.readFileSync('tmp_dep1.oo');
  54. let data2 = fs.readFileSync('tmp_dep2.oo');
  55. fs.writeFileSync('tmp_p', data1 + data2 + ' pattern');
  56. });
  57. rule('%.oo', '%.c', function () {
  58. let cmd = util.format('cp %s %s', this.source, this.name);
  59. console.log(cmd + ' task');
  60. exec(cmd);
  61. });
  62. ////////////////////////////////////////////////////////////
  63. ////////////////////////////////////////////////////////////
  64. // Pattern Rule with Folder
  65. // i.e. rule('tmpbin/%.oo', 'tmpsrc/%.c', ...
  66. file('tmp_pf', [
  67. 'tmp_src_init'
  68. , 'tmpbin'
  69. , 'tmpbin/tmp_dep1.oo'
  70. , 'tmpbin/tmp_dep2.oo' ], function (params) {
  71. console.log('tmp pattern folder task');
  72. let data1 = fs.readFileSync('tmpbin/tmp_dep1.oo');
  73. let data2 = fs.readFileSync('tmpbin/tmp_dep2.oo');
  74. fs.writeFileSync('tmp_pf', data1 + data2 + ' pattern folder');
  75. });
  76. rule('tmpbin/%.oo', 'tmpsrc/%.c', function () {
  77. let cmd = util.format('cp %s %s', this.source, this.name);
  78. console.log(cmd + ' task');
  79. exec(cmd);
  80. });
  81. file('tmpsrc/tmp_dep2.c',['tmpsrc'], function () {
  82. fs.writeFileSync('tmpsrc/tmp_dep2.c', 'src/src_2');
  83. console.log('tmpsrc/tmp_dep2.c task');
  84. });
  85. // Create static files in folder tmpsrc.
  86. task('tmp_src_init', ['tmpsrc'], function () {
  87. fs.writeFileSync('tmpsrc/tmp_dep1.c', 'src/src_1');
  88. console.log('tmpsrc/tmp_dep1.c task');
  89. });
  90. ////////////////////////////////////////////////////////////
  91. ////////////////////////////////////////////////////////////
  92. // Namespace Test. This is a Mixed Test.
  93. // Test for
  94. // - rules belonging to different namespace.
  95. // - rules with folder and pattern
  96. task('tmp_ns', [
  97. 'tmpbin'
  98. , 'rule:init'
  99. , 'tmpbin/tmp_dep2.oo' // *** This relies on a rule defined before.
  100. , 'rule:tmpbin/dep1.oo'
  101. , 'rule:tmpbin/file2.oo' ], function () {
  102. console.log('tmp pattern folder namespace task');
  103. let data1 = fs.readFileSync('tmpbin/dep1.oo');
  104. let data2 = fs.readFileSync('tmpbin/tmp_dep2.oo');
  105. let data3 = fs.readFileSync('tmpbin/file2.oo');
  106. fs.writeFileSync('tmp_ns', data1 + data2 + data3 + ' pattern folder namespace');
  107. });
  108. namespace('rule', function () {
  109. task('init', ['tmpsrc'], function () {
  110. fs.writeFileSync('tmpsrc/file2.c', 'src/src_3');
  111. console.log('tmpsrc/file2.c init task');
  112. });
  113. file('tmpsrc/dep1.c',['tmpsrc'], function () {
  114. fs.writeFileSync('tmpsrc/dep1.c', 'src/src_1');
  115. console.log('tmpsrc/dep1.c task');
  116. }, {async: true});
  117. rule('tmpbin/%.oo', 'tmpsrc/%.c', function () {
  118. let cmd = util.format('cp %s %s', this.source, this.name);
  119. console.log(cmd + ' ns task');
  120. exec(cmd);
  121. });
  122. });
  123. ////////////////////////////////////////////////////////////
  124. ////////////////////////////////////////////////////////////
  125. // Chain rule
  126. // rule('tmpbin/%.pdf', 'tmpbin/%.dvi', function() { ...
  127. // rule('tmpbin/%.dvi', 'tmpsrc/%.tex', ['tmpbin'], function() { ...
  128. task('tmp_cr', [
  129. 'chainrule:init'
  130. , 'chainrule:tmpbin/file1.pdf'
  131. , 'chainrule:tmpbin/file2.pdf' ], function () {
  132. console.log('tmp chainrule namespace task');
  133. let data1 = fs.readFileSync('tmpbin/file1.pdf');
  134. let data2 = fs.readFileSync('tmpbin/file2.pdf');
  135. fs.writeFileSync('tmp_cr', data1 + data2 + ' chainrule namespace');
  136. });
  137. namespace('chainrule', function () {
  138. task('init', ['tmpsrc', 'tmpbin'], function () {
  139. fs.writeFileSync('tmpsrc/file1.tex', 'tex1 ');
  140. fs.writeFileSync('tmpsrc/file2.tex', 'tex2 ');
  141. console.log('chainrule init task');
  142. });
  143. rule('tmpbin/%.pdf', 'tmpbin/%.dvi', function () {
  144. let cmd = util.format('cp %s %s', this.source, this.name);
  145. console.log(cmd + ' dvi->pdf task');
  146. exec(cmd);
  147. });
  148. rule('tmpbin/%.dvi', 'tmpsrc/%.tex', ['tmpbin'], function () {
  149. let cmd = util.format('cp %s %s', this.source, this.name);
  150. console.log(cmd + ' tex->dvi task');
  151. exec(cmd);
  152. });
  153. });
  154. ////////////////////////////////////////////////////////////
  155. namespace('precedence', function () {
  156. task('test', ['foo.html'], function () {
  157. console.log('ran test');
  158. });
  159. rule('.html', '.txt', function () {
  160. console.log('created html');
  161. let data = fs.readFileSync(this.source);
  162. fs.writeFileSync(this.name, data.toString());
  163. });
  164. });
  165. namespace('regexPattern', function () {
  166. task('test', ['foo.html'], function () {
  167. console.log('ran test');
  168. });
  169. rule(/\.html$/, '.txt', function () {
  170. console.log('created html');
  171. let data = fs.readFileSync(this.source);
  172. fs.writeFileSync(this.name, data.toString());
  173. });
  174. });
  175. namespace('sourceFunction', function () {
  176. let srcFunc = function (taskName) {
  177. return taskName.replace(/\.[^.]+$/, '.txt');
  178. };
  179. task('test', ['foo.html'], function () {
  180. console.log('ran test');
  181. });
  182. rule('.html', srcFunc, function () {
  183. console.log('created html');
  184. let data = fs.readFileSync(this.source);
  185. fs.writeFileSync(this.name, data.toString());
  186. });
  187. });
  188. ////////////////////////////////////////////////////////////
  189. task('clean', function () {
  190. rmRf('./foo');
  191. rmRf('./tmp');
  192. });