2 * Jake JavaScript build tool
3 * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 const PROJECT_DIR = process.env.PROJECT_DIR;
21 let exec = require('child_process').execSync;
22 let fs = require('fs');
23 let util = require('util');
24 let { rule, rmRf } = require(`${PROJECT_DIR}/lib/jake`);
29 ////////////////////////////////////////////////////////////
31 file('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);
38 rule('.o', '.c', function () {
39 let cmd = util.format('cp %s %s', this.source, this.name);
40 console.log(cmd + ' task');
44 file('tmp_dep1.c', function () {
45 fs.writeFileSync('tmp_dep1.c', 'src_1');
46 console.log('tmp_dep1.c task');
49 // note that tmp_dep2.o depends on tmp_dep2.c, which is a
51 task('tmp_init', function () {
52 fs.writeFileSync('tmp_dep2.c', 'src_2');
53 console.log('tmp_dep2.c task');
55 ////////////////////////////////////////////////////////////
57 ////////////////////////////////////////////////////////////
59 file('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');
66 rule('%.oo', '%.c', function () {
67 let cmd = util.format('cp %s %s', this.source, this.name);
68 console.log(cmd + ' task');
71 ////////////////////////////////////////////////////////////
73 ////////////////////////////////////////////////////////////
74 // Pattern Rule with Folder
75 // i.e. rule('tmpbin/%.oo', 'tmpsrc/%.c', ...
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');
87 rule('tmpbin/%.oo', 'tmpsrc/%.c', function () {
88 let cmd = util.format('cp %s %s', this.source, this.name);
89 console.log(cmd + ' task');
93 file('tmpsrc/tmp_dep2.c',['tmpsrc'], function () {
94 fs.writeFileSync('tmpsrc/tmp_dep2.c', 'src/src_2');
95 console.log('tmpsrc/tmp_dep2.c task');
98 // Create static files in folder tmpsrc.
99 task('tmp_src_init', ['tmpsrc'], function () {
100 fs.writeFileSync('tmpsrc/tmp_dep1.c', 'src/src_1');
101 console.log('tmpsrc/tmp_dep1.c task');
103 ////////////////////////////////////////////////////////////
106 ////////////////////////////////////////////////////////////
107 // Namespace Test. This is a Mixed Test.
109 // - rules belonging to different namespace.
110 // - rules with folder and pattern
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');
124 namespace('rule', function () {
125 task('init', ['tmpsrc'], function () {
126 fs.writeFileSync('tmpsrc/file2.c', 'src/src_3');
127 console.log('tmpsrc/file2.c init task');
130 file('tmpsrc/dep1.c',['tmpsrc'], function () {
131 fs.writeFileSync('tmpsrc/dep1.c', 'src/src_1');
132 console.log('tmpsrc/dep1.c task');
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');
141 ////////////////////////////////////////////////////////////
143 ////////////////////////////////////////////////////////////
145 // rule('tmpbin/%.pdf', 'tmpbin/%.dvi', function() { ...
146 // rule('tmpbin/%.dvi', 'tmpsrc/%.tex', ['tmpbin'], function() { ...
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');
157 namespace('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');
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');
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');
176 ////////////////////////////////////////////////////////////
177 namespace('precedence', function () {
178 task('test', ['foo.html'], function () {
179 console.log('ran test');
182 rule('.html', '.txt', function () {
183 console.log('created html');
184 let data = fs.readFileSync(this.source);
185 fs.writeFileSync(this.name, data.toString());
189 namespace('regexPattern', function () {
190 task('test', ['foo.html'], function () {
191 console.log('ran test');
194 rule(/\.html$/, '.txt', function () {
195 console.log('created html');
196 let data = fs.readFileSync(this.source);
197 fs.writeFileSync(this.name, data.toString());
201 namespace('sourceFunction', function () {
203 let srcFunc = function (taskName) {
204 return taskName.replace(/\.[^.]+$/, '.txt');
207 task('test', ['foo.html'], function () {
208 console.log('ran test');
211 rule('.html', srcFunc, function () {
212 console.log('created html');
213 let data = fs.readFileSync(this.source);
214 fs.writeFileSync(this.name, data.toString());
218 ////////////////////////////////////////////////////////////
219 task('clean', function () {