-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCakefile
271 lines (233 loc) · 8.92 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
async = require 'async'
{spawn, exec} = require 'child_process'
fs = require 'fs-extra'
glob = require 'glob'
log = console.log
path = require 'path'
remove = require 'remove'
watch = require 'watch'
# Node 0.6 compatibility hack.
unless fs.existsSync
fs.existsSync = (filePath) -> path.existsSync filePath
task 'build', ->
vendor ->
build()
task 'release', ->
vendor ->
build ->
release()
task 'vendor', ->
vendor()
task 'clean', ->
clean()
task 'watch', ->
build ->
setupWatch()
build = (callback) ->
for dir in ['build', 'build/css', 'build/font', 'build/html', 'build/images',
'build/js', 'build/vendor', 'build/vendor/font',
'build/vendor/js']
fs.mkdirSync dir unless fs.existsSync dir
commands = []
commands.push 'cp src/manifest.json build/'
commands.push 'cp -r src/html build/'
commands.push 'cp -r src/font build/'
# TODO(pwnall): consider optipng
commands.push 'cp -r src/images build/'
for inFile in glob.sync 'src/less/**/*.less'
continue if path.basename(inFile).match /^_/
outFile = inFile.replace(/^src\/less\//, 'build/css/').
replace(/\.less$/, '.css')
commands.push "node_modules/less/bin/lessc --strict-imports #{inFile} " +
"> #{outFile}"
for inFile in glob.sync 'vendor/js/*.js'
continue if inFile.match /\.min\.js$/
outFile = 'build/' + inFile
commands.push "cp #{inFile} #{outFile}"
for inFile in glob.sync 'vendor/font/*'
outFile = 'build/' + inFile
commands.push "cp #{inFile} #{outFile}"
commands.push 'cp -r src/coffee build'
commands.push 'node_modules/coffee-script/bin/coffee --output build/js ' +
'--map --compile build/coffee/*.coffee'
async.forEachSeries commands, run, ->
callback() if callback
release = (callback) ->
for dir in ['release', 'release/css', 'release/html', 'release/images',
'release/js', 'release/vendor', 'release/vendor/font',
'release/vendor/js']
fs.mkdirSync dir unless fs.existsSync dir
if fs.existsSync 'release/dropship-chrome.zip'
fs.unlinkSync 'release/dropship-chrome.zip'
# Remove the debug key from manifest.json
json = fs.readFileSync 'build/manifest.json', encoding: 'utf8'
json = json.replace(/^\s*"key":.*$/m, '')
fs.writeFileSync 'release/manifest.json', json
commands = []
# TODO(pwnall): consider a html minifier
commands.push 'cp -r build/html release/'
commands.push 'cp -r build/font release/'
commands.push 'cp -r build/images release/'
commands.push 'cp -r build/vendor/font release/vendor/'
for inFile in glob.sync 'src/less/**/*.less'
continue if path.basename(inFile).match /^_/
outFile = inFile.replace(/^src\/less\//, 'release/css/').
replace(/\.less$/, '.css')
commands.push "node_modules/less/bin/lessc --compress --strict-imports " +
"#{inFile} > #{outFile}"
for inFile in glob.sync 'build/js/*.js'
outFile = inFile.replace /^build\//, 'release/'
commands.push 'node_modules/uglify-js/bin/uglifyjs --compress --mangle ' +
"--output #{outFile} #{inFile}"
for inFile in glob.sync 'vendor/js/*.min.js'
outFile = 'release/' + inFile.replace(/\.min\.js$/, '.js')
commands.push "cp #{inFile} #{outFile}"
commands.push 'cd release && zip -r -9 -x "*.DS_Store" "*.sw*" @ ' +
'dropship-chrome.zip .'
commands.push 'mv release/dropship-chrome.zip ./'
async.forEachSeries commands, run, ->
callback() if callback
clean = (callback) ->
removeReleaseCb = ->
callback() if callback
removeBuildCb = ->
fs.exists 'release', (exists) ->
if exists
fs.remove 'release', removeReleaseCb
else
removeReleaseCb()
fs.exists 'build', (exists) ->
if exists
fs.remove 'build', removeBuildCb
else
removeBuildCb()
setupWatch = (callback) ->
scheduled = false
buildNeeded = false
cleanNeeded = false
onTick = ->
scheduled = false
if cleanNeeded
buildNeeded = false
cleanNeeded = false
console.log "Doing a clean build"
clean -> build()
else if buildNeeded
buildNeed = false
console.log "Building"
build()
watch.createMonitor 'src/', (monitor) ->
monitor.on 'created', (fileName) ->
return unless path.basename(fileName)[0] is '.'
buildNeeded = true
unless scheduled
scheduled = true
process.nextTick onTick
monitor.on 'changed', (fileName) ->
return unless path.basename(fileName)[0] is '.'
buildNeeded = true
unless scheduled
scheduled = true
process.nextTick onTick
monitor.on 'removed', (fileName) ->
return unless path.basename(fileName)[0] is '.'
cleanNeeded = true
buildNeeded = true
unless scheduled
scheduled = true
process.nextTick onTick
vendor = (callback) ->
dirs = ['vendor', 'vendor/js', 'vendor/less', 'vendor/font', 'vendor/tmp',
'vendor/less/font_awesome']
for dir in dirs
fs.mkdirSync dir unless fs.existsSync dir
downloads = [
['https://cdnjs.cloudflare.com/ajax/libs/dropbox.js/0.10.3/dropbox.min.js',
'vendor/js/dropbox.min.js'],
['https://cdnjs.cloudflare.com/ajax/libs/dropbox.js/0.10.3/dropbox.js',
'vendor/js/dropbox.js'],
# Zepto.js is a subset of jQuery.
['http://zeptojs.com/zepto.js', 'vendor/js/zepto.js'],
['http://zeptojs.com/zepto.min.js', 'vendor/js/zepto.min.js'],
# Humanize for user-readable sizes.
['https://raw.github.com/taijinlee/humanize/0.0.9/humanize.js',
'vendor/js/humanize.js'],
# Async.js for asynchronous iterators.
['https://raw.github.com/caolan/async/0.9.0/lib/async.js',
'vendor/js/async.js'],
# URI.js for URL parsing.
['https://raw.github.com/medialize/URI.js/v1.14.1/src/URI.min.js',
'vendor/js/uri.min.js'],
# FontAwesome for icons.
['https://github.com/FortAwesome/Font-Awesome/archive/v4.2.0.zip',
'vendor/tmp/font_awesome.zip'],
]
async.forEachSeries downloads, download, ->
# If a dropbox-js development tree happens to be checked out next to
# the extension, copy the dropbox.js files from there.
commands = []
if fs.existsSync '../dropbox-js/lib/dropbox.js'
commands.push 'cp ../dropbox-js/lib/dropbox.js vendor/js/'
if fs.existsSync '../dropbox-js/lib/dropbox.min.js'
commands.push 'cp ../dropbox-js/lib/dropbox.min.js vendor/js/'
# Minify humanize.
unless fs.existsSync 'vendor/js/humanize.min.js'
commands.push 'node_modules/uglify-js/bin/uglifyjs --compress ' +
'--mangle --output vendor/js/humanize.min.js vendor/js/humanize.js'
# Minify async.js.
unless fs.existsSync 'vendor/js/async.min.js'
commands.push 'node_modules/uglify-js/bin/uglifyjs --compress ' +
'--mangle --output vendor/js/async.min.js vendor/js/async.js'
# Use minified URI.js everywhere.
unless fs.existsSync 'vendor/js/uri.js'
commands.push 'cp vendor/js/uri.min.js vendor/js/uri.js'
# Unpack fontawesome.
unless fs.existsSync 'vendor/tmp/Font-Awesome-4.2.0/'
commands.push 'unzip -qq -d vendor/tmp vendor/tmp/font_awesome'
# Patch fontawesome inplace.
# commands.push 'sed -i -e "/^@FontAwesomePath:/d" ' +
# 'vendor/tmp/Font-Awesome-4.2.0/less/variables.less'
async.forEachSeries commands, run, ->
commands = []
# Copy fontawesome to vendor/.
for inFile in glob.sync 'vendor/tmp/Font-Awesome-4.2.0/less/*.less'
outFile = inFile.replace /^vendor\/tmp\/Font-Awesome-4\.2\.0\/less\//,
'vendor/less/font_awesome/'
unless fs.existsSync outFile
commands.push "cp #{inFile} #{outFile}"
for inFile in glob.sync 'vendor/tmp/Font-Awesome-4.2.0/fonts/*'
outFile = inFile.replace /^vendor\/tmp\/Font-Awesome-4\.2\.0\/fonts\//,
'vendor/font/'
unless fs.existsSync outFile
commands.push "cp #{inFile} #{outFile}"
async.forEachSeries commands, run, ->
callback() if callback
_current_cmd = null
run = (command, options, callback) ->
if !callback and typeof options is 'function'
callback = options
options = {}
else
options or= {}
if /^win/i.test(process.platform) # Awful windows hacks.
command = command.replace(/\//g, '\\')
cmd = spawn 'cmd', ['/C', command]
else
cmd = spawn '/bin/sh', ['-c', command]
_current_cmd = cmd
cmd.stdout.on 'data', (data) ->
process.stdout.write data unless options.noOutput
cmd.stderr.on 'data', (data) ->
process.stderr.write data unless options.noOutput
cmd.on 'exit', (code) ->
_current_cmd = null
if code isnt 0 and !options.noExit
console.log "Non-zero exit code #{code} running\n #{command}"
process.exit 1
callback(code) if callback?
null
download = ([url, file], callback) ->
if fs.existsSync file
callback() if callback?
return
run "curl --fail -L -o #{file} #{url}", callback