forked from jashkenas/coffeescript
/
cake.js
175 lines (151 loc) · 4.64 KB
/
cake.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
(function() {
var CoffeeScript, atasks, cakefileDirectory, fatalError, fs, helpers, missingTask, oparse, options, optparse, path, printTasks, switches, tame, tasks, __tame_k,
__slice = Array.prototype.slice;
tame = {
Deferrals: (function() {
function _Class(_arg) {
this.continuation = _arg;
this.count = 1;
}
_Class.prototype._fulfill = function() {
if (!--this.count) return this.continuation();
};
_Class.prototype.defer = function(defer_params) {
var _this = this;
++this.count;
return function() {
var inner_params, _ref;
inner_params = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
if (defer_params != null) {
if ((_ref = defer_params.assign_fn) != null) {
_ref.apply(null, inner_params);
}
}
return _this._fulfill();
};
};
return _Class;
})()
}
__tame_k = function() {};
fs = require('fs');
path = require('path');
helpers = require('./helpers');
optparse = require('./optparse');
CoffeeScript = require('./coffee-script');
tasks = {};
atasks = {};
options = {};
switches = [];
oparse = null;
helpers.extend(global, {
task: function(name, description, action, async) {
var _ref;
if (!action) {
_ref = [description, action], action = _ref[0], description = _ref[1];
}
return tasks[name] = {
name: name,
description: description,
action: action,
async: async
};
},
atask: function(name, description, action) {
return task(name, description, action, true);
},
option: function(letter, flag, description) {
return switches.push([letter, flag, description]);
},
invoke: function(name, cb) {
var t, __tame_deferrals,
_this = this;
if (!(t = tasks[name])) missingTask(name);
(function(__tame_k) {
if (t.async) {
(function(__tame_k) {
__tame_deferrals = new tame.Deferrals(__tame_k);
t.action(options, __tame_deferrals.defer({}));
__tame_deferrals._fulfill();
})(function() {
return __tame_k();
});
} else {
t.action(options);
return __tame_k();
}
})(function() {
return cb();
});
}
});
exports.run = function(cb) {
var arg, args, __tame_deferrals, _i, _len, _ref, _results, _while,
_this = this;
global.__originalDirname = fs.realpathSync('.');
process.chdir(cakefileDirectory(__originalDirname));
args = process.argv.slice(2);
CoffeeScript.run(fs.readFileSync('Cakefile').toString(), {
filename: 'Cakefile'
});
oparse = new optparse.OptionParser(switches);
if (!args.length) return printTasks();
try {
options = oparse.parse(args);
} catch (e) {
return fatalError("" + e);
}
_ref = options.arguments;
_len = _ref.length;
_i = 0;
_while = function(__tame_k) {
var _break, _continue;
_break = __tame_k;
_continue = function() {
++_i;
return _while(__tame_k);
};
if (_i < _len) {
arg = _ref[_i];
(function(__tame_k) {
__tame_deferrals = new tame.Deferrals(__tame_k);
invoke(arg, __tame_deferrals.defer({}));
__tame_deferrals._fulfill();
})(function() {
return _continue();
});
} else {
return _break();
}
};
_while(__tame_k);
};
printTasks = function() {
var cakefilePath, desc, name, spaces, task;
cakefilePath = path.join(path.relative(__originalDirname, process.cwd()), 'Cakefile');
console.log("" + cakefilePath + " defines the following tasks:\n");
for (name in tasks) {
task = tasks[name];
spaces = 20 - name.length;
spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
desc = task.description ? "# " + task.description : '';
console.log("cake " + name + spaces + " " + desc);
}
if (switches.length) return console.log(oparse.help());
};
fatalError = function(message) {
console.error(message + '\n');
console.log('To see a list of all tasks/options, run "cake"');
return process.exit(1);
};
missingTask = function(task) {
return fatalError("No such task: " + task);
};
cakefileDirectory = function(dir) {
var parent;
if (path.existsSync(path.join(dir, 'Cakefile'))) return dir;
parent = path.normalize(path.join(dir, '..'));
if (parent !== dir) return cakefileDirectory(parent);
throw new Error("Cakefile not found in " + (process.cwd()));
};
}).call(this);