1let fs = require('fs');
2let Q = require('q');
3
4desc('The default t.');
5task('default', function () {
6  console.log('default task');
7});
8
9desc('No action.');
10task({'noAction': ['default']});
11
12desc('No action, no prereqs.');
13task('noActionNoPrereqs');
14
15desc('Top-level zerbofrangazoomy task');
16task('zerbofrangazoomy', function () {
17  console.log('Whaaaaaaaa? Ran the zerbofrangazoomy task!');
18});
19
20desc('Task that throws');
21task('throwy', function () {
22  let errorListener = function (err) {
23    console.log('Emitted');
24    console.log(err.toString());
25
26    jake.removeListener('error', errorListener);
27  };
28
29  jake.on('error', errorListener);
30
31  throw new Error('I am bad');
32});
33
34desc('Task that rejects a Promise');
35task('promiseRejecter', function () {
36  const originalOption = jake.program.opts['allow-rejection'];
37
38  const errorListener = function (err) {
39    console.log(err.toString());
40    jake.removeListener('error', errorListener);
41    jake.program.opts['allow-rejection'] = originalOption; // Restore original 'allow-rejection' option
42  };
43  jake.on('error', errorListener);
44
45  jake.program.opts['allow-rejection'] = false; // Do not allow rejection so the rejection is passed to error handlers
46
47  Promise.reject('<promise rejected on purpose>');
48});
49
50desc('Accepts args and env vars.');
51task('argsEnvVars', function () {
52  let res = {
53    args: arguments
54    , env: {
55      foo: process.env.foo
56      , baz: process.env.baz
57    }
58  };
59  console.log(JSON.stringify(res));
60});
61
62namespace('foo', function () {
63  desc('The foo:bar t.');
64  task('bar', function () {
65    if (arguments.length) {
66      console.log('foo:bar[' +
67          Array.prototype.join.call(arguments, ',') +
68          '] task');
69    }
70    else {
71      console.log('foo:bar task');
72    }
73  });
74
75  desc('The foo:baz task, calls foo:bar as a prerequisite.');
76  task('baz', ['foo:bar'], function () {
77    console.log('foo:baz task');
78  });
79
80  desc('The foo:qux task, calls foo:bar with cmdline args as a prerequisite.');
81  task('qux', ['foo:bar[asdf,qwer]'], function () {
82    console.log('foo:qux task');
83  });
84
85  desc('The foo:frang task,`invokes` foo:bar with passed args as a prerequisite.');
86  task('frang', function () {
87    let t = jake.Task['foo:bar'];
88    // Do args pass-through
89    t.invoke.apply(t, arguments);
90    t.on('complete', () => {
91      console.log('foo:frang task');
92    });
93  });
94
95  desc('The foo:zerb task, `executes` foo:bar with passed args as a prerequisite.');
96  task('zerb', function () {
97    let t = jake.Task['foo:bar'];
98    // Do args pass-through
99    t.execute.apply(t, arguments);
100    t.on('complete', () => {
101      console.log('foo:zerb task');
102    });
103  });
104
105  desc('The foo:zoobie task, has no prerequisites.');
106  task('zoobie', function () {
107    console.log('foo:zoobie task');
108  });
109
110  desc('The foo:voom task, run the foo:zoobie task repeatedly.');
111  task('voom', function () {
112    let t = jake.Task['foo:bar'];
113    t.on('complete', function () {
114      console.log('complete');
115    });
116    t.execute.apply(t);
117    t.execute.apply(t);
118  });
119
120  desc('The foo:asdf task, has the same prereq twice.');
121  task('asdf', ['foo:bar', 'foo:baz'], function () {
122    console.log('foo:asdf task');
123  });
124
125});
126
127namespace('bar', function () {
128  desc('The bar:foo task, has no prerequisites, is async, returns Promise which resolves.');
129  task('foo', async function () {
130    return new Promise((resolve, reject) => {
131      console.log('bar:foo task');
132      resolve();
133    });
134  });
135
136  desc('The bar:promise task has no prerequisites, is async, returns Q-based promise.');
137  task('promise', function () {
138    return Q()
139      .then(function () {
140        console.log('bar:promise task');
141        return 123654;
142      });
143  });
144
145  desc('The bar:dependOnpromise task waits for a promise based async test');
146  task('dependOnpromise', ['promise'], function () {
147    console.log('bar:dependOnpromise task saw value', jake.Task["bar:promise"].value);
148  });
149
150  desc('The bar:brokenPromise task is a failing Q-promise based async task.');
151  task('brokenPromise', function () {
152    return Q()
153      .then(function () {
154        throw new Error("nom nom nom");
155      });
156  });
157
158  desc('The bar:bar task, has the async bar:foo task as a prerequisite.');
159  task('bar', ['bar:foo'], function () {
160    console.log('bar:bar task');
161  });
162
163});
164
165namespace('hoge', function () {
166  desc('The hoge:hoge task, has no prerequisites.');
167  task('hoge', function () {
168    console.log('hoge:hoge task');
169  });
170
171  desc('The hoge:piyo task, has no prerequisites.');
172  task('piyo', function () {
173    console.log('hoge:piyo task');
174  });
175
176  desc('The hoge:fuga task, has hoge:hoge and hoge:piyo as prerequisites.');
177  task('fuga', ['hoge:hoge', 'hoge:piyo'], function () {
178    console.log('hoge:fuga task');
179  });
180
181  desc('The hoge:charan task, has hoge:fuga as a prerequisite.');
182  task('charan', ['hoge:fuga'], function () {
183    console.log('hoge:charan task');
184  });
185
186  desc('The hoge:gero task, has hoge:fuga as a prerequisite.');
187  task('gero', ['hoge:fuga'], function () {
188    console.log('hoge:gero task');
189  });
190
191  desc('The hoge:kira task, has hoge:charan and hoge:gero as prerequisites.');
192  task('kira', ['hoge:charan', 'hoge:gero'], function () {
193    console.log('hoge:kira task');
194  });
195
196});
197
198namespace('fileTest', function () {
199  directory('foo');
200
201  desc('File task, concatenating two files together');
202  file('foo/concat.txt', ['fileTest:foo', 'fileTest:foo/src1.txt', 'fileTest:foo/src2.txt'], function () {
203    console.log('fileTest:foo/concat.txt task');
204    let data1 = fs.readFileSync('foo/src1.txt');
205    let data2 = fs.readFileSync('foo/src2.txt');
206    fs.writeFileSync('foo/concat.txt', data1 + data2);
207  });
208
209  desc('File task, async creation with writeFile');
210  file('foo/src1.txt', function () {
211    return new Promise(function (resolve, reject) {
212      fs.writeFile('foo/src1.txt', 'src1', function (err) {
213        if (err) {
214          reject(err);
215        }
216        else {
217          console.log('fileTest:foo/src1.txt task');
218          resolve();
219        }
220      });
221    });
222  });
223
224  desc('File task, sync creation with writeFileSync');
225  file('foo/src2.txt', ['default'], function () {
226    fs.writeFileSync('foo/src2.txt', 'src2');
227    console.log('fileTest:foo/src2.txt task');
228  });
229
230  desc('File task, do not run unless the prereq file changes');
231  file('foo/from-src1.txt', ['fileTest:foo', 'fileTest:foo/src1.txt'], function () {
232    let data = fs.readFileSync('foo/src1.txt').toString();
233    fs.writeFileSync('foo/from-src1.txt', data);
234    console.log('fileTest:foo/from-src1.txt task');
235  });
236
237  desc('File task, run if the prereq file changes');
238  task('touch-prereq', function () {
239    fs.writeFileSync('foo/prereq.txt', 'UPDATED');
240  });
241
242  desc('File task, has a preexisting file (with no associated task) as a prereq');
243  file('foo/from-prereq.txt', ['fileTest:foo', 'foo/prereq.txt'], function () {
244    let data = fs.readFileSync('foo/prereq.txt');
245    fs.writeFileSync('foo/from-prereq.txt', data);
246    console.log('fileTest:foo/from-prereq.txt task');
247  });
248
249  directory('foo/bar/baz');
250
251  desc('Write a file in a nested subdirectory');
252  file('foo/bar/baz/bamf.txt', ['foo/bar/baz'], function () {
253    fs.writeFileSync('foo/bar/baz/bamf.txt', 'w00t');
254  });
255
256  file('foo/output1.txt', ['foo'], () => {
257    fs.writeFileSync('foo/output1.txt', 'Contents of foo/output1.txt');
258  });
259
260  file('foo/output2a.txt', ['foo/output1.txt'], () => {
261    fs.writeFileSync('foo/output2a.txt', 'Contents of foo/output2a.txt');
262  });
263
264  file('foo/output2b.txt', ['foo/output1.txt'], () => {
265    fs.writeFileSync('foo/output2b.txt', 'Contents of foo/output2b.txt');
266  });
267
268  file('foo/output3.txt', [ 'foo/output2a.txt', 'foo/output2b.txt'], () => {
269    fs.writeFileSync('foo/output3.txt', 'w00t');
270  });
271});
272
273task('blammo');
274// Define task
275task('voom', ['blammo'], function () {
276  console.log(this.prereqs.length);
277});
278
279// Modify, add a prereq
280task('voom', ['noActionNoPrereqs']);
281
282namespace('vronk', function () {
283  task('groo', function () {
284    let t = jake.Task['vronk:zong'];
285    t.addListener('error', function (e) {
286      console.log(e.message);
287    });
288    t.invoke();
289  });
290  task('zong', function () {
291    throw new Error('OMFGZONG');
292  });
293});
294
295// define namespace
296namespace('one', function () {
297  task('one', function () {
298    console.log('one:one');
299  });
300});
301
302// modify namespace (add task)
303namespace('one', function () {
304  task('two', ['one:one'], function () {
305    console.log('one:two');
306  });
307});
308
309task('selfdepconst', [], function () {
310  task('selfdep', ['selfdep'], function () {
311    console.log("I made a task that depends on itself");
312  });
313});
314task('selfdepdyn', function () {
315  task('selfdeppar', [], {concurrency: 2}, function () {
316    console.log("I will depend on myself and will fail at runtime");
317  });
318  task('selfdeppar', ['selfdeppar']);
319  jake.Task['selfdeppar'].invoke();
320});
321
322namespace("large", function () {
323  task("leaf", function () {
324    console.log("large:leaf");
325  });
326
327  const same = [];
328  for (let i = 0; i < 2000; i++) {
329    same.push("leaf");
330  }
331
332  desc("Task with a large number of same prereqs");
333  task("same", same, { concurrency: 2 }, function () {
334    console.log("large:same");
335  });
336
337  const different = [];
338  for (let i = 0; i < 2000; i++) {
339    const name = "leaf-" + i;
340    task(name, function () {
341      if (name === "leaf-12" || name === "leaf-123") {
342        console.log(name);
343      }
344    });
345    different.push(name);
346  }
347
348  desc("Task with a large number of different prereqs");
349  task("different", different, { concurrency: 2 } , function () {
350    console.log("large:different");
351  });
352});
353