-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cakefile
91 lines (83 loc) · 3.49 KB
/
Cakefile
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
fs = require 'fs'
{spawn} = require 'child_process'
# Extend `task` to use `option_defaults` and provide callbacks (task chaining)
do ->
tasks = {}
_task = global.task
global.task = (name, description, actions...) ->
[fnc, callchain...] = for action in actions
if typeof action is 'function'
(options, next_task, more_tasks...) ->
console.log "running task #{name}..."
options[key] ?= val for key, val of option_defaults
callback = -> next_task?(options, more_tasks...)
action(options, callback)
callback?() if action.length < 2
else
tasks[action] ? (throw "No such task: #{action}")
tasks[name] = (options, callbacks...) ->
fnc(options, callchain.concat(callbacks)...)
_task name, description, tasks[name]
# Options and defaults
option '-n', '--node [CMD]', 'path to node executable'
option '-c', '--coffeescript [DIR]', 'path to coffeescript (incl. source)'
option '-s', '--source [DIR]', 'source dir'
option '-b', '--build [DIR]', 'build dir'
option '-o', '--dist [PATH]', 'path of merged javascript file'
option_defaults =
node: 'node'
coffeescript: './coffee-script'
source: './src'
build: './build/coffee-script'
dist: './CoffeeScriptParser.js'
# Helper to execute a command with a callback
run = (command, args, callback) ->
proc = spawn command, args
proc.stdout.pipe process.stdout, end: false
proc.stderr.pipe process.stderr, end: false
proc.on 'exit', (status) ->
process.exit(1) if status != 0
callback() if typeof callback is 'function'
## Tasks
task 'build:source', 'build from source', (o, callback) ->
files = []
files.push "#{o.source}/#{file}" for file in [
'lexer.coffee', 'grammar.coffee', 'CoffeeScriptParser.coffee']
files.push "#{o.coffeescript}/src/#{file}" for file in [
'helpers.coffee', 'rewriter.coffee', 'nodes.coffee', 'scope.litcoffee']
run o.node,
["#{o.coffeescript}/bin/coffee", '-c', '-o', o.build].concat(files),
callback
task 'build:parser', 'build the parser', 'build:source', (o)->
parser = require("#{o.build}/grammar").parser
parser.hasErrorRecovery = true # workaround preserving error recovery code
code = """
var Parser = function Parser(){};
Parser.prototype = #{parser.generateModule_()};
exports.Parser = Parser;
"""
fs.writeFileSync "#{o.build}/parser.js", code
task 'merge', 'merge compiled source to CoffeeScriptParser.js', (o)->
code = for name in ['helpers', 'rewriter', 'lexer', 'scope', 'nodes',
'parser']
"""
require['./#{name}'] = new function() {
var exports = this;
#{fs.readFileSync "#{o.build}/#{name}.js"}
return this;
};
"""
code = """
// Generated by CoffeeScript
if (typeof exports === 'undefined') { var exports = this; }
function require(path){ return require[path]; }
#{code.join('\n')}
#{fs.readFileSync "#{o.build}/CoffeeScriptParser.js"}
"""
fs.writeFileSync o.dist, code
task 'test', 'run tests', (o, callback) ->
run o.node,
["#{o.coffeescript}/bin/coffee", './test.coffee', o.dist],
callback
task 'build', 'build everything and run tests',
'build:parser', 'merge', 'test'