2010-07-24 11:31:43 -04:00
|
|
|
(function() {
|
2010-06-12 19:05:13 -04:00
|
|
|
var CoffeeScript, fs, helpers, missingTask, oparse, options, optparse, path, printTasks, switches, tasks;
|
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-02-16 20:42:10 -05:00
|
|
|
tasks = {};
|
2010-02-25 21:43:42 -05:00
|
|
|
options = {};
|
|
|
|
switches = [];
|
|
|
|
oparse = null;
|
2010-03-16 01:53:25 -04:00
|
|
|
helpers.extend(global, {
|
2010-05-14 23:40:04 -04:00
|
|
|
task: function(name, description, action) {
|
2010-09-20 21:56:18 -04:00
|
|
|
var _ref;
|
2010-04-04 00:20:17 -04:00
|
|
|
if (!(action)) {
|
2010-09-20 21:56:18 -04:00
|
|
|
_ref = [description, action];
|
|
|
|
action = _ref[0];
|
|
|
|
description = _ref[1];
|
2010-03-26 05:25:17 -04:00
|
|
|
}
|
2010-07-24 15:27:11 -04:00
|
|
|
return (tasks[name] = {
|
2010-02-16 20:42:10 -05:00
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
action: action
|
2010-07-24 15:27:11 -04:00
|
|
|
});
|
2010-02-16 20:42:10 -05:00
|
|
|
},
|
2010-05-14 23:40:04 -04:00
|
|
|
option: function(letter, flag, description) {
|
2010-02-25 21:43:42 -05:00
|
|
|
return switches.push([letter, flag, description]);
|
|
|
|
},
|
2010-05-14 23:40:04 -04:00
|
|
|
invoke: function(name) {
|
2010-02-17 20:37:30 -05:00
|
|
|
if (!(tasks[name])) {
|
2010-06-12 19:05:13 -04:00
|
|
|
missingTask(name);
|
2010-02-17 20:37:30 -05:00
|
|
|
}
|
2010-02-25 21:43:42 -05:00
|
|
|
return tasks[name].action(options);
|
2010-02-16 20:42:10 -05:00
|
|
|
}
|
|
|
|
});
|
2010-05-14 23:40:04 -04:00
|
|
|
exports.run = function() {
|
2010-02-16 20:42:10 -05:00
|
|
|
return path.exists('Cakefile', function(exists) {
|
2010-09-20 22:13:08 -04:00
|
|
|
var _i, _len, _ref, _result, arg, args;
|
2010-02-16 20:42:10 -05:00
|
|
|
if (!(exists)) {
|
2010-08-14 17:25:29 -04:00
|
|
|
throw new Error("Cakefile not found in " + (process.cwd()));
|
2010-02-16 20:42:10 -05:00
|
|
|
}
|
2010-03-30 20:06:44 -04:00
|
|
|
args = process.argv.slice(2, process.argv.length);
|
2010-05-15 00:34:14 -04:00
|
|
|
CoffeeScript.run(fs.readFileSync('Cakefile').toString(), {
|
2010-07-30 20:37:12 -04:00
|
|
|
fileName: 'Cakefile'
|
2010-03-07 22:17:45 -05:00
|
|
|
});
|
2010-08-21 19:16:02 -04:00
|
|
|
oparse = new optparse.OptionParser(switches);
|
2010-02-25 21:53:42 -05:00
|
|
|
if (!(args.length)) {
|
2010-06-12 19:05:13 -04:00
|
|
|
return printTasks();
|
2010-02-25 21:53:42 -05:00
|
|
|
}
|
|
|
|
options = oparse.parse(args);
|
2010-09-20 21:56:18 -04:00
|
|
|
_result = []; _ref = options.arguments;
|
2010-09-20 22:13:08 -04:00
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
2010-09-20 21:56:18 -04:00
|
|
|
arg = _ref[_i];
|
2010-09-19 08:29:15 -04:00
|
|
|
_result.push(invoke(arg));
|
2010-02-25 21:53:42 -05:00
|
|
|
}
|
2010-09-19 08:29:15 -04:00
|
|
|
return _result;
|
2010-02-16 20:42:10 -05:00
|
|
|
});
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
printTasks = function() {
|
2010-09-21 23:17:43 -04:00
|
|
|
var _ref, desc, name, spaces, task;
|
2010-02-25 21:43:42 -05:00
|
|
|
puts('');
|
2010-09-20 21:56:18 -04:00
|
|
|
_ref = tasks;
|
|
|
|
for (name in _ref) {
|
|
|
|
task = _ref[name];
|
2010-02-25 21:43:42 -05:00
|
|
|
spaces = 20 - name.length;
|
2010-09-21 23:17:43 -04:00
|
|
|
spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
|
2010-08-07 08:02:16 -04:00
|
|
|
desc = task.description ? ("# " + (task.description)) : '';
|
2010-08-14 17:25:29 -04:00
|
|
|
puts("cake " + (name) + (spaces) + " " + (desc));
|
2010-07-15 21:18:35 -04:00
|
|
|
}
|
2010-09-22 00:41:17 -04:00
|
|
|
return switches.length ? puts(oparse.help()) : null;
|
2010-02-25 01:17:43 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
missingTask = function(task) {
|
2010-08-14 17:25:29 -04:00
|
|
|
puts("No such task: \"" + (task) + "\"");
|
2010-02-25 01:17:43 -05:00
|
|
|
return process.exit(1);
|
|
|
|
};
|
2010-09-21 03:53:58 -04:00
|
|
|
}).call(this);
|