2010-02-16 20:42:10 -05:00
|
|
|
(function(){
|
2010-03-16 01:53:25 -04:00
|
|
|
var CoffeeScript, fs, helpers, no_such_task, oparse, options, optparse, path, print_tasks, switches, tasks;
|
2010-02-16 20:42:10 -05:00
|
|
|
var __hasProp = Object.prototype.hasOwnProperty;
|
2010-03-06 20:18:50 -05:00
|
|
|
// `cake` is a simplified version of [Make](http://www.gnu.org/software/make/)
|
|
|
|
// ([Rake](http://rake.rubyforge.org/), [Jake](http://github.com/280north/jake))
|
|
|
|
// for CoffeeScript. You define tasks with names and descriptions in a Cakefile,
|
|
|
|
// and can call them from the command line, or invoke them from other tasks.
|
|
|
|
// Running `cake` with no arguments will print out a list of all the tasks in the
|
|
|
|
// current directory's Cakefile.
|
|
|
|
// External dependencies.
|
2010-02-16 20:42:10 -05:00
|
|
|
fs = require('fs');
|
|
|
|
path = require('path');
|
2010-03-16 01:53:25 -04:00
|
|
|
helpers = require('./helpers').helpers;
|
2010-03-15 23:39:46 -04:00
|
|
|
optparse = require('./optparse');
|
|
|
|
CoffeeScript = require('./coffee-script');
|
2010-03-06 20:18:50 -05:00
|
|
|
// Keep track of the list of defined tasks, the accepted options, and so on.
|
2010-02-16 20:42:10 -05:00
|
|
|
tasks = {};
|
2010-02-25 21:43:42 -05:00
|
|
|
options = {};
|
|
|
|
switches = [];
|
|
|
|
oparse = null;
|
2010-03-06 20:18:50 -05:00
|
|
|
// Mixin the top-level Cake functions for Cakefiles to use directly.
|
2010-03-16 01:53:25 -04:00
|
|
|
helpers.extend(global, {
|
2010-03-06 20:18:50 -05:00
|
|
|
// Define a Cake task with a short name, a sentence description,
|
|
|
|
// and the function to run as the action itself.
|
2010-02-16 20:42:10 -05:00
|
|
|
task: function task(name, description, action) {
|
2010-03-21 07:17:58 -04:00
|
|
|
tasks[name] = {
|
2010-02-16 20:42:10 -05:00
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
action: action
|
|
|
|
};
|
2010-03-21 07:17:58 -04:00
|
|
|
return tasks[name];
|
2010-02-16 20:42:10 -05:00
|
|
|
},
|
2010-03-06 20:18:50 -05:00
|
|
|
// Define an option that the Cakefile accepts. The parsed options hash,
|
|
|
|
// containing all of the command-line options passed, will be made available
|
|
|
|
// as the first argument to the action.
|
2010-02-25 21:43:42 -05:00
|
|
|
option: function option(letter, flag, description) {
|
|
|
|
return switches.push([letter, flag, description]);
|
|
|
|
},
|
2010-03-06 20:18:50 -05:00
|
|
|
// Invoke another task in the current Cakefile.
|
2010-02-16 20:42:10 -05:00
|
|
|
invoke: function invoke(name) {
|
2010-02-17 20:37:30 -05:00
|
|
|
if (!(tasks[name])) {
|
|
|
|
no_such_task(name);
|
|
|
|
}
|
2010-02-25 21:43:42 -05:00
|
|
|
return tasks[name].action(options);
|
2010-02-16 20:42:10 -05:00
|
|
|
}
|
|
|
|
});
|
2010-03-06 20:18:50 -05:00
|
|
|
// Run `cake`. Executes all of the tasks you pass, in order. Note that Node's
|
|
|
|
// asynchrony may cause tasks to execute in a different order than you'd expect.
|
|
|
|
// If no tasks are passed, print the help screen.
|
2010-02-16 20:42:10 -05:00
|
|
|
exports.run = function run() {
|
|
|
|
return path.exists('Cakefile', function(exists) {
|
2010-02-25 23:39:14 -05:00
|
|
|
var _a, _b, _c, _d, arg, args;
|
2010-02-16 20:42:10 -05:00
|
|
|
if (!(exists)) {
|
2010-03-06 13:59:11 -05:00
|
|
|
throw new Error("Cakefile not found in " + (process.cwd()));
|
2010-02-16 20:42:10 -05:00
|
|
|
}
|
2010-03-07 13:41:15 -05:00
|
|
|
args = process.argv.slice(2, process.argv.length);
|
2010-03-07 22:17:45 -05:00
|
|
|
CoffeeScript.run(fs.readFileSync('Cakefile'), {
|
|
|
|
source: 'Cakefile'
|
|
|
|
});
|
2010-02-25 21:53:42 -05:00
|
|
|
oparse = new optparse.OptionParser(switches);
|
|
|
|
if (!(args.length)) {
|
|
|
|
return print_tasks();
|
|
|
|
}
|
|
|
|
options = oparse.parse(args);
|
|
|
|
_a = []; _b = options.arguments;
|
2010-02-25 23:39:14 -05:00
|
|
|
for (_c = 0, _d = _b.length; _c < _d; _c++) {
|
2010-02-25 21:53:42 -05:00
|
|
|
arg = _b[_c];
|
|
|
|
_a.push(invoke(arg));
|
|
|
|
}
|
|
|
|
return _a;
|
2010-03-21 07:17:58 -04:00
|
|
|
return null;
|
2010-02-16 20:42:10 -05:00
|
|
|
});
|
|
|
|
};
|
2010-03-06 20:18:50 -05:00
|
|
|
// Display the list of Cake tasks in a format similar to `rake -T`
|
2010-02-25 01:17:43 -05:00
|
|
|
print_tasks = function print_tasks() {
|
2010-02-25 21:43:42 -05:00
|
|
|
var _a, _b, _c, _d, _e, _f, i, name, spaces, task;
|
|
|
|
puts('');
|
|
|
|
_a = tasks;
|
|
|
|
for (name in _a) { if (__hasProp.call(_a, name)) {
|
|
|
|
task = _a[name];
|
|
|
|
spaces = 20 - name.length;
|
|
|
|
spaces = spaces > 0 ? (function() {
|
|
|
|
_b = []; _e = 0; _f = spaces;
|
2010-02-25 23:39:14 -05:00
|
|
|
for (_d = 0, i = _e; (_e <= _f ? i <= _f : i >= _f); (_e <= _f ? i += 1 : i -= 1), _d++) {
|
2010-02-25 21:43:42 -05:00
|
|
|
_b.push(' ');
|
|
|
|
}
|
|
|
|
return _b;
|
2010-03-21 07:17:58 -04:00
|
|
|
return null;
|
2010-02-25 21:43:42 -05:00
|
|
|
}).call(this).join('') : '';
|
2010-03-06 13:59:11 -05:00
|
|
|
puts("cake " + name + spaces + " # " + (task.description));
|
2010-02-25 01:17:43 -05:00
|
|
|
}}
|
2010-02-25 21:43:42 -05:00
|
|
|
if (switches.length) {
|
2010-03-06 13:59:11 -05:00
|
|
|
return puts(oparse.help());
|
2010-02-25 21:43:42 -05:00
|
|
|
}
|
2010-03-21 07:17:58 -04:00
|
|
|
return null;
|
2010-02-25 01:17:43 -05:00
|
|
|
};
|
|
|
|
// Print an error and exit when attempting to all an undefined task.
|
|
|
|
no_such_task = function no_such_task(task) {
|
2010-03-06 13:59:11 -05:00
|
|
|
process.stdio.writeError("No such task: \"" + task + "\"\n");
|
2010-02-25 01:17:43 -05:00
|
|
|
return process.exit(1);
|
|
|
|
};
|
2010-02-24 18:56:32 -05:00
|
|
|
})();
|