-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCakefile
133 lines (109 loc) · 5.04 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
fs = require 'fs'
path = require 'path'
{exec} = require 'child_process'
libFiles = [
# omit src/ and .js to make the below lines a little shorter
'closure.header'
'core'
'adt'
'math.ord.adt'
'closure.footer'
]
###
Build helpers (sequenced, composible)
###
# Filename -> (Maybe (() -> IO) -> () -> IO) -> Maybe (() -> IO) -> () -> IO
ifExists = (filename) -> (condCallback) -> (callback) -> ->
path.exists filename, (exists) ->
if exists
(condCallback callback)()
else
callback() if callback?
# [Filename] -> (String -> () -> IO) -> () -> IO
concatFiles = (files) -> (callback) ->
contents = new Array files.length
remaining = files.length
->
for file, index in files then do (file, index) ->
fs.readFile "#{file}", 'utf8', (err, fileContents) ->
throw err if err
contents[index] = fileContents
(callback contents)() if --remaining is 0 and callback?
# [Filename] -> (String -> () -> IO) -> () -> IO
concatSrcFiles = (files) ->
concatFiles ("src/#{file}.js" for file in files)
# Filename -> Maybe (() -> IO) -> String -> () -> IO
writeFile = (filename) -> (callback) -> (text) -> ->
fs.writeFile "#{filename}", text.join(''), 'utf8', (err) ->
throw err if err
callback() if callback?
# String -> Maybe (() -> IO) -> () -> IO
logDone = (message) -> (callback) -> ->
console.log "...Done (#{message})"
callback() if callback?
###
Build scripts
###
# Maybe (() -> IO) -> () -> IO
build = (callback) -> (concatSrcFiles libFiles) (writeFile 'build/glquery.math.js') callback
# Maybe (() -> IO) -> () -> IO
minify = (callback) -> ->
path.exists 'node_modules/.bin/uglifyjs', (exists) ->
tool = if exists then 'node_modules/.bin/uglifyjs' else 'uglifyjs'
path.exists 'build/glquery.math.js', (exists) ->
if exists
exec "#{tool} --no-copyright build/glquery.math.js > build/glquery.math.min.js", (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
callback() if callback?
# Maybe (() -> IO) -> () -> IO
wrap = (callback) ->
filenameMath = 'glquery.math.js'
filenameMin = 'glquery.math.min.js'
# TODO: Unfortunately it is not so easy to wrap `(callback) -> f0 f1 f2 callback` in JavaScript
# In order to get something similar to point-free style (http://www.haskell.org/haskellwiki/Pointfree)
# we could do something like `(f0.compose f1).compose f2`
# or alternatively `(compose (compose f0) f1) f2` or `(f0.compose() f1).compose() f2`
# Maybe (() -> IO) -> () -> IO
writeMath = (callback) -> (concatFiles ['src/header.js', "build/#{filenameMath}"]) (writeFile "dist/#{filenameMath}") callback
writeMin = (callback) -> (concatFiles ['src/header.js', "build/#{filenameMin}"]) (writeFile "dist/#{filenameMin}") callback
writeModuleMath = (callback) -> (concatFiles ['src/header.js', 'src/module.header.js', "build/#{filenameMath}", 'src/module.footer.js']) (writeFile 'dist/glquery.math.module.js') callback
writeModuleMin = (callback) -> (concatFiles ['src/header.js', 'src/module.header.js', "build/#{filenameMin}", 'src/module.footer.js']) (writeFile 'dist/glquery.math.module.min.js') callback
# Maybe (() -> IO) -> () -> IO
wrapMath = (callback) -> writeMath (logDone 'glquery.math.js') callback
wrapMin = (callback) -> writeMin (logDone 'glquery.math.min.js') callback
wrapModuleMath = (callback) -> writeModuleMath (logDone 'glquery.math.module.js') callback
wrapModuleMin = (callback) -> writeModuleMin (logDone 'glquery.math.module.min.js') callback
# Maybe (() -> IO) -> () -> IO
buildMath = (ifExists "build/#{filenameMath}") ((callback) -> wrapMath wrapModuleMath callback)
buildMin = (ifExists "build/#{filenameMin}") ((callback) -> wrapMin wrapModuleMin callback)
buildMath buildMin callback
###
Tasks
###
task 'all', "Build all distribution files", ->
exec "mkdir -p 'build'", (err, stdout, stderr) ->
(build (logDone 'build') minify (logDone 'minify') wrap())()
task 'build', "Concatenate source files into a single library file", ->
exec "mkdir -p 'build'", (err, stdout, stderr) ->
(build (logDone 'build') wrap())()
console.log "ok...."
task 'fetch:npm', "Fetch the npm package manager", ->
exec "curl http://npmjs.org/install.sh | sudo sh", (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
console.log "...Done"
invokeNext()
task 'fetch:uglifyjs', "Fetch the UglifyJS minification tool", ->
exec "npm install uglify-js", (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
console.log "...Done"
invokeNext()
task 'minify', "Minify the resulting application file after build", ->
exec "mkdir -p 'build'", (err, stdout, stderr) ->
(minify wrap())()
task 'clean', "Cleanup all build files and distribution files", ->
exec "rm -rf build;rm dist/glquery.math.js;rm dist/glquery.math.min.js;rm dist/glquery.math.module.js;rm dist/glquery.math.module.min.js", (err, stdout, stderr) ->
console.log stdout + stderr
console.log "...Done (clean)"