2010-07-24 15:31:43 +00:00
|
|
|
(function() {
|
2011-09-12 02:25:27 +00:00
|
|
|
var CoffeeScript, findCakefilePathSync, 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;
|
2011-08-08 16:55:22 +00:00
|
|
|
if (!action) {
|
|
|
|
_ref = [description, action], action = _ref[0], description = _ref[1];
|
|
|
|
}
|
2010-10-20 22:19:08 +00:00
|
|
|
return tasks[name] = {
|
2010-02-17 01:42:10 +00:00
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
action: action
|
2010-10-20 22:19:08 +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) {
|
2011-08-07 06:59:37 +00:00
|
|
|
if (!tasks[name]) missingTask(name);
|
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() {
|
2011-09-12 02:25:27 +00:00
|
|
|
var arg, args, _i, _len, _ref, _results;
|
|
|
|
process.chdir(findCakefilePathSync(fs.realpathSync('.')));
|
|
|
|
args = process.argv.slice(2);
|
|
|
|
CoffeeScript.run(fs.readFileSync('Cakefile').toString(), {
|
|
|
|
filename: 'Cakefile'
|
2010-02-17 01:42:10 +00:00
|
|
|
});
|
2011-09-12 02:25:27 +00:00
|
|
|
oparse = new optparse.OptionParser(switches);
|
|
|
|
if (!args.length) return printTasks();
|
|
|
|
options = oparse.parse(args);
|
|
|
|
_ref = options.arguments;
|
|
|
|
_results = [];
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
arg = _ref[_i];
|
|
|
|
_results.push(invoke(arg));
|
|
|
|
}
|
|
|
|
return _results;
|
2010-02-17 01:42:10 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
printTasks = function() {
|
2010-11-22 00:53:31 +00:00
|
|
|
var desc, name, spaces, task;
|
2010-10-24 16:48:42 +00:00
|
|
|
console.log('');
|
2010-11-22 00:53:31 +00:00
|
|
|
for (name in tasks) {
|
|
|
|
task = tasks[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-10-21 23:50:36 +00:00
|
|
|
desc = task.description ? "# " + task.description : '';
|
2010-10-24 16:48:42 +00:00
|
|
|
console.log("cake " + name + spaces + " " + desc);
|
2010-07-16 01:18:35 +00:00
|
|
|
}
|
2011-08-07 06:59:37 +00:00
|
|
|
if (switches.length) return console.log(oparse.help());
|
2010-02-25 06:17:43 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
missingTask = function(task) {
|
2010-10-24 16:48:42 +00:00
|
|
|
console.log("No such task: \"" + task + "\"");
|
2010-02-25 06:17:43 +00:00
|
|
|
return process.exit(1);
|
|
|
|
};
|
2011-09-12 02:25:27 +00:00
|
|
|
findCakefilePathSync = function(curPath) {
|
|
|
|
var parent;
|
|
|
|
if (path.existsSync(path.join(curPath, 'Cakefile'))) return curPath;
|
|
|
|
parent = path.normalize(path.join(curPath, '..'));
|
|
|
|
if (parent !== curPath) return findCakefilePathSync(parent);
|
|
|
|
throw new Error("Cakefile not found in " + (process.cwd()));
|
|
|
|
};
|
2010-09-21 07:53:58 +00:00
|
|
|
}).call(this);
|