This repository has been archived by the owner on Nov 8, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Cakefile
276 lines (217 loc) · 8.36 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
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
# Cakefile for Mondrian
# Builds the app and the test files
connect = require('connect')
http = require('http')
fs = require 'fs'
{exec} = require 'child_process'
lessc = require 'less'
colors = require 'colors'
haml = require 'haml'
ROOT_BUILD_DIRECTORY = 'build'
SOURCE_HEADER = '''
###
Mondrian vector editor
http://mondrian.io
This software is available under the MIT License. Have fun with it.
Contact: [email protected]
###
'''
filePaths = (callback, keys) ->
# Build file paths from build.yml
# Requires js-yaml node package
paths = []
src = require('js-yaml').safeLoad(fs.readFileSync('build.yml', 'utf8')).src;
if keys?
src = src.filter (module) ->
keys.indexOf(Object.keys(module)[0]) > -1
src.forEach (module) ->
key = Object.keys(module)[0]
fns = module[key]
if typeof(fns[0]) == 'object' and fns[0]._dir != undefined
dir = fns[0]._dir
fns = fns.slice 1
else
dir = key
paths = paths.concat ("src/coffee/#{dir or ''}#{if dir then '/' else ''}#{fn}.coffee" for fn in fns)
callback paths
validateBuildFiles = (paths) ->
# Ensures all files referenced in build.yml actually exist
for path in paths
if not fs.existsSync path
throw "#{path} does not exist"
log = (status, msg) ->
switch status
when "ok"
console.log "[OK] ".green + msg
when "error"
console.log "[ERROR] ".red + msg
when "info"
console.log "[INFO] ".magenta + msg
compileCSS = (pairs) ->
pairs.forEach (config) ->
startTime = new Date()
fs.readFile config.source, 'utf-8', (e, data) ->
lessc.render data, (e, css) ->
if not e
fs.writeFile config.dest, css
compileTime = (new Date().valueOf() - startTime.valueOf()) / 1000
log "ok", "Compiled #{config.source} => #{config.dest} in #{compileTime} seconds"
else
console.log lessc.formatError e, { color: true } if e
compileHAML = (pairs) ->
pairs.forEach (config) ->
startTime = new Date()
fs.readFile config.source, 'utf-8', (e, data) ->
html = haml.render data.toString()
fs.writeFile config.dest, html
compileTime = (new Date().valueOf() - startTime.valueOf()) / 1000
log "ok", "Compiled #{config.source} => #{config.dest} in #{compileTime} seconds"
fileLengths = (paths) ->
lineLengths = paths.map (p) -> p.length
maxPathLength = Math.max.apply(Math, lineLengths)
lengths = []
for path in paths
file = fs.readFileSync path, 'utf8'
for i in [0..maxPathLength - path.length + 2]
path += " "
lengths.push [path, file.match(/\n/gi)?.length or 0]
# Sort by longest
lengths.sort (a, b) ->
if a[1] < b[1]
return 1
else if a[1] > b[1]
return -1
else
return 0
for len in lengths
console.log len.join('')
concatSrcFiles = (paths) ->
contents = ''
counter = 0
for path in paths
contents += fs.readFileSync path, 'utf8'
contents
compileCoffee = (src, outputFile = "#{ROOT_BUILD_DIRECTORY}/assets/javascript/build.js", callback = ->) ->
# Write temp file
tmpFile = outputFile.replace /\.js/, '.coffee'
fs.writeFile tmpFile, src, 'utf8', (err) ->
throw err if err
exec "coffee --compile #{tmpFile}", (err, stdout) ->
if err
log "error", "Coffee compilation failed "
throw err
callback()
compileAppcache = ->
fs.readFile 'build/app/mondrian.appcache', 'utf8', (err, old) ->
buildno = 1
if old
buildno = parseInt(/^# Revision (\d+)$/m.exec(old)[1]) + 1
next = 'CACHE MANIFEST\n# Revision ' + buildno + '\n'
getGitHeadSHA (sha) ->
next += '# SHA ' + sha + '\n'
next += '# Date: ' + new Date().toDateString() + '\n'
fs.readFile 'src/app/mondrian.appcache', 'utf8', (err, data) ->
if err or typeof data isnt "string"
console.log("'src/app/mondrian.appcache' is missing or malformed.")
console.log("Unable to compile appcache.")
return
fs.writeFile 'build/app/mondrian.appcache', next + data
getGitHeadSHA = (cb) ->
fs.readFile '.git/HEAD', 'utf8', (err, ref) ->
if err or typeof ref isnt "string"
console.log('Missing or malformed git HEAD - SHA unavailable.')
return cb null
getGitSHAFromRef '.git/' + ref.slice(4, ref.length).trim(), cb
getGitSHAFromRef = (ref, cb) ->
fs.readFile ref, 'utf8', (err, sha) ->
if err or typeof sha isnt "string"
console.log('Missing or malformed git branch SHA.')
return cb null
cb sha.trim()
task 'build', 'Build project', ->
compileAppcache()
compileCSS([
{ source: 'src/less/ui.less', dest: "#{ROOT_BUILD_DIRECTORY}/assets/style/app.css" }
{ source: 'src/less/embed.less', dest: "#{ROOT_BUILD_DIRECTORY}/assets/style/embed.css" }
{ source: 'src/less/page.less', dest: "#{ROOT_BUILD_DIRECTORY}/assets/style/page.css" }
{ source: 'src/less/contributing.less', dest: "#{ROOT_BUILD_DIRECTORY}/assets/style/contributing.css" }
{ source: 'src/less/testing.less', dest: "#{ROOT_BUILD_DIRECTORY}/assets/style/testing.css" }
])
compileHAML([
{ source: "src/haml/xml.haml", dest: "#{ROOT_BUILD_DIRECTORY}/xml/index.html" }
{ source: "src/haml/contributing.haml", dest: "#{ROOT_BUILD_DIRECTORY}/contributing/index.html" }
])
barLength = 15
if fs.existsSync '.compiletime'
lastCompileTime = fs.readFileSync '.compiletime', 'utf8'
else
lastCompileTime = 20000 # Generous default
lastCompileTime = parseInt lastCompileTime, 10
filePaths (paths) ->
# Ensure all files exist
validateBuildFiles paths
# Concat files
completeSrc = "#{SOURCE_HEADER}\n#{concatSrcFiles paths}"
# Progress bar
compileStart = new Date()
compileProgress = 0
progressInterval = lastCompileTime / barLength
barInterval = setInterval (->
return if compileProgress == barLength
compileProgress += 1
bar = "["
for x in [0...compileProgress]
bar += "█"
for x in [0...barLength - compileProgress]
bar += " "
bar += "] #{Math.round((lastCompileTime - ((compileProgress / barLength) * lastCompileTime)) / 1000)} seconds remaining \r"
process.stdout.write bar
), progressInterval
compiledAppPath = "#{ROOT_BUILD_DIRECTORY}/assets/javascript/build.js"
compileCoffee completeSrc, compiledAppPath, ->
compileTime = new Date().valueOf() - compileStart.valueOf()
finishedMessage = "Compiled app => #{compiledAppPath} in #{compileTime / 1000} seconds"
for x in [0...(barLength - finishedMessage.length) + 30]
finishedMessage += " "
log "ok", finishedMessage
clearInterval barInterval
exec "echo #{compileTime} > .compiletime"
task 'server', 'Run local server', ->
directory = [__dirname, "build"].join('/')
connect()
.use(connect.static(directory))
.use(connect.logger('dev'))
.listen 3000
log "info", "Listening on port 3000."
task 'lengths', 'Print source file lengths', ->
filePaths (paths) ->
fileLengths paths
option '-n', '--name [NAME]', 'Test name'
option '-c', '--console', 'Test name'
task 'tests', 'Run unit tests cuz', (options) ->
name = options.name
filePaths (paths) ->
paths.unshift "src/settings.coffee"
paths.unshift "src/constants.coffee"
paths.unshift "src/utils.coffee"
# Include Test class
paths.push "tests/test.coffee"
# Include tests
paths.push "tests/#{name}.coffee"
validateBuildFiles paths
completeSrc = concatSrcFiles paths
compileCoffee completeSrc, "tests/#{name}.test.js", ->
if Object.keys(options).indexOf("console") > -1
startTime = new Date().valueOf()
exec "node tests/#{name}.test.js", (err, stdout) ->
throw err if err
endTime = new Date().valueOf()
console.log stdout
console.log "Tests ran in #{endTime - startTime} ms"
, ['uiClasses', 'geometry', name] # Include named section from build files
task 'styles', 'Compile CSS', ->
console.log "Build script changed. Build CSS with 'cake build'."
task 'minify', 'Minify source code', ->
exec "uglifyjs #{ROOT_BUILD_DIRECTORY}/assets/javascript/build.js > #{ROOT_BUILD_DIRECTORY}/assets/javascript/build.min.js", (err, stdout, stderr) ->
throw err if err
console.log "Minified JavaScript in #{ROOT_BUILD_DIRECTORY}/assets/"