-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCakefile
191 lines (171 loc) · 5.87 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
# -------------------------------------------------------------------------------
# Modules
# Assumed to be in src/XXXXX.coffee, @is also the require() string
modules = [
'loopscript'
'riffwave'
'freq'
'examples'
'beatmaker'
]
pages = [
'index'
'play'
'learn'
'source'
]
pageTitles =
index: "Home"
play: "Play"
learn: "Learn"
source: "Source"
# -------------------------------------------------------------------------------
# Build scripts
browserify = require 'browserify'
fs = require 'fs'
util = require 'util'
watch = require 'node-watch'
http = require 'http'
{spawn} = require 'child_process'
coffeeName = 'coffee'
if process.platform == 'win32'
coffeeName += '.cmd'
generateJSBundle = (cb) ->
name = "loopscript"
outputDir = "js/"
b = browserify {
basedir: outputDir
extensions: ['coffee']
}
b.transform 'coffeeify'
for module in modules
b.require('../src/' + module + '.coffee', { expose: "./#{module}" })
outputFilename = "#{outputDir}#{name}.js"
bundlePipe = b.bundle({ debug: true })
.on 'error', (err) ->
util.log "Error #{err}"
bundlePipe
.pipe(require('mold-source-map').transformSourcesRelativeTo(outputDir))
.pipe(fs.createWriteStream(outputFilename))
.on 'finish', ->
util.log "Generated #{outputFilename}"
cb() if cb?
generateJSLib = (callback) ->
coffee = spawn coffeeName, ['-c', '-o', 'cli', 'src']
coffee.stderr.on 'data', (data) ->
process.stderr.write data.toString()
coffee.stdout.on 'data', (data) ->
print data.toString()
coffee.on 'exit', (code) ->
util.log "Generated CLI lib dir"
callback?() if code is 0
wrapClass = (token, cls) ->
return "<span class=\"#{cls}\">#{token}</span>"
wrapToken = (token) ->
if token.match(/^(tone|loop|track|sample|section)$/)
return wrapClass(token, 'keyword')
if token.match(/^(->|;)$/)
return wrapClass(token, 'operator')
if token.match(/^(pattern|adsr|reverb|bpm|freq|duration|src|octave|note|volume|clip|srcnote|srcoctave|wave)$/)
return wrapClass(token, 'command')
if token.match(/^[\\.a-lA-LxX]{16}$/)
return wrapClass(token, 'pattern')
return token
genPrettyExampleCode = (text) ->
lines = text.split(/\n/);
pretty = ""
for line in lines
commentMatches = line.match(/([^#]*)(#.*)?$/)
pieces = commentMatches[1].split(/[ ]/g)
for piece in pieces
pretty += wrapToken(piece) + " "
if typeof commentMatches[2] == 'string'
if commentMatches[2].length > 0
pretty += "<span class=\"comment\">#{commentMatches[2]}</span>"
pretty += "\n"
return pretty
generateHTML = (cb) ->
header = fs.readFileSync("html/header.html", 'utf-8')
footer = fs.readFileSync("html/footer.html", 'utf-8')
for file in pages
srcText = fs.readFileSync("html/#{file}.src.html", 'utf-8')
lines = srcText.split('\n')
dstText = ""
exampleIndex = 0
while (line = lines.shift()) != undefined
line = line.replace(/(\r\n|\n|\r)/gm,"") # strip newlines
if matches = line.match(/^EXAMPLE\s+(.+)/)
exampleCode = ""
exampleIndex++
while line = lines.shift()
line = line.replace(/(\r\n|\n|\r)/gm,"") # strip newlines
if line != 'END'
exampleCode += line + "\n"
else
exampleCode = exampleCode.replace(/\n+$/, "");
exampleTitle = matches[1]
exampleCodeSingleLine = exampleCode
exampleCodeSingleLine = exampleCodeSingleLine.replace(/\n/g, "\\n")
exampleCodeSingleLine = exampleCodeSingleLine.replace(/"/g, "\\\"")
prettyExampleCode = genPrettyExampleCode(exampleCode)
line = """
<div id="exOuter#{exampleIndex}" class="exOuter">
<div id="exTitle#{exampleIndex}" class="exTitle">
#{exampleTitle}
</div>
<div id="exCode#{exampleIndex}" class="exCode well">#{prettyExampleCode}</div>
<div id="exPlayerActions#{exampleIndex}" class="exPlayerActions">
[<a class=\"listenclick\" onclick="useExampleCode#{exampleIndex}(true); return false">Listen Here</a>]
[<a href="#" onclick="useExampleCode#{exampleIndex}(false)">Listen in Playground</a>]
</div>
<div id="exPlayer#{exampleIndex}" class="exPlayer">
</div>
<script>
function useExampleCode#{exampleIndex}(doRender) {
var exampleCode#{exampleIndex} = "#{exampleCodeSingleLine}";
if(doRender) {
render(exampleCode#{exampleIndex}, '#exPlayer#{exampleIndex}', false, false);
} else {
var encodedScript = LZString.compressToBase64(exampleCode#{exampleIndex});
window.location = "play.html?s=" + encodedScript;
}
}
</script>
</div>
"""
break;
dstText += line + "\n"
interpedHeader = header
for name in pages
activeClass = ""
if name == file
activeClass = "class=\"active\""
interpedHeader = interpedHeader.replace("!#{name}!", activeClass)
interpedHeader = interpedHeader.replace("!title!", pageTitles[file])
fs.writeFileSync("#{file}.html", interpedHeader + dstText + footer)
util.log "Generated #{file}.html"
cb() if cb?
buildEverything = (cb) ->
generateJSBundle ->
generateHTML ->
generateJSLib ->
cb() if cb
task 'build', 'build html', (options) ->
buildEverything ->
util.log "Build complete."
option '-p', '--port [PORT]', 'Dev server port'
task 'server', 'Run server and watch for changed source files to automatically rebuild', (options) ->
buildEverything ->
util.log "Watching for changes in src"
options.port ?= 9000
util.log "Starting server at http://localhost:#{options.port}/"
nodeStatic = require 'node-static'
file = new nodeStatic.Server '.'
httpServer = http.createServer (request, response) ->
request.addListener 'end', ->
file.serve(request, response);
.resume()
httpServer.listen options.port
watch ['src', 'html'], (filename) ->
util.log "Source code #{filename} changed, regenerating bundle..."
buildEverything()