-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlakefile
213 lines (192 loc) · 6.68 KB
/
Slakefile
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
require! fs
option 'testFile' 'File in (/lib or /test) to run test on' 'FILE'
option 'currentfile' 'Latest file that triggered the save' 'FILE'
externalScripts =
\http://service.ihned.cz/js/modernizr/v2.6.2.svg.min.js
\http://service.ihned.cz/js/d3/v3.3.2.min.js
\http://d3js.org/d3.geo.tile.v0.min.js
\http://service.ihned.cz/js/topojson/v1.min.js
\http://service.ihned.cz/js/moment/v2.2.1.min.js
externalStyles = []
deferScripts = <[ base.js ]>
gzippable = <[ ]>
build-styles = (options = {}) ->
require! async
(err, [external, local]) <~ async.parallel do
* (cb) -> fs.readFile "#__dirname/www/external.css", cb
(cb) -> prepare-stylus \screen, options, cb
fs.writeFile "#__dirname/www/screen.css", external + "\n\n\n" + local
prepare-stylus = (file, options, cb) ->
console.log "Building Stylus"
require! stylus
(err, data) <~ fs.readFile "#__dirname/www/styl/#file.styl"
data .= toString!
stylusCompiler = stylus data
..include "#__dirname/www/styl/"
..define \iurl stylus.url paths: ["#__dirname/www/img/"]
if options.compression
stylusCompiler.set \compress true
(err, css) <~ stylusCompiler.render
throw err if err
console.log "Stylus built"
cb null css
build-script = (file, cb) ->
require! child_process.exec
(err, result) <~ exec "lsc -o #__dirname/www/js -c #__dirname/#file"
throw err if err
cb?!
build-all-scripts = (cb) ->
console.log "Building scripts..."
require! child_process.exec
(err, result) <~ exec "lsc -o #__dirname/www/js -c #__dirname/www/ls"
throw err if err
console.log "Scripts built"
cb?!
download-external-scripts = (cb) ->
console.log "Dowloading scripts..."
require! request
require! async
(err, responses) <~ async.map externalScripts, request~get
bodies = responses.map (.body)
<~ fs.writeFile "#__dirname/www/external.js" bodies.join "\n;\n"
console.log "Scripts loaded"
cb?!
download-external-styles = (cb) ->
console.log "Downloading styles"
require! request
require! async
(err, responses) <~ async.map externalStyles, request~get
contents = responses.map (.body)
<~ fs.writeFile "#__dirname/www/external.css" contents.join "\n\n"
console.log "Styles loaded"
cb!
combine-scripts = (options = {}, cb) ->
console.log "Combining scripts..."
require! uglify: "uglify-js"
(err, files) <~ fs.readdir "#__dirname/www/js"
files .= filter -> it isnt '_loadExternal.js' and it isnt 'script.js.map'
files .= sort (a, b) ->
indexA = deferScripts.indexOf a
indexB = deferScripts.indexOf b
indexA - indexB
files .= map -> "./www/js/#it"
minifyOptions = {}
if not options.compression
minifyOptions
..compress = no
..mangle = no
..outSourceMap = "../js/script.js.map"
..sourceRoot = "../../"
files.unshift "./www/js/_loadExternal.js"
result = uglify.minify files, minifyOptions
{map, code} = result
if not options.compression
code += "\n//@ sourceMappingURL=./js/script.js.map"
fs.writeFile "#__dirname/www/js/script.js.map", map
else
external = fs.readFileSync "#__dirname/www/external.js"
code = external + code
fs.writeFileSync "#__dirname/www/script.js", code
console.log "Scripts combined"
cb? err
run-script = (file) ->
require! child_process.exec
(err, stdout, stderr) <~ exec "lsc #__dirname/#file"
throw err if err
console.error stderr if stderr
console.log stdout
test-script = (file) ->
require! child_process.exec
[srcOrTest, ...fileAddress] = file.split /[\\\/]/
fileAddress .= join '/'
<~ build-all-server-scripts
cmd = "mocha --compilers ls:livescript -R tap --bail #__dirname/test/#fileAddress"
(err, stdout, stderr) <~ exec cmd
niceTestOutput stdout, stderr, cmd
build-all-server-scripts = (cb) ->
require! child_process.exec
(err, stdout, stderr) <~ exec "lsc -o #__dirname/lib -c #__dirname/src"
throw stderr if stderr
cb? err
relativizeFilename = (file) ->
file .= replace __dirname, ''
file .= replace do
new RegExp \\\\, \g
'/'
file .= substr 1
gzip-files = (cb) ->
require! async
(err) <~ async.map gzippable, gzip-file
cb err
gzip-file = (file, cb) ->
require! zlib
gzip = zlib.createGzip!
address = "#__dirname/www/#file"
gzippedAddress = "#__dirname/www/#file.gz"
input = fs.createReadStream address
output = fs.createWriteStream gzippedAddress
input.pipe gzip .pipe output
cb!
refresh-manifest = (cb) ->
(err, file) <~ fs.readFile "#__dirname/www/manifest.template.appcache"
return if err
file .= toString!
file += '\n# ' + new Date!toUTCString!
<~ fs.writeFile "#__dirname/www/manifest.appcache", file
cb?!
task \build ->
download-external-scripts!
<~ download-external-styles
build-styles compression: no
<~ build-all-scripts
combine-scripts compression: no
task \deploy ->
<~ download-external-scripts!
# build-all-server-scripts!
# refresh-manifest!
<~ download-external-styles
build-styles compression: yes
<~ build-all-scripts
<~ combine-scripts compression: yes
<~ gzip-files!
task \build-styles ->
build-styles compression: no
task \build-script ({currentfile}) ->
file = relativizeFilename currentfile
isServer = \src/ == file.substr 0, 4
isTest = \test/ == file.substr 0, 5
if isServer or isTest
test-script file
else
<~ build-script file
combine-scripts compression: no
niceTestOutput = (test, stderr, cmd) ->
lines = test.split "\n"
oks = 0
fails = 0
out = []
shortOut = []
disabledTests = []
for line in lines
if 'ok' == line.substr 0, 2
++oks
else if 'not' == line.substr 0,3
++fails
out.push line
shortOut.push line.match(/not ok [0-9]+ (.*)$/)[1]
else if 'Disabled' == line.substr 0 8
disabledTests.push line
else if line and ('#' != line.substr 0, 1) and ('1..' != line.substr 0, 3)
console.log line# if (' ' != line.substr 0, 3)
if oks && !fails
console.log "Tests OK (#{oks})"
disabledTests.forEach -> console.log it
else
#console.log "!!!!!!!!!!!!!!!!!!!!!!! #{fails} !!!!!!!!!!!!!!!!!!!!!!!"
if out.length
console.log shortOut.join ", "#line for line in shortOut
else
console.log "Tests did not run (error in testfile?)"
console.log test
console.log stderr
console.log cmd