cleaning and commenting cake.coffee

This commit is contained in:
Jeremy Ashkenas 2010-02-25 01:17:43 -05:00
parent 69feac3a01
commit fe32146adc
2 changed files with 46 additions and 48 deletions

View File

@ -2,15 +2,13 @@
var coffee, fs, no_such_task, path, print_tasks, tasks;
var __hasProp = Object.prototype.hasOwnProperty;
// `cake` is a simplified version of Make (Rake, 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.
fs = require('fs');
path = require('path');
coffee = require('coffee-script');
tasks = {};
no_such_task = function no_such_task(task) {
process.stdio.writeError('No such task: "' + task + '"\n');
return process.exit(1);
};
// Mixin the Cake functionality.
// Mixin the top-level Cake functions for Cakefiles to use.
process.mixin({
// Define a task with a name, a description, and the action itself.
task: function task(name, description, action) {
@ -28,6 +26,30 @@
return tasks[name].action();
}
});
// Running `cake` runs the tasks you pass asynchronously (node-style), or
// prints them out, with no arguments.
exports.run = function run() {
return path.exists('Cakefile', function(exists) {
var args;
if (!(exists)) {
throw new Error('Cakefile not found in ' + process.cwd());
}
args = process.ARGV.slice(2, process.ARGV.length);
return fs.readFile('Cakefile', function(err, source) {
var _a, _b, _c, arg;
eval(coffee.compile(source));
if (!(args.length)) {
return print_tasks();
}
_a = []; _b = args;
for (_c = 0; _c < _b.length; _c++) {
arg = _b[_c];
_a.push(invoke(arg));
}
return _a;
});
});
};
// Display the list of Cake tasks.
print_tasks = function print_tasks() {
var _a, _b, _c, _d, _e, _f, _g, i, name, spaces, task;
@ -48,33 +70,9 @@
}}
return _a;
};
// Running `cake` runs the tasks you pass asynchronously (node-style), or
// prints them out, with no arguments.
exports.run = function run() {
return path.exists('Cakefile', function(exists) {
var args;
if (!(exists)) {
throw new Error('Cakefile not found in ' + process.cwd());
}
args = process.ARGV.slice(2, process.ARGV.length);
return fs.readFile('Cakefile', function(err, source) {
var _a, _b, _c, arg;
eval(coffee.compile(source));
if (!(args.length)) {
return print_tasks();
}
_a = []; _b = args;
for (_c = 0; _c < _b.length; _c++) {
arg = _b[_c];
_a.push((function() {
if (!(tasks[arg])) {
no_such_task(arg);
}
return tasks[arg].action();
}).call(this));
}
return _a;
});
});
// Print an error and exit when attempting to all an undefined task.
no_such_task = function no_such_task(task) {
process.stdio.writeError('No such task: "' + task + '"\n');
return process.exit(1);
};
})();

View File

@ -1,4 +1,6 @@
# `cake` is a simplified version of Make (Rake, 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.
fs: require 'fs'
path: require 'path'
@ -6,11 +8,7 @@ coffee: require 'coffee-script'
tasks: {}
no_such_task: (task) ->
process.stdio.writeError('No such task: "' + task + '"\n')
process.exit(1)
# Mixin the Cake functionality.
# Mixin the top-level Cake functions for Cakefiles to use.
process.mixin {
# Define a task with a name, a description, and the action itself.
@ -23,13 +21,6 @@ process.mixin {
tasks[name].action()
}
# Display the list of Cake tasks.
print_tasks: ->
for name, task of tasks
spaces: 20 - name.length
spaces: if spaces > 0 then (' ' for i in [0..spaces]).join('') else ''
puts "cake " + name + spaces + ' # ' + task.description
# Running `cake` runs the tasks you pass asynchronously (node-style), or
# prints them out, with no arguments.
exports.run: ->
@ -39,7 +30,16 @@ exports.run: ->
fs.readFile 'Cakefile', (err, source) ->
eval coffee.compile source
return print_tasks() unless args.length
for arg in args
no_such_task arg unless tasks[arg]
tasks[arg].action()
invoke arg for arg in args
# Display the list of Cake tasks.
print_tasks: ->
for name, task of tasks
spaces: 20 - name.length
spaces: if spaces > 0 then (' ' for i in [0..spaces]).join('') else ''
puts "cake " + name + spaces + ' # ' + task.description
# Print an error and exit when attempting to all an undefined task.
no_such_task: (task) ->
process.stdio.writeError('No such task: "' + task + '"\n')
process.exit(1)