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-09-24 20:29:44 -04:00
|
|
|
helpers = require('./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-10-07 07:05:22 -04:00
|
|
|
if (!action) {
|
2010-09-28 16:47:12 -04:00
|
|
|
_ref = [description, action], action = _ref[0], description = _ref[1];
|
2010-03-26 05:25:17 -04:00
|
|
|
}
|
2010-10-20 18:19:08 -04:00
|
|
|
return tasks[name] = {
|
2010-02-16 20:42:10 -05:00
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
action: action
|
2010-10-20 18:19:08 -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-10-07 07:05:22 -04: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-10-19 11:07:10 -04:00
|
|
|
var _i, _len, _ref, _result, arg, args;
|
2010-10-07 07:05:22 -04: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-10-12 16:47:45 -04:00
|
|
|
args = process.argv.slice(2);
|
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-10-07 07:05:22 -04: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-10-21 21:51:06 -04:00
|
|
|
_ref = options.arguments;
|
2010-10-06 23:24:32 -04:00
|
|
|
_result = [];
|
2010-10-21 21:51:06 -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-10-19 11:07:10 -04:00
|
|
|
var _ref, desc, name, spaces, task;
|
2010-10-24 12:48:42 -04:00
|
|
|
console.log('');
|
2010-10-06 23:24:32 -04:00
|
|
|
for (name in _ref = tasks) {
|
|
|
|
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-10-21 19:50:36 -04:00
|
|
|
desc = task.description ? "# " + task.description : '';
|
2010-10-24 12:48:42 -04:00
|
|
|
console.log("cake " + name + spaces + " " + desc);
|
2010-07-15 21:18:35 -04:00
|
|
|
}
|
2010-10-27 22:50:20 -04:00
|
|
|
return switches.length ? console.log(oparse.help()) : void 0;
|
2010-02-25 01:17:43 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
missingTask = function(task) {
|
2010-10-24 12:48:42 -04:00
|
|
|
console.log("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);
|