forked from jashkenas/coffeescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCakefile.js
342 lines (301 loc) · 11 KB
/
Cakefile.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Generated by CoffeeScript 1.6.2
(function() {
var CoffeeScript, bold, build, exec, fs, green, header, helpers, log, path, red, reset, run, runTests, spawn, _ref;
fs = require('fs');
path = require('path');
CoffeeScript = require('./lib/coffee-script');
_ref = require('child_process'), spawn = _ref.spawn, exec = _ref.exec;
helpers = require('./lib/coffee-script/helpers');
bold = red = green = reset = '';
if (!process.env.NODE_DISABLE_COLORS) {
bold = '\x1B[0;1m';
red = '\x1B[0;31m';
green = '\x1B[0;32m';
reset = '\x1B[0m';
}
header = "/**\n * CoffeeScript Compiler v" + CoffeeScript.VERSION + "\n * http://coffeescript.org\n *\n * Copyright 2011, Jeremy Ashkenas\n * Released under the MIT License\n */";
build = function(cb) {
var file, files;
files = fs.readdirSync('src');
files = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = files.length; _i < _len; _i++) {
file = files[_i];
if (file.match(/\.(lit)?coffee$/)) {
_results.push('src/' + file);
}
}
return _results;
})();
return run(['-c', '-o', 'lib/coffee-script'].concat(files), cb);
};
run = function(args, cb) {
var proc;
proc = spawn('node', ['bin/coffee'].concat(args));
proc.stderr.on('data', function(buffer) {
return console.log(buffer.toString());
});
return proc.on('exit', function(status) {
if (status !== 0) {
process.exit(1);
}
if (typeof cb === 'function') {
return cb();
}
});
};
log = function(message, color, explanation) {
return console.log(color + message + reset + ' ' + (explanation || ''));
};
option('-p', '--prefix [DIR]', 'set the installation prefix for `cake install`');
task('install', 'install CoffeeScript into /usr/local (or --prefix)', function(options) {
var base, bin, lib, node;
base = options.prefix || '/usr/local';
lib = "" + base + "/lib/coffee-script";
bin = "" + base + "/bin";
node = "~/.node_libraries/coffee-script";
console.log("Installing CoffeeScript to " + lib);
console.log("Linking to " + node);
console.log("Linking 'coffee' to " + bin + "/coffee");
return exec(["mkdir -p " + lib + " " + bin, "cp -rf bin lib LICENSE README package.json src " + lib, "ln -sfn " + lib + "/bin/coffee " + bin + "/coffee", "ln -sfn " + lib + "/bin/cake " + bin + "/cake", "mkdir -p ~/.node_libraries", "ln -sfn " + lib + "/lib/coffee-script " + node].join(' && '), function(err, stdout, stderr) {
if (err) {
return console.log(stderr.trim());
} else {
return log('done', green);
}
});
});
task('build', 'build the CoffeeScript language from source', build);
task('build:full', 'rebuild the source twice, and run the tests', function() {
return build(function() {
return build(function() {
var csDir, csPath, mod;
csPath = './lib/coffee-script';
csDir = path.dirname(require.resolve(csPath));
for (mod in require.cache) {
if (csDir === mod.slice(0, csDir.length)) {
delete require.cache[mod];
}
}
if (!runTests(require(csPath))) {
return process.exit(1);
}
});
});
});
task('build:parser', 'rebuild the Jison parser (run build first)', function() {
var parser;
helpers.extend(global, require('util'));
require('jison');
parser = require('./lib/coffee-script/grammar').parser;
return fs.writeFile('lib/coffee-script/parser.js', parser.generate());
});
task('build:ultraviolet', 'build and install the Ultraviolet syntax highlighter', function() {
return exec('plist2syntax ../coffee-script-tmbundle/Syntaxes/CoffeeScript.tmLanguage', function(err) {
if (err) {
throw err;
}
return exec('sudo mv coffeescript.yaml /usr/local/lib/ruby/gems/1.8/gems/ultraviolet-0.10.2/syntax/coffeescript.syntax');
});
});
task('build:browser', 'rebuild the merged script for inclusion in the browser', function() {
var code, name, _i, _len, _ref1;
code = '';
_ref1 = ['helpers', 'rewriter', 'lexer', 'parser', 'scope', 'nodes', 'sourcemap', 'coffee-script', 'browser'];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
name = _ref1[_i];
code += "require['./" + name + "'] = (function() {\n var exports = {}, module = {exports: exports};\n " + (fs.readFileSync("lib/coffee-script/" + name + ".js")) + "\n return module.exports;\n})();";
}
code = "(function(root) {\n var CoffeeScript = function() {\n function require(path){ return require[path]; }\n " + code + "\n return require['./coffee-script'];\n }();\n\n if (typeof define === 'function' && define.amd) {\n define(function() { return CoffeeScript; });\n } else {\n root.CoffeeScript = CoffeeScript;\n }\n}(this));";
if (process.env.MINIFY !== 'false') {
code = require('uglify-js').minify(code, {
fromString: true
}).code;
}
fs.writeFileSync('extras/coffee-script.js', header + '\n' + code);
console.log("built ... running browser tests:");
return invoke('test:browser');
});
task('doc:site', 'watch and continually rebuild the documentation for the website', function() {
return exec('rake doc', function(err) {
if (err) {
throw err;
}
});
});
task('doc:source', 'rebuild the internal documentation', function() {
return exec('docco src/*.*coffee && cp -rf docs documentation && rm -r docs', function(err) {
if (err) {
throw err;
}
});
});
task('doc:underscore', 'rebuild the Underscore.coffee documentation page', function() {
return exec('docco examples/underscore.coffee && cp -rf docs documentation && rm -r docs', function(err) {
if (err) {
throw err;
}
});
});
task('bench', 'quick benchmark of compilation time', function() {
var Rewriter, coffee, fmt, js, litcoffee, littokens, nodes, now, sources, time, tokens, total;
Rewriter = require('./lib/coffee-script/rewriter').Rewriter;
sources = ['coffee-script', 'grammar', 'helpers', 'lexer', 'nodes', 'rewriter'];
coffee = sources.map(function(name) {
return fs.readFileSync("src/" + name + ".coffee");
}).join('\n');
litcoffee = fs.readFileSync("src/scope.litcoffee").toString();
fmt = function(ms) {
return " " + bold + ((" " + ms).slice(-4)) + reset + " ms";
};
total = 0;
now = Date.now();
time = function() {
var ms;
total += ms = -(now - (now = Date.now()));
return fmt(ms);
};
tokens = CoffeeScript.tokens(coffee, {
rewrite: false
});
littokens = CoffeeScript.tokens(litcoffee, {
rewrite: false,
literate: true
});
tokens = tokens.concat(littokens);
console.log("Lex " + (time()) + " (" + tokens.length + " tokens)");
tokens = new Rewriter().rewrite(tokens);
console.log("Rewrite" + (time()) + " (" + tokens.length + " tokens)");
nodes = CoffeeScript.nodes(tokens);
console.log("Parse " + (time()));
js = nodes.compile({
bare: true
});
console.log("Compile" + (time()) + " (" + js.length + " chars)");
return console.log("total " + (fmt(total)));
});
runTests = function(CoffeeScript) {
var arrayEgal, code, currentFile, egal, error, failures, file, filename, files, func, literate, name, passedTests, startTime, _i, _len, _ref1;
startTime = Date.now();
currentFile = null;
passedTests = 0;
failures = [];
_ref1 = require('assert');
for (name in _ref1) {
func = _ref1[name];
global[name] = func;
}
global.CoffeeScript = CoffeeScript;
global.Repl = require('./lib/coffee-script/repl');
global.test = function(description, fn) {
var e;
try {
fn.test = {
description: description,
currentFile: currentFile
};
fn.call(fn);
return ++passedTests;
} catch (_error) {
e = _error;
return failures.push({
filename: currentFile,
error: e,
description: description != null ? description : void 0,
source: fn.toString != null ? fn.toString() : void 0
});
}
};
egal = function(a, b) {
if (a === b) {
return a !== 0 || 1 / a === 1 / b;
} else {
return a !== a && b !== b;
}
};
arrayEgal = function(a, b) {
var el, idx, _i, _len;
if (egal(a, b)) {
return true;
} else if (a instanceof Array && b instanceof Array) {
if (a.length !== b.length) {
return false;
}
for (idx = _i = 0, _len = a.length; _i < _len; idx = ++_i) {
el = a[idx];
if (!arrayEgal(el, b[idx])) {
return false;
}
}
return true;
}
};
global.eq = function(a, b, msg) {
return ok(egal(a, b), msg != null ? msg : "Expected " + a + " to equal " + b);
};
global.arrayEq = function(a, b, msg) {
return ok(arrayEgal(a, b), msg != null ? msg : "Expected " + a + " to deep equal " + b);
};
process.on('exit', function() {
var description, error, fail, filename, message, source, time, _i, _len;
time = ((Date.now() - startTime) / 1000).toFixed(2);
message = "passed " + passedTests + " tests in " + time + " seconds" + reset;
if (!failures.length) {
return log(message, green);
}
log("failed " + failures.length + " and " + message, red);
for (_i = 0, _len = failures.length; _i < _len; _i++) {
fail = failures[_i];
error = fail.error, filename = fail.filename, description = fail.description, source = fail.source;
console.log('');
if (description) {
log(" " + description, red);
}
log(" " + error.stack, red);
if (source) {
console.log(" " + source);
}
}
});
files = fs.readdirSync('test');
for (_i = 0, _len = files.length; _i < _len; _i++) {
file = files[_i];
if (!(helpers.isCoffee(file))) {
continue;
}
literate = helpers.isLiterate(file);
currentFile = filename = path.join('test', file);
code = fs.readFileSync(filename);
try {
CoffeeScript.run(code.toString(), {
filename: filename,
literate: literate
});
} catch (_error) {
error = _error;
failures.push({
filename: filename,
error: error
});
}
}
return !failures.length;
};
task('test', 'run the CoffeeScript language test suite', function() {
return runTests(CoffeeScript);
});
task('test:browser', 'run the test suite against the merged browser script', function() {
var result, source;
source = fs.readFileSync('extras/coffee-script.js', 'utf-8');
result = {};
global.testingBrowser = true;
(function() {
return eval(source);
}).call(result);
return runTests(result.CoffeeScript);
});
}).call(this);
/*
//@ sourceMappingURL=Cakefile.map
*/