-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCakefile
264 lines (222 loc) · 7.82 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
browserify = require 'browserify'
coffeeify = require 'coffeeify'
uglifyify = require 'uglifyify'
Markdown = require 'remarkable'
fs = require 'fs'
path = require 'path'
{spawn} = require 'child_process'
util = require 'util'
coffeeName = 'coffee'
if process.platform == 'win32'
coffeeName += '.cmd'
buildUI = (callback) ->
# equal of command line $ "browserify --debug -t coffeeify ./src/main.coffee > bundle.js "
productionBuild = (process.env.NODE_ENV == 'production')
opts = {
extensions: ['.coffee']
}
if not productionBuild
opts.debug = true
b = browserify opts
b.add './src/ui/main.coffee'
b.transform coffeeify
if productionBuild
b.transform { global: true, ignore: ['**/main.*'] }, uglifyify
b.bundle (err, result) ->
if not err
fs.writeFile "build/templates/ui.js", result, (err) ->
if not err
util.log "UI compilation finished."
callback?()
else
util.log "UI bundle write failed: " + err
else
util.log "UI compilation failed: " + err
buildMarkdown = (callback) ->
parser = new Markdown {
# options
}
files = fs.readdirSync('./markdown')
header = """
# This file is autogenerated. See Cakefile.
React = require 'react'
{span} = require './tags'
markdowns =
"""
footer = """
class MarkdownSpan extends React.Component
constructor: (props) ->
super props
render: ->
html = markdowns[@props.name] ? ""
return span {
dangerouslySetInnerHTML:
__html: html
}
module.exports = MarkdownSpan
'globals: markdowns'
"""
markdownSpan = header + "\n"
for filename in files
parsed = path.parse(filename)
markdown = fs.readFileSync("./markdown/#{filename}")
html = parser.render(String(markdown))
markdownSpan += " #{parsed.name}: " + JSON.stringify(html) + "\n"
markdownSpan += "\n" + footer
fs.writeFileSync("./src/ui/MarkdownSpan.coffee", markdownSpan)
util.log "MarkdownSpan generation finished."
callback?()
buildTool = (callback) ->
coffee = spawn coffeeName, ['-c', '-o', 'build', 'src/tool']
coffee.stderr.on 'data', (data) ->
process.stderr.write data.toString()
process.exit(-1)
coffee.stdout.on 'data', (data) ->
print data.toString()
coffee.on 'exit', (code) ->
util.log "Tool compilation finished."
callback?() if code is 0
# Adapted from 'wrench' module
readdir = (baseDir) ->
baseDir = baseDir.replace(/\/$/, '')
readdirSyncRecursive = (baseDir) ->
files = []
isDir = (fname) ->
if fs.existsSync(path.join(baseDir, fname))
return fs.statSync( path.join(baseDir, fname) ).isDirectory()
return false
prependBaseDir = (fname) ->
return path.join(baseDir, fname)
curFiles = fs.readdirSync(baseDir)
nextDirs = curFiles.filter(isDir)
curFiles = curFiles.map(prependBaseDir)
files = files.concat(curFiles)
while nextDirs.length > 0
files = files.concat( readdirSyncRecursive(path.join(baseDir, nextDirs.shift())) )
return files
# convert absolute paths to relative
fileList = readdirSyncRecursive(baseDir).map (val) ->
return path.relative(baseDir, val)
return fileList
rmdir = (dir) ->
isWindows = !!process.platform.match(/^win/)
try
files = fs.readdirSync(dir)
catch err
return
for file in files
file = path.join(dir, file)
currFile = fs.lstatSync(file)
if currFile.isDirectory()
rmdir(file)
else if currFile.isSymbolicLink()
if isWindows
fs.chmodSync(file, 666) # Windows needs this unless joyent/node#3006 is resolved..
fs.unlinkSync(file)
else
if isWindows
fs.chmodSync(file, 666) # Windows needs this unless joyent/node#3006 is resolved..
fs.unlinkSync(file)
return fs.rmdirSync(dir)
scan = (filename) ->
foundBadIndent = false
lines = String(fs.readFileSync("src/#{filename}")).split(/\n/)
prevIndent = 0
for line, index in lines
line = line.replace(/[\r]/, '')
pageNumber = index+1
indent = 0
if matches = line.match(/^(\s*)/)
indent = matches[1].length
if (prevIndent > 0) and (indent >= prevIndent+4)
console.error "ERROR: Found double indent: #{filename}:#{pageNumber} (#{prevIndent} -> #{indent})"
foundBadIndent = true
prevIndent = indent
return foundBadIndent
findDoubleIndents = (callback) ->
files = readdir('src')
foundBadIndent = false
for file in files
if file.match(/.coffee$/)
if scan(file)
foundBadIndent = true
if not foundBadIndent
util.log "No double indents found."
callback?()
findUnexpectedGlobals = (callback) ->
rmdir 'tmp'
try
fs.mkdirSync 'tmp'
catch
# already exists
coffee = spawn coffeeName, ['-b', '-o', 'tmp', '-c', 'src']
coffee.stderr.on 'data', (data) ->
process.stderr.write data.toString()
process.exit(-1)
coffee.stdout.on 'data', (data) ->
print data.toString()
coffee.on 'exit', (code) ->
if code != 0
console.error "Failed to compile CoffeeScript files."
return
foundError = false
files = readdir('tmp').filter (f) -> f.match(/.js$/)
for filename in files
parsed = path.parse(filename)
moduleName = parsed.name
lines = String(fs.readFileSync("tmp/#{filename}")).replace(/\r/, '').split(/\n/).filter (line) -> not line.match(/^\s*$/)
expectedGlobals = {}
rawGlobals = null
rawVars = null
for line, index in lines
break if rawGlobals and rawVars
if matches = line.match(/^['"]globals:\s*(.+)['"];?$/)
rawGlobals = matches[1]
if matches = line.match(/^var\s+([^;]+);?/)
rawVars = matches[1]
if matches = line.match(/^(\S+) = require\(/)
# console.log "[#{moduleName}] found require: '#{matches[1]}'"
if matches[1] == 'ref'
console.warn "WARNING: #{moduleName} has a global named 'ref'. Please remove multiple return values from require()."
expectedGlobals[matches[1]] = true
if matches = line.match(/^module.exports = ([a-zA-Z0-9_]+);?/)
expectedGlobals[matches[1]] = true
if not rawVars
console.error "ERROR: cannot find variable declarations for #{moduleName}"
foundError = true
continue
hasOneGlobal = false
for k of expectedGlobals
hasOneGlobal = true
if not hasOneGlobal
console.error "ERROR: cannot find any expected globals for #{moduleName}"
foundError = true
continue
if rawGlobals
expectedGlobalsList = rawGlobals.split(/\\n|\s+/).filter (v) -> !v.match(/^\s*$/)
for v in expectedGlobalsList
if expectedGlobals[v]
console.warn "WARNING: #{moduleName} has a redundant expected global '#{v}'"
expectedGlobals[v] = true
declaredVarsList = rawVars.split(/,|\s+/).filter (v) -> !v.match(/^\s*$/)
declaredVars = {}
for v in declaredVarsList
declaredVars[v] = true
# console.log "module #{moduleName}, globals", expectedGlobalsList, "vars", declaredVarsList
for v of expectedGlobals
if not declaredVars[v]
console.error "ERROR: #{moduleName} expecting nonexistent global '#{v}'"
foundError = true
for v in declaredVarsList
if not expectedGlobals[v]
console.error "ERROR: #{moduleName} has unexpected global '#{v}'"
foundError = true
return if foundError
util.log "No unexpected globals found."
callback?()
task 'build', 'build JS bundle', (options) ->
buildMarkdown ->
findDoubleIndents ->
findUnexpectedGlobals ->
buildTool ->
buildUI ->