-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCakefile
201 lines (155 loc) · 4.93 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
# Cakefile for building application (makefile like).
appName = "my_app"
# Dependencies
sys = require 'sys'
print = console.log
puts = sys.puts
fs = require 'fs'
{exec} = require 'child_process'
# Additional files to add to file that can be found in source directory.:
appFiles = [
]
# Source files (automatically filled)
srcFiles = []
# Jasmine tests files
testFiles = []
# Stylus file to build css
styleFile = "./styles/#{appName}.styl"
# Tools
# Browse source directory to find file with coffee extension and add them
# to the app files list.
walk = (dir, fileList) ->
list = fs.readdirSync(dir)
if list
for file in list
if file
filename = dir + '/' + file
stat = fs.statSync(filename)
if stat and stat.isDirectory()
walk(filename, fileList)
else if filename.substr(-6) == "coffee"
fileList.push(filename)
else if filename.substr(-4) == "styl"
fileList.push(filename)
fileList
# Process compile given files to coffee script file adding given suffix.
process = (appContents, suffix, callback) ->
# Concatenate files
fs.writeFile "app/#{appName}.#{suffix}.coffee", \
appContents.join('\n\n'), \
'utf8', \
(err) ->
if err
print 'Process caught exception:\n' + err
else
# Compile files
exec "coffee --compile app/#{appName}.#{suffix}.coffee", \
(err, stdout, stderr) ->
if err
print 'Compilation caught exception:\n' + err
else
fs.unlink "app/#{appName}.#{suffix}.coffee", (err) ->
if err
print err
else
callback()
# End Tools
# Grab src files and add them to app files.
walk("./src", srcFiles)
# Weird way to enusre that main file is converted at the end of the result file.
i = 0
appFile = ""
for file, index in srcFiles
if file == "./src/#{appName}.coffee"
appFile = file
i = index
srcFiles.splice(i, 1)
srcFiles.push(appFile)
appFiles = appFiles.concat(srcFiles)
# Browse tests files
walk("./tests/specs", testFiles)
# Build task
task 'build', 'Build single application file from source files', ->
puts 'Start build'
appContents = new Array
remaining = appFiles.length
# Compile styles
puts 'Building CSS'
exec "stylus " + styleFile, (err, stdout, stderr) ->
if err
print "Stylus caught exception: \n" + err
else
print stdout
# Then compile JS Code
puts 'Building JS'
for file, index in appFiles then do (file, index) ->
puts file
fs.readFile "#{file}", 'utf8', (err, fileContents) ->
if err
print 'Read file caught exception:\n ' + err
else
appContents[index] = fileContents
if --remaining is 0
process appContents, "dev", () ->
puts 'Build done'
invoke 'minify'
# Make JS file lighter
task 'minify', 'Minify the resulting application file after build', ->
puts 'Start minify'
command = "uglifyjs app/#{appName}.dev.js > app/#{appName}.js"
exec command, (err, stdout, stderr) ->
if err
print "Minify caught exception: \n" + err
else
puts 'Minify done.'
# Automatically build app when a change occurs
task 'watch', 'Watch prod source files and build changes', ->
print "Starting watching for modifications..."
# Watch Coffeescript files
for file in appFiles then do (file) ->
fs.watchFile "#{file}", (curr, prev) ->
if +curr.mtime isnt +prev.mtime
print "Saw change in app/#{file}.coffee"
invoke 'build'
# Watch Stylus file
fs.watchFile styleFile, (curr, prev) ->
if +curr.mtime isnt +prev.mtime
print "Saw change in " + styleFile
invoke 'build'
# Build documentations
task 'docs', 'Build documentations', ->
puts 'Start buidling code documentations'
command = "docco " + srcFiles.join(" ")
exec command, (err, stdout, stderr) ->
if err
print "Docco caught exception: \n" + err
else
print stdout
print 'Documentations built.'
# Build test sources
task 'build-tests', 'Compile coffee script tests to JS', ->
appContents = new Array
# Then compile JS Code
puts 'Building JS'
allFiles = []
allFiles = allFiles.concat(srcFiles)
allFiles = allFiles.concat(testFiles)
remaining = allFiles.length
for file, index in allFiles then do (file, index) ->
puts file
fs.readFile "#{file}", 'utf8', (err, fileContents) ->
if err
print "Read file caught exception: \n" + err
else
print stdout + stderr
appContents[index] = fileContents
if --remaining is 0
process appContents, "tests", ->
puts 'Tests build'
# Run tests
task 'tests', 'run tests through browser', ->
puts 'Run tests through firefox'
command = "firefox index-test.html "
exec command, (err, stdout, stderr) ->
if err
print "Run firefox caught exception: \n" + err