/
Cakefile
149 lines (124 loc) · 3.85 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
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
#
# Cakefile
#
# Copyright (c) 2012 Lee Olayvar <leeolayvar@gmail.com>
# MIT Licensed
#
fs = require 'fs'
path = require 'path'
{spawn} = require 'child_process'
util = require 'util'
bork = require 'bork'
Walker = require 'walker'
COFFEE_BIN = './node_modules/coffee-script/bin/coffee'
MOCHA_BIN = './node_modules/mocha/bin/mocha'
# (cmd, args=[], callback=->) -> undefined
#
# Params:
# cmd: The command to execute.
# args: A list of args to pass to the process
# callback: Callback on process exit.
#
# Desc:
# A simple process launcher that streams output.
exec = (cmd, args=[], cb=->) ->
bin = spawn cmd, args
bin.stdout.on 'data', (data) ->
process.stdout.write data
bin.stderr.on 'data', (data) ->
process.stderr.write data
bin.on 'exit', cb
# (source, output, match=->true, callback=->) -> undefined
#
# Params:
# source: The source of the copy.
# output: The output for the copy.
# match: A match function for the files. Return true if a match, false if
# no match.
# callback: Called when all copies are complete.
#
# Desc:
# Copy files recursively based on a match function.
copy = (source, output, match=(->true), callback=->) ->
# Our root Bork task
bork_task = bork()
bork_task.seq -> callback()
# Define our fs logic for copying
copy_file = (file_source, callback) ->
file_output = path.join output, path.relative(source, file_source)
ins = fs.createReadStream file_source
ous = fs.createWriteStream file_output
util.pump ins, ous, callback
# Set up our dir walker
walker = Walker source
walker.on 'file', (file, stat) ->
if match(file)
bork_task.link (done) -> copy_file file, done
walker.on 'end', ->
bork_task.start()
# (source, output, callback) -> undefined
#
# Params:
# source: The source file/directory.
# output: The output file/directory.
# callback: Callback for when it's all finished.
#
# Desc:
# A shorthand function for copy() and calling exec COFFEE_BIN.
copy_compile = (source, output, callback) ->
bork_task = bork()
coffee_task = bork_task.seq (done) ->
exec COFFEE_BIN, ['-co', output, source], -> done()
coffee_task.seq (done) ->
copy source, output,
(file) ->
if path.extname(file) is '.coffee'
return false
else
return true
-> callback()
bork_task.start()
# We're using bork tasks to get around the horrible async support in Cake
# currently.
bork_task = bork()
task 'build', 'build all', ->
invoke 'build:bin'
invoke 'build:lib'
invoke 'build:test'
bork_task.start()
task 'build:bin', 'build bin', ->
console.log 'build:bin start()'
bork_task.link (done) -> copy_compile './bin', './build/bin', ->
console.log 'build:bin end'
done()
task 'build:lib', 'build lib', ->
console.log 'build:lib start()'
bork_task.link (done) -> copy_compile './lib', './build/lib', ->
console.log 'build:lib end'
done()
task 'build:test', 'build test', ->
console.log 'build:test start()'
bork_task.link (done) -> copy_compile './test', './build/test', ->
console.log 'build:test end'
done()
task 'test', 'build test, then run it', ->
invoke 'build:test'
invoke 'test:nobuild'
bork_task.start()
task 'test:full', 'build test, then run it', ->
invoke 'build:lib'
invoke 'build:test'
invoke 'test:nobuild'
bork_task.start()
task 'test:nobuild', 'just run the tests, don\'t build anything', ->
bork_task.seq (done) ->
console.log 'test start'
exec MOCHA_BIN, ['./build/test'], ->
console.log 'test done'
done()
task 'prepublish', 'build all, test all. designed to work before `npm publish`', ->
invoke 'build:bin'
invoke 'build:lib'
invoke 'build:test'
invoke 'test:nobuild'
bork_task.start()