This repository has been archived by the owner on Jan 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cakefile
310 lines (256 loc) · 9.5 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env node
#
# Copyright (c) 2010 Konstantin Bender.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
fs = require 'fs'
shell = require 'shelljs'
# ------------------------------------------------------------- Constants ------
RED = "\x1B[0;31m"
GREEN = "\x1B[0;32m"
WHITE = "\x1B[0;37m"
RESET = "\x1B[0m"
OK = GREEN + "OK" + RESET
FAILED = RED + "FAILED" + RESET
# --------------------------------------------------------------- Engines ------
engines = {}
engines['node'] = title: "NodeJS", command: "node"
engines['v8'] = title: "V8", command: "v8"
engines['nitro'] = title: "Nitro", command: "jsc"
engines['spider'] = title: "SpiderMonkey", command: "js"
engines['phantom'] = title: "PhantomJS", command: "phantomjs"
default_engine = engines['node']
# -------------------------------------------------------------- Browsers ------
browsers = {}
browsers['safari'] = name: "Safari"
browsers['firefox'] = name: "Firefox"
browsers['chrome'] = name: "Google Chrome"
browsers['webkit'] = name: "WebKit"
default_browser = browsers['safari']
# ----------------------------------------------------------------Browser ------
option '-o', "--output [NAME]", "output directory, default: build"
option '-e', "--engine [NAME]", "use one of the engines: #{Object.keys(engines).join(', ')}"
option '-b', "--browser [NAME]", "use one of the browsers: #{Object.keys(browsers).join(', ')}"
option '-h', "--version [NAME]", "release version, required for task 'release'"
option '-d', "--dry", "only prepare release, don't publish"
# ------------------------------------------------------------------ Milk ------
SOURCE = [
"core"
"function"
"boolean"
"number"
"date"
"string"
"reg-exp"
"array"
"math"
"milk"
].map (pattern) -> "source/#{pattern}.coffee"
TESTS = [
"test"
"test-core"
"test-function"
"test-boolean"
"test-number"
"test-date"
"test-string"
"test-reg-exp"
"test-array"
"test-math"
"test-milk"
].map (pattern) -> "tests/#{pattern}.coffee"
task 'check', "check what engines are installed", (options) ->
for id, engine of engines
put "Checking whether #{WHITE + engine['title'] + RESET} is installed ... "
installed = run "which #{engine['command']}", silent: yes, survive: yes
puts if installed then GREEN + "YES" + RESET else RED + "NO" + RESET
task 'build', "build Milk", (options) ->
invoke 'prepare'
put "Building Milk library ... "
joined = ""
for source, index in SOURCE
joined += "\n" unless index is 0
joined += "# *****************************************************************************\n"
joined += "# File: #{source}\n"
joined += load source
save joined, "build/milk.coffee"
run "coffee --output build/ --compile --bare build/milk.coffee"
puts OK
put "Building Milk tests ... "
run "cat #{TESTS.join ' '} > build/milk-tests.coffee"
run "coffee --output build/ --compile --bare build/milk-tests.coffee"
run "coffee --output build/ --compile --bare support/driver.coffee support/spec-runner.coffee"
run "cat build/driver.js externals/jasmine/jasmine.js build/milk.js build/milk-tests.js build/spec-runner.js > build/test-milk.js"
puts OK
task 'test', "build & run Milk tests", (options) ->
invoke 'build'
invoke 'run'
task 'play', "build & run Milk in browser", (options) ->
invoke 'build'
put "Generating play.html ... "
html = """
<!DOCTYPE html>
<html>
<head>
<title>Milk Playground</title>
<script type='text/javascript' src='milk.js'></script>
</head>
<body>
<div id='content'>
</div>
</body>
</html>
"""
save html, "build/play.html"
puts OK
browser = browsers[options['browser']] or default_browser
put "Opening play.html in #{WHITE + browser['name'] + RESET} ... "
run "open build/play.html -a #{browser['name']}"
puts OK
task 'run', "run Milk tests\n", (options) ->
engine = engines[options['engine']] or default_engine
puts "Running Milk tests on #{WHITE + engine['title'] + RESET} ..."
if options['engine']?
run "cd build; #{engine['command']} test-milk.js"
else
require './build/test-milk.js'
task 'test:all', "build & run all tests on all engines", (options) ->
invoke 'build'
invoke 'run:all'
task 'run:all', "run all tests on all engines\n", (options) ->
invoke 'check'
for own id, engine of engines
if which engine.command
puts "==================================================\n"
run "cake --engine #{id} run"
task 'camel', "build camel case version & run tests", (options) ->
invoke 'clean'
invoke 'build'
coffee_file_names = [
'milk'
'milk-tests'
]
put "Converting to CamelCase ... "
for name in coffee_file_names
underscorized = load "build/#{name}.coffee"
camel_cased = underscorized.replace /_([a-z])/g, (match) -> match[1].toUpperCase()
finalized = camel_cased.replace /([a-z])_/g, (match) -> match[1]
save finalized, "build/#{name}.coffee"
puts OK
put "Compiling ... "
for name in coffee_file_names
run "coffee --output build/ --compile --bare build/#{name}.coffee"
puts OK
invoke 'run'
task 'website', "build website\n", (options) ->
marked = require 'marked'
invoke 'prepare'
put "Building website ... "
run "mkdir -p build/website"
run "rm -rf build/website/*"
run "cp website/* build/website/"
index_html = load "website/index.html"
content_md = load "website/content.md"
index_html = index_html.replace "<!-- content.md -->", marked content_md
save index_html, "build/website/index.html"
puts OK
task 'release', "release a version of Milk (website, NPM)\n", (options) ->
invoke 'build'
invoke 'website'
version = options['version']
dry = options['dry']
check version?, "Can't release, --version required"
put "Preparing release ... "
run "rm -rf build/release"
run "mkdir -p build/release"
puts OK
put "Preparing node module ... "
run "mkdir -p build/release/node"
run "cp build/milk.js build/release/node/milk-node.js"
run "sed 's/x\.x\.x/#{version}/' support/package.json > build/release/node/package.json"
puts OK
put "Preparing website ... "
run "mkdir -p build/release/website"
run "git clone --quiet --single-branch --branch website .git build/release/website"
run "cd build/release/website; git clean -fd"
run "rm -rf build/release/website/*"
run "cp build/website/* build/release/website/; rm -rf build/release/website/*.md"
run "sed 's/x\.x\.x/#{version}/' build/milk.coffee > build/release/website/milk-#{version}.coffee"
run "sed 's/x\.x\.x/#{version}/' build/milk.js > build/release/website/milk-#{version}.js"
run "sed 's/x\.x\.x/#{version}/g' build/website/index.html > build/release/website/index.html"
puts OK
if dry
puts "Dry run, won't publish"
process.exit 0
put "Publishing website ... "
# run "cd build/release/website/; git add *; git commit -a -m 'Publish website (#{version})'; git push origin gh-pages:gh-pages"
# run "git push origin gh-pages:gh-pages"
puts OK
put "Publishing NPM package ... "
# run "cd build/release/node/; node publish"
puts OK
# --------------------------------------------------------------- Default ------
task 'prepare', "create the build directory", (options) ->
put "Preparing ... "
run "mkdir -p build"
puts OK
task 'clean', "delete the build directory\n", (options) ->
put "Cleaning ... "
run "rm -rf build"
puts OK
# ------------------------------------------------------------- Functions ------
run = (command, options = {}) ->
parts = command.split " > "
command = parts[0]
redirect = parts[1]
options['silent'] = yes if redirect?
result = shell.exec command, options
code = result['code']
output = result['output']
if code > 0
put output unless options['silent']
puts FAILED unless options['silent']
process.exit code unless options['survive']
output.to redirect if output? and redirect?
output
check = (condition, message) ->
unless condition
puts "#{message} #{FAILED}"
process.exit 1
put = (message) ->
process.stdout.write message
puts = (message) ->
put message + "\n"
load = (path) ->
data = fs.readFileSync path
if not data?
console.log "Error reading from file: #{path}"
process.exit error
data.toString()
save = (string, path) ->
error = fs.writeFileSync path, string
if error > 0
console.log "Error writing to file: #{path}"
process.exit error
exists = (path) ->
pwd = run "pwd", silent: yes
tested_path = run "test -e #{path} && cd #{path}; pwd", silent: yes
return yes if path is pwd
return yes if tested_path.length > 0 and tested_path isnt pwd
no