forked from meltingice/CamanJS
/
Cakefile
193 lines (150 loc) · 5.63 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
fs = require 'fs'
{exec} = require 'child_process'
util = require 'util'
{jsmin} = require 'jsmin'
try
packer = require 'packer'
catch err
packer = null
targetName = "caman"
###
CoffeeScript Options
###
csSrcDir = "src"
csTargetDir = "dist"
targetCoffee = "#{csTargetDir}/caman"
targetCoreJS = "#{csTargetDir}/#{targetName}.js"
targetCoreMinJS = "#{csTargetDir}/#{targetName}.min.js"
targetCorePackJS = "#{csTargetDir}/#{targetName}.pack.js"
coffeeCoreOpts = "-j #{targetName}.js -o #{csTargetDir}"
targetFullJS = "#{csTargetDir}/#{targetName}.full.js"
targetFullMinJS = "#{csTargetDir}/#{targetName}.full.min.js"
targetFullPackJS = "#{csTargetDir}/#{targetName}.full.pack.js"
coffeeFullOpts = "-j #{targetName}.full.js -o #{csTargetDir}"
# All source files listed in include order
coffeeFiles = [
"core/util"
# Core library
"core/caman"
# Everything else
"core/analyze"
"core/autoload"
"core/blender"
"core/calculate"
"core/convert"
"core/event"
"core/filter"
"core/io"
"core/layer"
"core/logger"
"core/pixelinfo"
"core/plugin"
"core/renderer"
"core/store"
# Non-core files
"lib/blenders"
"lib/filters"
"lib/size"
]
pluginsFolder = "src/plugins/src"
###
Event System
###
finishedCallback = {}
finished = (type) ->
finishedCallback[type]() if finishedCallback[type]?
finishListener = (type, cb) ->
finishedCallback[type] = cb
getPlugins = ->
content = ""
util.log "Gathering plugin files in #{pluginsFolder}"
pluginFiles = fs.readdirSync pluginsFolder
util.log "Discovered #{pluginFiles.length} plugins"
for plugin in pluginFiles
continue if fs.statSync("#{pluginsFolder}/#{plugin}").isDirectory()
content += fs.readFileSync("#{pluginsFolder}/#{plugin}", "utf8") + "\n\n"
return content
###
Tasks
###
task 'docs', 'Generates documentation for the coffee files', ->
util.log 'Invoking docco on the CoffeeScript source files'
files = coffeeFiles
files[i] = "src/#{files[i]}.coffee" for i in [0...files.length]
pluginFiles = fs.readdirSync pluginsFolder
for plugin in pluginFiles
continue if fs.statSync("#{pluginsFolder}/#{plugin}").isDirectory()
files.push "#{pluginsFolder}/#{plugin}"
exec "docco #{files.join(' ')}", (err, stdout, stderr) ->
util.log err if err
util.log "Documentation built into docs/ folder."
task 'watch', 'Automatically recompile the CoffeeScript files when updated', ->
util.log "Watching for changes in #{csSrcDir}"
for jsFile in coffeeFiles then do (jsFile) ->
fs.watchFile "#{csSrcDir}/#{jsFile}.coffee", (curr, prev) ->
if +curr.mtime isnt +prev.mtime
util.log "#{csSrcDir}/#{jsFile}.coffee updated"
invoke 'build'
task 'build', 'Compile and minify all CoffeeScript source files', ->
finishListener 'js', -> invoke 'minify'
invoke 'compile'
option '-m', '--map', 'Compile with source maps'
task 'compile', 'Compile all CoffeeScript source files', (options) ->
util.log "Building #{targetCoreJS} and #{targetFullJS}"
contents = []
remaining = coffeeFiles.length
util.log "Appending #{coffeeFiles.length} files to #{targetCoffee}.coffee"
for file, index in coffeeFiles then do (file, index) ->
fs.readFile "#{csSrcDir}/#{file}.coffee", "utf8", (err, fileContents) ->
util.log err if err
contents[index] = fileContents
util.log "[#{index + 1}] #{file}.coffee"
process() if --remaining is 0
process = ->
core = contents.join("\n\n")
full = core + "\n\n" + getPlugins()
if options.map
util.log "Source map support enabled"
coreOpts = "#{coffeeCoreOpts} -m #{targetCoffee}.coffee"
fullOpts = "#{coffeeFullOpts} -m #{targetCoffee}.full.coffee"
else
coreOpts = "#{coffeeCoreOpts} -c #{targetCoffee}.coffee"
fullOpts = "#{coffeeFullOpts} -c #{targetCoffee}.full.coffee"
fs.writeFile "#{targetCoffee}.coffee", core, "utf8", (err) ->
util.log err if err
exec "coffee #{coreOpts}", (err, stdout, stderr) ->
util.log err if err
util.log "Compiled #{targetCoreJS}"
if options.map
map = JSON.parse fs.readFileSync("#{targetCoffee}.map")
map.sources = ["caman.coffee"]
fs.writeFileSync "#{targetCoffee}.map", JSON.stringify(map)
fs.writeFile "#{targetCoffee}.full.coffee", full, "utf8", (err) ->
util.log err if err
exec "coffee #{fullOpts}", (err, stdout, stderr) ->
util.log err if err
util.log "Compiled #{targetFullJS}"
if options.map
map = JSON.parse fs.readFileSync("#{targetCoffee}.full.map")
map.sources = ["caman.full.coffee"]
fs.writeFileSync "#{targetCoffee}.full.map", JSON.stringify(map)
# if not err
# fs.unlink "#{targetCoffee}.full.coffee", (err) -> util.log err if err
finished('js')
task 'minify', 'Minify the CoffeeScript files', ->
util.log "Minifying #{targetCoreJS}"
fs.readFile targetCoreJS, "utf8", (err, contents) ->
fs.writeFile targetCoreMinJS, jsmin(contents), "utf8", (err) ->
util.log err if err
if packer
util.log "Packing #{targetCoreJS}"
fs.writeFile targetCorePackJS, packer.pack(contents, true), "utf8", (err) ->
util.log err if err
util.log "Minifying #{targetFullJS}"
fs.readFile targetFullJS, "utf8", (err, contents) ->
fs.writeFile targetFullMinJS, jsmin(contents), "utf8", (err) ->
util.log err if err
if packer
util.log "Packing #{targetFullJS}"
fs.writeFile targetFullPackJS, packer.pack(contents, true), "utf8", (err) ->
util.log err if err