-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCakefile
241 lines (197 loc) · 7.18 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
# A JavaScript x86 emulator
# Some of the code based on:
# JPC: A x86 PC Hardware Emulator for a pure Java Virtual Machine
# Release Version 2.0
#
# A project from the Physics Dept, The University of Oxford
#
# Copyright (C) 2007-2009 Isis Innovation Limited
#
# Javascript version written by Paul Sohier, 2012
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# Code from the Cakefile is based on https://raw.github.com/krismolendyke/InstantJasmineCoffee/master/Cakefile
fs = require 'fs'
{exec} = require 'child_process'
util = require 'util'
coffee = require 'coffee-script'
SrcCoffeeDir = 'coffee'
TargetJsDir = 'src'
TargetFileName = 'emulator'
TargetCoffeeFile = "#{TargetJsDir}/#{TargetFileName}.coffee"
TargetCoffeeFileDebug = "#{TargetJsDir}/#{TargetFileName}-debug.coffee"
TargetJsFile = "#{TargetJsDir}/#{TargetFileName}.js"
TargetJsFileDebug= "#{TargetJsDir}/#{TargetFileName}-debug.js"
TargetJsMinFile = "#{TargetJsDir}/#{TargetFileName}.min.js"
CoffeeOpts = " --output #{TargetJsDir} --compile #{TargetCoffeeFile}"
CoffeeOptsDebug = "-b --output #{TargetJsDir} --compile #{TargetCoffeeFileDebug}"
header = """
/**
* A JavaScript x86 emulator
* Some of the code based on:
* JPC: A x86 PC Hardware Emulator for a pure Java Virtual Machine
* Release Version 2.0
*
* A project from the Physics Dept, The University of Oxford
*
* Copyright (C) 2007-2009 Isis Innovation Limited
*
* Javascript version written by Paul Sohier, 2012
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
**/
"""
headerC = """
###
A JavaScript x86 emulator
Some of the code based on:
JPC: A x86 PC Hardware Emulator for a pure Java Virtual Machine
Release Version 2.0
A project from the Physics Dept, The University of Oxford
Copyright (C) 2007-2009 Isis Innovation Limited
Javascript version written by Paul Sohier, 2012
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as published by
the Free Software Foundation.
###
"""
task 'watchugly', 'Watch source files and build + minify changes', ->
invoke 'buildugly'
util.log "Watching for changes in #{SrcCoffeeDir}"
exec "cat coffee/files.txt", (err, stdout, stderr) ->
handleError(err) if err
handleError(stderr) if stderr
cf = stdout.split("\n")
util.log "Found #{cf.length} files"
cf = stdout.split("\n")
for file in cf then do (file) ->
fs.watchFile "#{SrcCoffeeDir}/#{file}.coffee", (curr, prev) ->
if +curr.mtime isnt +prev.mtime
util.log "Saw change in #{SrcCoffeeDir}/#{file}.coffee"
invoke 'buildugly'
task 'watch', 'Watch source files and build changes', ->
invoke 'build'
util.log "Watching for changes in #{SrcCoffeeDir}"
exec "cat coffee/files.txt", (err, stdout, stderr) ->
handleError(err) if err
handleError(stderr) if stderr
cf = stdout.split("\n")
util.log "Found #{cf.length} files"
for file in cf then do (file) ->
fs.watchFile "#{SrcCoffeeDir}/#{file}.coffee", (curr, prev) ->
if +curr.mtime isnt +prev.mtime
util.log "Saw change in #{SrcCoffeeDir}/#{file}.coffee"
invoke 'build'
task 'buildugly', 'Build a single JavaScript file from src files and minify them', ->
invoke 'build'
invoke 'uglify'
task 'build', 'Build a single JavaScript file from src files', ->
util.log "Building #{TargetJsFile}"
exec "cat coffee/files.txt", (err, stdout, stderr) ->
handleError(err) if err
handleError(stderr) if stderr
cf = stdout.split("\n")
appContents = new Array
remaining = cf.length
util.log "Appending #{cf.length} files to #{TargetCoffeeFile}"
for file, index in cf then do (file, index) ->
if (file != "")
fs.readFile "#{SrcCoffeeDir}/#{file}.coffee"
, 'utf8'
, (err, fileContents) ->
handleError(err) if err
appContents[index] = "#Start file: #{file}\n\n" + fileContents + "#end file: #{file}\n\n"
util.log "[#{index + 1}] #{file}.coffee"
process() if --remaining is 0
else
process() if --remaining is 0
process = ->
tmp = headerC + appContents.join('\n\n')
fs.writeFile TargetCoffeeFile
, tmp
, 'utf8'
, (err) ->
handleError(err) if err
exec "cp #{TargetCoffeeFile} #{TargetCoffeeFileDebug}", (err, stdout, stderr) ->
handleError(err) if err
util.log "Copied"
exec "/usr/local/bin/coffee #{CoffeeOpts}", (err, stdout, stderr) ->
handleError(err) if err
message = "Compiled #{TargetJsFile}"
displayNotification message
util.log message
task 'builddebug', 'Build a single Debug JavaScript file from src files', ->
util.log "Building #{TargetJsFile}"
exec "cat coffee/files.txt", (err, stdout, stderr) ->
handleError(err) if err
handleError(stderr) if stderr
cf = stdout.split("\n")
appContents = new Array
remaining = cf.length
util.log "Appending #{cf.length} files to #{TargetCoffeeFile}"
for file, index in cf then do (file, index) ->
if (file != "")
fs.readFile "#{SrcCoffeeDir}/#{file}.coffee"
, 'utf8'
, (err, fileContents) ->
handleError(err) if err
appContents[index] = "#Start file: #{file}\n\n" + fileContents + "#end file: #{file}\n\n"
util.log "[#{index + 1}] #{file}.coffee"
process() if --remaining is 0
else
process() if --remaining is 0
process = ->
tmp = headerC + appContents.join('\n\n')
fs.writeFile TargetCoffeeFile
, tmp
, 'utf8'
, (err) ->
handleError(err) if err
exec "cp #{TargetCoffeeFile} #{TargetCoffeeFileDebug}", (err, stdout, stderr) ->
handleError(err) if err
util.log "Copied"
exec "coffee #{CoffeeOptsDebug} ", (err, stdout, stderr) ->
handleError(err) if err
message = "Compiled #{TargetJsFileDebug}"
displayNotification message
util.log message
task 'uglify', 'Minify and obfuscate', ->
#Compiler: http://code.google.com/closure/compiler/
exec "java -jar ../compiler-latest/compiler.jar --language_in=ECMASCRIPT5_STRICT --compilation_level=ADVANCED_OPTIMIZATIONS --manage_closure_dependencies --js=src/emulator.js --js_output_file=src/emulator-min.js", (err, stdout, stderr) ->
handleError(err) if err
addHeader("src/emulator-min.js", header)
util.log stdout
util.log stderr
util.log "Minified emulator.js"
displayNotification "Minified emulator.js"
coffee = (options = "", file) ->
util.log "Compiling #{file}"
exec "coffee #{options} -c #{file}", (err, stdout, stderr) ->
handleError(err) if err
addHeader = (file) ->
fs.readFile "#{file}"
, 'utf8'
, (err, fileContents) ->
handleError(err) if err
util.log "Write #{file} with new header"
nw = header + fileContents
fs.unlink file, (err) -> handleError(err) if err
fs.writeFile file
, nw
, 'utf8'
, (err) ->
handleError(err) if err
handleError = (error) ->
util.log error
displayNotification error
displayNotification = (message = '') ->
options = {
title: 'Compileresult'
}
try
growl = require('growl')
growl(message, options)