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