forked from esconie/dropbox-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCakefile
264 lines (222 loc) · 8.06 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
async = require 'async'
{spawn, exec} = require 'child_process'
fs = require 'fs'
glob = require 'glob'
log = console.log
path = require 'path'
remove = require 'remove'
# Node 0.6 compatibility hack.
unless fs.existsSync
fs.existsSync = (filePath) -> path.existsSync filePath
task 'build', ->
build()
task 'test', ->
vendor ->
build ->
ssl_cert ->
tokens ->
test_cases = glob.sync 'test/js/**/*_test.js'
test_cases.sort() # Consistent test case order.
run 'node_modules/.bin/mocha --colors --slow 200 --timeout 20000 ' +
"--require test/js/helpers/setup.js #{test_cases.join(' ')}"
task 'webtest', ->
vendor ->
build ->
ssl_cert ->
tokens ->
webtest()
task 'cert', ->
remove.removeSync 'test/ssl', ignoreMissing: true
ssl_cert()
task 'vendor', ->
remove.removeSync './test/vendor', ignoreMissing: true
vendor()
task 'tokens', ->
remove.removeSync './test/token', ignoreMissing: true
build ->
tokens ->
process.exit 0
task 'doc', ->
run 'node_modules/.bin/codo src'
task 'extension', ->
run 'node_modules/.bin/coffee --compile test/chrome_extension/*.coffee'
task 'chrome', ->
vendor ->
build ->
buildChromeApp 'app_v1'
task 'chrome2', ->
vendor ->
build ->
buildChromeApp 'app_v2'
task 'chrometest', ->
vendor ->
build ->
buildChromeApp 'app_v1', ->
testChromeApp()
task 'chrometest2', ->
vendor ->
build ->
buildChromeApp 'app_v2', ->
testChromeApp()
task 'cordova', ->
vendor ->
build ->
buildCordovaApp()
task 'cordovatest', ->
vendor ->
build ->
buildCordovaApp ->
testCordovaApp()
build = (callback) ->
commands = []
# Ignoring ".coffee" when sorting.
# We want "driver.coffee" to sort before "driver-browser.coffee"
source_files = glob.sync 'src/**/*.coffee'
source_files.sort (a, b) ->
a.replace(/\.coffee$/, '').localeCompare b.replace(/\.coffee$/, '')
# Compile without --join for decent error messages.
commands.push 'node_modules/.bin/coffee --output tmp --compile ' +
source_files.join(' ')
commands.push 'node_modules/.bin/coffee --output lib --compile ' +
"--join dropbox.js #{source_files.join(' ')}"
# Minify the javascript, for browser distribution.
commands.push 'cd lib && ../node_modules/.bin/uglifyjs --compress ' +
'--mangle --output dropbox.min.js --source-map dropbox.min.map ' +
'dropbox.js'
# Tests are supposed to be independent, so the build order doesn't matter.
test_dirs = glob.sync 'test/src/**/'
for test_dir in test_dirs
out_dir = test_dir.replace(/^test\/src\//, 'test/js/')
test_files = glob.sync path.join(test_dir, '*.coffee')
commands.push "node_modules/.bin/coffee --output #{out_dir} " +
"--compile #{test_files.join(' ')}"
async.forEachSeries commands, run, ->
callback() if callback
webtest = (callback) ->
webFileServer = require './test/js/helpers/web_file_server.js'
if 'BROWSER' of process.env
if process.env['BROWSER'] is 'false'
url = webFileServer.testUrl()
console.log "Please open the URL below in your browser:\n #{url}"
else
webFileServer.openBrowser process.env['BROWSER']
else
webFileServer.openBrowser()
callback() if callback?
ssl_cert = (callback) ->
fs.mkdirSync 'test/ssl' unless fs.existsSync 'test/ssl'
if fs.existsSync 'test/ssl/cert.pem'
callback() if callback?
return
run 'openssl req -new -x509 -days 365 -nodes -batch ' +
'-out test/ssl/cert.pem -keyout test/ssl/cert.pem ' +
'-subj /O=dropbox.js/OU=Testing/CN=localhost ', callback
vendor = (callback) ->
# All the files will be dumped here.
fs.mkdirSync 'test/vendor' unless fs.existsSync 'test/vendor'
# Embed the binary test image into a 7-bit ASCII JavaScript.
buffer = fs.readFileSync 'test/binary/dropbox.png'
bytes = (buffer.readUInt8(i) for i in [0...buffer.length])
browserJs = "window.testImageBytes = [#{bytes.join(', ')}];\n"
fs.writeFileSync 'test/vendor/favicon.browser.js', browserJs
workerJs = "self.testImageBytes = [#{bytes.join(', ')}];\n"
fs.writeFileSync 'test/vendor/favicon.worker.js', workerJs
downloads = [
# chai.js ships different builds for browsers vs node.js
['http://chaijs.com/chai.js', 'test/vendor/chai.js'],
# sinon.js also ships special builds for browsers
['http://sinonjs.org/releases/sinon.js', 'test/vendor/sinon.js'],
# ... and sinon.js ships an IE-only module
['http://sinonjs.org/releases/sinon-ie.js', 'test/vendor/sinon-ie.js']
]
async.forEachSeries downloads, download, ->
callback() if callback
testChromeApp = (callback) ->
# Clean up the profile.
fs.mkdirSync 'test/chrome_profile' unless fs.existsSync 'test/chrome_profile'
# TODO(pwnall): remove experimental flag when the identity API gets stable
command = "\"#{chromeCommand()}\" --load-extension=test/chrome_app " +
'--enable-experimental-extension-apis ' +
'--user-data-dir=test/chrome_profile --no-default-browser-check ' +
'--no-first-run --no-service-autorun --disable-default-apps ' +
'--homepage=about:blank --v=-1'
run command, ->
callback() if callback
buildChromeApp = (manifestFile, callback) ->
buildStandaloneApp "test/chrome_app", ->
run "cp test/chrome_app/manifests/#{manifestFile}.json " +
'test/chrome_app/manifest.json', ->
callback() if callback
buildStandaloneApp = (appPath, callback) ->
unless fs.existsSync appPath
fs.mkdirSync appPath
unless fs.existsSync "#{appPath}/test"
fs.mkdirSync "#{appPath}/test"
unless fs.existsSync "#{appPath}/node_modules"
fs.mkdirSync "#{appPath}/node_modules"
links = [
['lib', "#{appPath}/lib"],
['node_modules/mocha', "#{appPath}/node_modules/mocha"],
['node_modules/sinon-chai', "#{appPath}/node_modules/sinon-chai"],
['test/token', "#{appPath}/test/token"],
['test/binary', "#{appPath}/test/binary"],
['test/html', "#{appPath}/test/html"],
['test/js', "#{appPath}/test/js"],
['test/vendor', "#{appPath}/test/vendor"],
]
commands = for link in links
"cp -r #{link[0]} #{path.dirname(link[1])}"
async.forEachSeries commands, run, ->
callback() if callback
chromeCommand = ->
paths = [
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
'/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
'/Applications/Chromium.app/MacOS/Contents/Chromium',
]
for path in paths
return path if fs.existsSync path
if process.platform is 'win32'
'chrome'
else
'google-chrome'
testCordovaApp = (callback) ->
run 'test/cordova_app/cordova/run', ->
callback() if callback
buildCordovaApp = (callback) ->
if fs.existsSync 'test/cordova_app/www' # iOS
appPath = 'test/cordova_app/www'
else if fs.existsSync 'test/cordova_app/assets/www' # Android
appPath = 'test/cordova_app/assets/www'
else
throw new Error 'Cordova www directory not found'
buildStandaloneApp appPath, ->
cordova_js = glob.sync("#{appPath}/cordova-*.js").sort().
reverse()[0]
run "cp #{cordova_js} #{appPath}/test/js/platform.js", ->
run "cp test/html/cordova_index.html #{appPath}/index.html", ->
callback() if callback
tokens = (callback) ->
TokenStash = require './test/js/helpers/token_stash.js'
tokenStash = new TokenStash
(new TokenStash()).get ->
callback() if callback?
run = (args...) ->
for a in args
switch typeof a
when 'string' then command = a
when 'object'
if a instanceof Array then params = a
else options = a
when 'function' then callback = a
command += ' ' + params.join ' ' if params?
cmd = spawn '/bin/sh', ['-c', command], options
cmd.stdout.on 'data', (data) -> process.stdout.write data
cmd.stderr.on 'data', (data) -> process.stderr.write data
process.on 'SIGHUP', -> cmd.kill()
cmd.on 'exit', (code) -> callback() if callback? and code is 0
download = ([url, file], callback) ->
if fs.existsSync file
callback() if callback?
return
run "curl -o #{file} #{url}", callback