-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCakeFile
229 lines (143 loc) · 5.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
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
###
--------------------------------------------------
Configuration
--------------------------------------------------
###
# Source directory
source = './src'
# Output directory & JS file
output = './bin/rx.splash.js'
# File (in `source` dir) to build import tree from
main = 'rx.splash.coffee'
# Options passed to CoffeeScript the compiler (e.g. --bare)
opts = '--lint'
###
--------------------------------------------------
Dependancies
--------------------------------------------------
###
fs = require 'fs'
util = require 'util'
{exec} = require 'child_process'
try growl = require 'growl'
###
--------------------------------------------------
Constants
--------------------------------------------------
###
REGEX_FILENAME = /\.\w+$/i
REGEX_IMPORT = /#\s?import\s+?([\w\.\-\/\*]+)/g
###
--------------------------------------------------
Node construct for building topological
graphs.
--------------------------------------------------
###
class Node
constructor: ( @name, @content, @edges =[] ) ->
add: ( edges... ) -> @edges = @edges.concat edges
###
--------------------------------------------------
Recursively traverses a directory and
returns a list of all .coffee files.
--------------------------------------------------
###
traverse = ( path, result = [] ) ->
files = fs.readdirSync path
map = {}
for file in files
file = "#{path}/#{file}"
stat = fs.statSync file
if stat.isFile() and /\.coffee$/.test file then result.push file
else if stat.isDirectory() then traverse file, result
result
###
--------------------------------------------------
Loads and indexes a list of files
--------------------------------------------------
###
catalog = ( list, done, result = [] ) ->
process = ( file ) -> ( error, content ) ->
if not error then result.push new Node file, content else throw error
done result if result.length is list.length
fs.readFile file, 'utf8', process file for file in list
###
--------------------------------------------------
Recursively resolves dependancies
--------------------------------------------------
###
resolve = ( node, result = [] ) ->
resolve edge, result for edge in node.edges when edge not in result
result.push node
result
###
--------------------------------------------------
Watch main source and build changes
--------------------------------------------------
###
task 'watch', 'Watch sources and build changes', ( options ) ->
invoke 'build'
util.log "Watching for changes in #{source}"
fs.watchFile [ source, main ].join( '/' ), ( now, old ) => invoke 'build' if +now.mtime isnt +old.mtime
# Build a list of all files
files = traverse source
for file in files then do ( file ) ->
fs.watchFile file, ( now, old ) ->
util.log "Saw change in #{file}"
invoke 'build' if +now.mtime isnt +old.mtime
###
--------------------------------------------------
Builds main source by resolving and
concatenating depandancies before passing
the output to the CoffeeScript compiler
--------------------------------------------------
###
task 'sbuild', 'Build: Sublime Text', -> invoke 'build'
task 'build', 'Build main source and dependancies', ( options ) ->
files = traverse source
# Process them into Nodes
catalog files, ( nodes ) ->
# Map all source paths to Nodes
map = {}
add = ( node, name ) -> ( map[ name ] ?= [] ).push node
for node in nodes
path = node.name.replace( REGEX_FILENAME, '' ).split '/'
last = path.pop()
add node, path.slice( 0, index ).concat( '*' ).join '/' for index in [ 0..path.length ]
add node, path.concat( last ).join '/'
add node, node.name
# Compute edges
for node in nodes
while match = REGEX_IMPORT.exec node.content
if target = match[1]
key = [ source, target.replace /\.(?!coffee)/g, '/' ].join '/'
if not map[ key ] then throw "No file matching import: #{key}"
else node.add link for link in map[ key ]
# Build dependency graph
path = [ source, main ].join '/'
link = map[ path ]?[0]
# Resolve dependancies
if link then chain = resolve link
else throw "Root node not found: #{path}"
# Concatenate contents into one file
content = ( node.content for node in chain ).join '\n\n'
merged = output.replace( REGEX_FILENAME, '' ) + '.coffee'
fs.writeFile merged, content, 'utf8', ( error ) ->
throw error if error
exec "coffee -c #{opts} #{merged}", ( error ) ->
if error then throw error else
if fs.existsSync merged then fs.unlink merged, ( error ) -> throw error if error
grrrr "Project compiled into: #{output}"
minified = output.replace( REGEX_FILENAME, '' ) + '-min.js'
merged = output.replace( REGEX_FILENAME, '' ) + '.js'
console.log minified, merged
exec "uglifyjs -m -o #{minified} #{merged}"
###
--------------------------------------------------
Growl proxy
--------------------------------------------------
###
grrrr = ( message = '' ) ->
options = title: 'CoffeeScript'
growl?.notify message, options
util.log message