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