-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cakefile
160 lines (143 loc) · 4.6 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
browserify = require 'browserify'
coffeeify = require 'coffeeify'
uglifyify = require 'uglifyify'
nodeStatic = require 'node-static'
fs = require 'fs'
path = require 'path'
{spawn} = require 'child_process'
util = require 'util'
watch = require 'node-watch'
coffeeName = 'coffee'
if process.platform == 'win32'
coffeeName += '.cmd'
buildGame = (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 './game/src/main.coffee'
b.transform coffeeify
if productionBuild
b.transform { global: true, ignore: ['**/main.*'] }, uglifyify
b.bundle (err, result) ->
if not err
fs.writeFile "game/lib/index.js", result, (err) ->
if not err
util.log "Game compilation finished."
callback?()
else
util.log "\x07Game bundle write failed: " + err
else
util.log "\x07Game compilation failed: " + err
buildWebSolver = (callback) ->
# equal of command line $ "browserify --debug -t coffeeify ./src/websolver.coffee > bundle.js "
productionBuild = (process.env.NODE_ENV == 'production')
opts = {
extensions: ['.coffee']
}
if not productionBuild
opts.debug = true
b = browserify opts
b.add './game/src/websolver.coffee'
b.transform coffeeify
if productionBuild
b.transform { global: true, ignore: ['**/websolver.*'] }, uglifyify
b.bundle (err, result) ->
if not err
fs.writeFile "game/lib/websolver.js", result, (err) ->
if not err
util.log "Web solver compilation finished."
callback?()
else
util.log "\x07Web solver bundle write failed: " + err
else
util.log "\x07Web solver compilation failed: " + err
buildSolver = (callback) ->
# equal of command line $ "browserify --debug -t coffeeify ./src/main.coffee > bundle.js "
productionBuild = (process.env.NODE_ENV == 'production')
opts = {
extensions: ['.coffee']
detectGlobals: false
insertGlobals: false
}
if not productionBuild
opts.debug = true
b = browserify opts
b.add './game/src/solver.coffee'
b.transform coffeeify
if productionBuild
b.transform { global: true, ignore: ['**/main.*'] }, uglifyify
b.bundle (err, result) ->
if not err
fs.writeFile "solver.js", result, (err) ->
if not err
util.log "Solver compilation finished."
callback?()
else
util.log "\x07Solver bundle write failed: " + err
else
util.log "\x07Solver compilation failed: " + err
buildVersion = (callback) ->
gameVersion = getVersion()
source = """
module.exports = "#{gameVersion}"
"""
versionFilename = "./game/src/version.coffee"
sourceOnDisk = fs.readFileSync(versionFilename, "utf8")
if source != sourceOnDisk
fs.writeFileSync(versionFilename, source)
util.log "Updated version to #{gameVersion}"
callback?()
buildEverything = (callback) ->
buildVersion ->
buildGame ->
buildWebSolver ->
buildSolver ->
buildAppCache ->
callback?()
getVersion = ->
return JSON.parse(fs.readFileSync("package.json", "utf8")).version
buildAppCache = (callback) ->
callback?()
# gameVersion = getVersion()
# manifest = """
# CACHE MANIFEST
# # version #{gameVersion}
# CACHE:
# index.html
# lib/index.js
# """
# fs.writeFile "game/game.appcache", manifest, (err) ->
# if not err
# util.log "Appcache generation finished. (#{gameVersion})"
# callback?()
# else
# util.log "Appcache write failed: " + err
watchEverything = ->
util.log "Watching for changes in src"
watch ['game/src','package.json'], (filename) ->
coffeeFileRegex = /\.coffee$/
if coffeeFileRegex.test(filename) || (filename == 'package.json')
util.log "Source code #{filename} changed."
util.log "Regenerating bundle..."
buildEverything()
buildEverything()
task 'build', 'build game', (options) ->
buildEverything()
task 'watch', 'Run dev server and watch for changed source files to automatically rebuild', (options) ->
watchEverything()
task 'serve', 'serve game and watch', (options) ->
console.log "nodeStatic #{Object.keys(nodeStatic)}"
fileServer = new nodeStatic.Server('./game', { cache: 0 })
require('http').createServer( (request, response) ->
request.addListener('end', ->
util.log "Serving #{request.url}"
fileServer.serve(request, response)
).resume()
).listen(8080)
util.log "Listening on port 8080"
watchEverything()