-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCakefile
219 lines (174 loc) · 7.03 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
fs = require 'fs'
{exec} = require 'child_process'
util = require 'util'
uglify = require './node_modules/uglify-js'
prodSrcCoffeeDir = 'src/coffeescript'
specSrcCoffeeDir = 'spec/coffeescript'
specTargetJsDir = 'spec/javascripts'
testTargetJsDir = 'src/javascript'
prodTargetJsDir = 'public_html/js'
prodTargetFileName = 'script'
prodTargetCoffeeFile = "#{prodSrcCoffeeDir}/#{prodTargetFileName}.coffee"
prodTargetJsFile = "#{prodTargetJsDir}/#{prodTargetFileName}.js"
prodTargetJsMinFile = "#{prodTargetJsDir}/#{prodTargetFileName}.min.js"
prodCoffeeOpts = "--output #{prodTargetJsDir} --compile #{prodTargetCoffeeFile}"
specCoffeeOpts = "--output #{specTargetJsDir}"
testCoffeeOpts = "--bare --output #{testTargetJsDir}"
prodCoffeeFiles = [
'ScrollManager'
'DocumentReady'
]
################################################################################
#
#
#
################################################################################
task 'run:test','Run test specs', ->
util.log 'Running test specs'
exec 'jasmine-headless-webkit', (err, stdout, stderr) ->
handleError(err) if err
util.log stdout
displayNotification "Test complete"
################################################################################
#
#
#
################################################################################
task 'build:dest', 'Build a single JavaScript file from prod files', ->
# util.log "Building #{prodTargetJsFile}"
appContents = new Array remaining = prodCoffeeFiles.length
# util.log "Appending #{prodCoffeeFiles.length} files to #{prodTargetCoffeeFile}"
for file, index in prodCoffeeFiles then do (file, index) ->
fs.readFile "#{prodSrcCoffeeDir}/#{file}.coffee"
, 'utf8'
, (err, fileContents) ->
handleError(err) if err
appContents[index] = fileContents
util.log "[#{index + 1}] #{file}.coffee"
process() if --remaining is 0
process = ->
fs.writeFile prodTargetCoffeeFile
, appContents.join('\n\n')
, 'utf8'
, (err) ->
handleError(err) if err
exec "coffee #{prodCoffeeOpts}", (err, stdout, stderr) ->
handleError(err) if err
message = "Compiled #{prodTargetJsFile}"
util.log message
displayNotification message
fs.unlink prodTargetCoffeeFile, (err) -> handleError(err) if err
invoke 'minify'
################################################################################
#
#
#
################################################################################
task 'build:source', 'Build individual javascript source files', ->
fs.readdir prodSrcCoffeeDir, (err, files) ->
handleError(err) if err
for file, index in prodCoffeeFiles then do (file, index) ->
if file != "DocumentReady"
# util.log "Building #{testTargetJsDir}/#{file}.js source files"
coffee testCoffeeOpts, "#{prodSrcCoffeeDir}/#{file}.coffee"
################################################################################
#
#
#
################################################################################
task 'build:test', 'Build individual test specs', ->
# util.log 'Building test specs'
fs.readdir specSrcCoffeeDir, (err, files) ->
handleError(err) if err
for file in files then do (file) ->
if file.match(/\.coffee$/)
coffee specCoffeeOpts, "#{specSrcCoffeeDir}/#{file}"
################################################################################
#
#
#
################################################################################
task 'build', 'Build and test project', ->
invoke 'build:dest'
invoke 'build:source'
invoke 'build:test'
################################################################################
#
#
#
################################################################################
task 'minify', 'Minify', ->
jsp = uglify.parser
pro = uglify.uglify
fs.readFile prodTargetJsFile, 'utf8', (err, fileContents) ->
ast = jsp.parse fileContents # parse code and get the initial AST
# ast = pro.ast_mangle ast # get a new AST with mangled names
ast = pro.ast_squeeze ast # get an AST with compression optimizations
final_code = pro.gen_code ast # compressed code here
fs.writeFile prodTargetJsMinFile, final_code
# fs.unlink prodTargetJsFile, (err) -> handleError(err) if err
message = "Uglified #{prodTargetJsMinFile}"
# util.log message
displayNotification message
################################################################################
#
#
#
################################################################################
coffee = (options = "", file) ->
# util.log "Compiling #{file}"
exec "coffee #{options} --compile #{file}", (err, stdout, stderr) ->
handleError(err) if err
displayNotification "#{file}"
################################################################################
#
#
#
################################################################################
handleError = (error) ->
util.log error
displayNotification error
################################################################################
#
#
#
################################################################################
displayNotification = (message = '') ->
options = {
title: 'CoffeeScript'
image: 'lib/CoffeeScript.png'
}
try require('./node_modules/growl').notify message, options
###
#
# nead to work out how to watch for new files, removed files.
# unitill then using old rakefile for task watching
#
task 'watch', 'Watch prod source files and build changes', ->
invoke 'build:source'
invoke 'build'
util.log "Watching for changes in #{prodSrcCoffeeDir}"
for file in prodCoffeeFiles then do (file) ->
fs.watchFile "#{prodSrcCoffeeDir}/#{file}.coffee", (curr, prev) ->
if +curr.mtime isnt +prev.mtime
util.log "Saw change in #{prodSrcCoffeeDir}/#{file}.coffee"
invoke 'build:source'
invoke 'build'
task 'watch:all', 'Watch production and test CoffeeScript', ->
invoke 'watch'
invoke 'watch:test'
task 'watch:test', 'Watch test specs and build changes', ->
invoke 'build:test'
util.log "Watching for changes in #{specSrcCoffeeDir}"
fs.readdir specSrcCoffeeDir, (err, files) ->
handleError(err) if err
for file in files then do (file) ->
fs.watchFile "#{specSrcCoffeeDir}/#{file}", (curr, prev) ->
if +curr.mtime isnt +prev.mtime
util.log "Saw change in #{specSrcCoffeeDir}/#{file}"
invoke 'build:test'
task 'build:all', 'Build production and test CoffeeScript', ->
invoke 'build:source'
invoke 'build:test'
invoke 'build'
###