-
Notifications
You must be signed in to change notification settings - Fork 3
/
Cakefile
149 lines (127 loc) · 4.96 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
#
# Cakefile to build application
#
# author: Jan Gottschick
#
fs = require 'fs'
spawn = require('child_process').spawn
gaze = require 'gaze'
glob = require 'glob'
mkdirp = require 'mkdirp'
path = require 'path'
###########################################
#
# basic tasks
#
task 'build:coffee', 'compiling coffeescript files', ->
buildCoffee 'src/**/*.+(coffee|litcoffee)'
task 'build:js', 'copying javascript files', ->
copyToLib 'src/**/*.+(js|map)', 'javascript'
task 'build:pegjs', 'compiling pegjs files', ->
buildPegJs 'src/**/*.pegjs'
task 'build:pegcoffee', 'compiling pegcoffee files', ->
buildPegCoffee 'src/**/*.pegcoffee'
task 'test:jasmine', 'test spec files', ->
sources = glob.sync 'spec/**/*Spec.+(js|coffee|litcoffee)'
spawn 'jasmine-node', ['--autotest', '--noStack', '--coffee'].concat(sources), {stdio: "inherit"}
###########################################
#
# utilities
#
removeDir = (path) ->
spawn 'rm', ['-rf', path], {stdio: "inherit"}
console.log "Removed directory " + path + "/**/*"
copyToLib = (files) ->
sources = glob.sync(files)
for source in sources
do (source) ->
if not source.match(/^[\w\/]+Test.\w+$/)
target = source.replace(/^src\//, 'lib/')
copyFile source, target
console.log "Copied " + source
copyFile = (source, target) ->
targetDir = target.split('/')[...-1].join('/')
mkdirp.sync targetDir
fs.writeFileSync target, fs.readFileSync(source)
###########################################
#
# watcher utilities
#
watcher = (files, f) ->
console.log 'Watching ' + files
gaze files, (err, watcher) ->
@on 'changed', (filepath) ->
f path.relative(process.cwd(), filepath)
@on 'added', (filepath) ->
f path.relative(process.cwd(), filepath)
buildPegJs = (sources, dir = 'lib/', cb) ->
for source in glob.sync(sources)
do (source) ->
target = source.replace(/src\//, dir).replace(/.pegjs$/, '.js')
targetModule = source.replace(/src\//, dir).replace(/.pegjs$/, 'Module.js')
targetDir = target.split('/')[...-1].join('/')
targetVar = target.split('/')[-1...][0].replace(/.js$/, '')
mkdirp.sync targetDir
peg = spawn 'pegjs', ['--export-var', targetVar, source, target], { stdio: 'inherit' }
peg.on "uncaughtException", (error) ->
console.log target + ": " + error
peg.on "close", (code) ->
console.log "Compiled " + source + " to " + target
peg = spawn 'pegjs', [source, targetModule], { stdio: 'inherit' }
peg.on "uncaughtException", (error) ->
console.log targetModule + ": " + error
peg.on "close", (code) ->
console.log "Compiled " + source + " to " + targetModule
cb(target) if cb
buildPegCoffee = (sources, dir = 'lib/', cb) ->
for source in glob.sync(sources)
do (source) ->
target = source.replace(/src\//, dir).replace(/.pegcoffee$/, '.js')
targetModule = source.replace(/src\//, dir).replace(/.pegcoffee$/, 'Module.js')
targetDir = target.split('/')[...-1].join('/')
targetVar = target.split('/')[-1...][0].replace(/.js$/, '')
mkdirp.sync targetDir
peg = spawn 'pegjs', ['--export-var', targetVar, '--plugin', 'pegjs-coffee-plugin', source, target], { stdio: 'inherit' }
peg.on "uncaughtException", (error) ->
console.log error
peg.on "close", (code) ->
console.log "Compiled " + source + " to " + target
peg = spawn 'pegjs', ['--plugin', 'pegjs-coffee-plugin', source, targetModule], { stdio: 'inherit' }
peg.on "uncaughtException", (error) ->
console.log error
peg.on "close", (code) ->
console.log "Compiled " + source + " to " + targetModule
cb(target) if cb
buildCoffee = (sources, dir = 'lib/', cb) ->
for source in glob.sync(sources)
do (source) ->
if not source.match(/^[\w\/]+Test.\w+$/)
target = source.replace(/src\//, dir).replace(/.litcoffee$/, '.js').replace(/.coffee$/, '.js')
targetDir = target.split('/')[...-1].join('/')
mkdirp.sync targetDir
coffee = spawn 'coffee', ['-c', '-o', targetDir, source], { stdio: 'inherit' }
coffee.on "uncaughtException", (error) ->
console.log error
coffee.on "close", (code) ->
console.log "Compiled " + source
cb(target) if cb
buildJs = (source) ->
copyToLib(source)
###########################################
#
# The available main tasks to run
#
task 'clean', 'use this to remove all files from lib', ->
removeDir('lib')
task 'build', 'use this to build the application from ground up', ->
invoke 'build:pegcoffee'
invoke 'build:pegjs'
invoke 'build:coffee'
invoke 'build:js'
task 'watch', 'use this to watch changes an update the application', ->
watcher 'src/**/*.pegcoffee', buildPegCoffee
watcher 'src/**/*.pegjs', buildPegJs
watcher 'src/**/*.+(coffee|litcoffee)', buildCoffee
watcher 'src/**/*.+(js|map)', buildJs
task 'test', 'use this to execute all tests for the application', ->
invoke 'test:jasmine'