forked from chop-dbhi/cilantro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.coffee
502 lines (416 loc) · 16 KB
/
Gruntfile.coffee
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# All source files are created and managed by type in `src`.
#
# There three output directories:
#
# - `local` - Development and testing
# - `build` - Intermediate directory prior to optimizations and/or
# concatentation
# - `dist` - Final output for release purposes. This carries over to the master
# branch to be copied in the root of the repo.
#
# Each Grunt task has a target named `local` which performs copying,
# compiling or symlinking in the `local` directory for development.
# Additional targets may be defined for `build` or `dist` depending on
# the requirements.
#
# Shared options for a task should be defined first, followed by each
# task entry. Each task can define it's own set of options to override
# the shared ones defined for the task.
module.exports = (grunt) ->
shell = require('shelljs')
pkg = grunt.file.readJSON('package.json')
run = (cmd) ->
grunt.log.ok cmd
shell.exec cmd
grunt.initConfig
pkg: pkg
srcDir: 'src'
specDir: 'spec'
localDir: 'local'
buildDir: 'build'
distDir: 'dist'
serve:
forever:
options:
keepalive: true
port: 8125
jasmine:
options:
port: 8126
watch:
coffee:
tasks: ['coffee:local']
files: ['<%= srcDir %>/coffee/**/*']
js:
tasks: ['copy:local']
files: [
'<%= srcDir %>/js/**/*'
'<%= srcDir %>/css/**/*'
'<%= srcDir %>/font/**/*'
'<%= srcDir %>/img/**/*'
]
tests:
tasks: ['jasmine:local:build']
files: ['<%= specDir %>/**/*']
sass:
tasks: ['sass:local']
files: ['<%= srcDir %>/scss/**/*']
coffee:
options:
bare: true
# Output to 'local' for development
local:
options:
sourceMap: true
expand: true
cwd: '<%= srcDir %>/coffee'
dest: '<%= localDir %>/js'
src: '**/*.coffee'
rename: (dest, src) ->
dest + '/' + src.replace('.coffee', '.js')
# Compiles files to the 'build' directory for distribution
build:
expand: true
cwd: '<%= srcDir %>/coffee'
dest: '<%= buildDir %>/js'
src: '**/*.coffee'
rename: (dest, src) ->
dest + '/' + src.replace('.coffee', '.js')
sass:
local:
options:
trace: true
sourcemap: true
files:
'<%= localDir %>/css/style.css': '<%= srcDir %>/scss/style.scss'
dist:
options:
quiet: true
style: 'compressed'
files:
'<%= distDir %>/css/style.css': '<%= srcDir %>/scss/style.scss'
copy:
local:
files: [
expand: true
cwd: '<%= srcDir %>/js'
src: ['**/*']
dest: '<%= localDir %>/js'
,
expand: true
cwd: '<%= srcDir %>/css'
src: ['**/*']
dest: '<%= localDir %>/css'
,
expand: true
cwd: '<%= srcDir %>/font'
src: ['**/*']
dest: '<%= localDir %>/font'
,
expand: true
cwd: '<%= srcDir %>/img'
src: ['**/*']
dest: '<%= localDir %>/img'
,
expand: true
src: ['bower.json', 'package.json']
dest: '<%= localDir %>'
]
build:
files: [
expand: true
cwd: '<%= srcDir %>/templates'
src: ['**/*']
dest: '<%= buildDir %>/js/templates'
,
expand: true
cwd: '<%= srcDir %>/js'
src: ['**/*']
dest: '<%= buildDir %>/js'
]
dist:
files: [
expand: true
cwd: '<%= srcDir %>/css'
src: ['**/*']
dest: '<%= distDir %>/css'
,
expand: true
cwd: '<%= srcDir %>/font'
src: ['**/*']
dest: '<%= distDir %>/font'
,
expand: true
cwd: '<%= srcDir %>/img'
src: ['**/*']
dest: '<%= distDir %>/img'
,
expand: true
src: ['bower.json', 'package.json']
dest: '<%= distDir %>'
]
symlink:
options:
type: 'dir'
templates:
relativeSrc: '../../<%= srcDir %>/templates'
dest: '<%= localDir %>/js/templates'
requirejs:
options:
baseUrl: '.'
inlineText: true
preserveLicenseComments: false
generateSourceMaps: true
wrap: false
logLevel: 1
throwWhen:
optimize: true
# RequireJS plugin configs
config:
tpl:
variable: 'data'
modules: [
name: pkg.name
exclude: [
'jquery'
'backbone'
'underscore'
'marionette'
'highcharts'
'bootstrap'
'json2'
]
]
# Post analysis of built files
done: (done, output) ->
dups = require('rjs-build-analysis').duplicates(output)
if dups.length > 0
grunt.log.subhead 'Duplicates found in RequireJS build:'
grunt.log.warn dups
done new Error('r.js built duplicate modules, please check the `excludes` option.')
done()
dist:
options:
appDir: 'build/js'
dir: 'dist/js'
optimize: 'uglify2'
removeCombined: true
generateSourceMaps: false
clean:
local: ['local']
build: ['build']
dist: ['dist']
postdist: [
'<%= distDir %>/js/templates'
]
release: [
'<%= localDir %>/js/build.txt'
'<%= localDir %>/js/**/**/**/**/*.map'
'<%= localDir %>/css/**/*.map'
'<%= distDir %>/js/build.txt'
]
jasmine:
options:
specs: '<%= specDir %>/**/**/**/*.js'
host: 'http://127.0.0.1:8126'
keepRunner: true
template: require('grunt-template-jasmine-requirejs')
local:
options:
templateOptions:
requireConfig:
baseUrl: '<%= localDir %>/js'
config:
tpl:
variable: 'data'
dist:
options:
templateOptions:
requireConfig:
baseUrl: '<%= distDir %>/js'
config:
tpl:
variable: 'data'
grunt.loadNpmTasks 'grunt-contrib-jasmine'
grunt.loadNpmTasks 'grunt-contrib-watch'
grunt.loadNpmTasks 'grunt-contrib-coffee'
grunt.loadNpmTasks 'grunt-contrib-sass'
grunt.loadNpmTasks 'grunt-contrib-requirejs'
grunt.loadNpmTasks 'grunt-contrib-copy'
grunt.loadNpmTasks 'grunt-contrib-clean'
grunt.loadNpmTasks 'grunt-symlink'
grunt.registerMultiTask 'serve', 'Run a Node server for testing', ->
http = require('http')
path = require('path')
url = require('url')
fs = require('fs')
contentTypes =
'.js': 'text/javascript'
'.css': 'text/css'
'.html': 'text/html'
options = @options
hostname: 'localhost'
base: '.'
port: 8125
keepalive: false
serveResponse = (filename, response) ->
extname = path.extname(filename)
contentType = contentTypes[extname] or 'text/plain'
response.writeHead 200,
'Content-Type': contentType
stream = fs.createReadStream(filename)
stream.pipe response
serve404 = (filename, response) ->
response.writeHead 404
response.end()
serve500 = (filename, response) ->
response.writeHead 500
response.end()
server = http.createServer (request, response) ->
uri = url.parse(request.url).pathname
filename = path.join(options.base, uri)
fs.exists filename, (exists) ->
if exists
fs.readFile filename, (error, content) ->
if error
if error.code is 'EISDIR'
filename += 'index.html'
fs.exists filename, (exists) ->
if exists
fs.readFile filename, (error, content) ->
if error
serve500(filename, response)
else
serveResponse(filename, response)
else
serve404(filename, response)
else
serve500(filename, response)
else
serveResponse(filename, response)
else
serve404(filename, response)
if options.hostname is '*'
options.hostname = null
if options.port is '?'
options.port = 0
done = @async()
server.listen(options.port, options.hostname)
.on 'listening', ->
address = server.address()
hostname = server.hostname or 'localhost'
if not options.keepalive
done()
else
grunt.log.writeln "Listening on #{ hostname }:#{ address.port }..."
.on 'error', (error) ->
if error.code is 'EADDRINUSE'
grunt.fatal("Port #{ options.port } is already in use by another process.")
else
grunt.fatal(error)
grunt.registerTask 'local', 'Creates a build for local development and testing', [
'sass:local'
'coffee:local'
'copy:local'
'symlink'
'jasmine:local:build'
]
grunt.registerTask 'dist', 'Creates a build for distribution', [
'clean:build'
'coffee:build'
'copy:build'
'clean:dist'
'requirejs:dist'
'sass:dist'
'copy:dist'
'clean:postdist'
]
grunt.registerTask 'work', 'Performs the initial local build and starts a watch process', [
'local'
'watch'
]
grunt.registerTask 'test', 'Compiles local CoffeeScript files and uses Jasmine', [
'coffee:local'
'copy:local'
'symlink'
'serve:jasmine'
'jasmine:local'
]
changeVersion = (fname, version) ->
contents = grunt.file.readJSON(fname)
current = contents.version
contents.version = version
grunt.file.write(fname, JSON.stringify(contents, null, 2))
grunt.log.ok "#{ fname }: #{ current } => #{ version }"
replaceVersion = (fname, current, version) ->
options = encoding: 'utf8'
content = grunt.file.read(fname, options)
regexp = new RegExp("version: '#{ current }'")
if not regexp.test(content)
grunt.fatal('File contents does not match version')
content = content.replace(regexp, "version: '#{ version }'")
grunt.file.write(fname, content, options)
grunt.log.ok "#{ fname }: #{ current } => #{ version }"
# Uses package.json as the canonical version
grunt.registerTask 'bump-final', 'Updates the version to final', ->
svutil = require('semver-utils')
current = pkg.version
version = svutil.parse(pkg.version)
# Ensure it is able to be bumped
if version.release isnt 'beta'
grunt.fatal("Version #{ current } not beta. Is this ready for release?")
# Remove release and build strings
version.release = ''
version.build = ''
pkg.version = svutil.stringify(version)
replaceVersion('src/js/cilantro/core.js', current, pkg.version)
for fname in ['package.json', 'bower.json']
changeVersion(fname, pkg.version)
grunt.registerTask 'bump-patch', 'Updates the version to next patch-release', ->
svutil = require('semver-utils')
current = pkg.version
version = svutil.parse(pkg.version)
# Ensure it is able to be bumped
console.log(version.release)
if version.release
grunt.fatal("Version #{ current } not final. Should this be bumped to a pre-release?")
# Remove release and build strings
version.patch = '' + (parseInt(version.patch, 10) + 1)
version.release = 'beta'
version.build = ''
pkg.version = svutil.stringify(version)
replaceVersion('src/js/cilantro/core.js', current, pkg.version)
for fname in ['package.json', 'bower.json']
changeVersion(fname, pkg.version)
run 'git add bower.json package.json src/js/cilantro/core.js'
run "git commit -m '#{ [version.major, version.minor, version.patch].join('.') } Bump'"
grunt.registerTask 'tag-release', 'Create a release on master', ->
run 'git add bower.json package.json src/js/cilantro/core.js'
run "git commit -m '#{ pkg.version } Release'"
run "git tag #{ pkg.version }"
grunt.registerTask 'release-binaries', 'Create a release binary for upload', ->
releaseDirName = "#{ pkg.name }-#{ pkg.version }"
run "rm -rf #{ pkg.name }"
run "mkdir -p #{ pkg.name }"
run "cp -r dist/* #{ pkg.name }"
run "zip -r #{ releaseDirName }.zip #{ pkg.name }"
run "tar -Hzcf #{ releaseDirName }.tar.gz #{ pkg.name }"
run "rm -rf #{ pkg.name }"
run "mkdir -p #{ pkg.name }"
run "cp -r local/* #{ pkg.name }"
run "zip -r #{ releaseDirName }-src.zip #{ pkg.name }"
run "tar -Hzcf #{ releaseDirName }-src.tar.gz #{ pkg.name }"
run "rm -rf #{ pkg.name }"
grunt.registerTask 'release-help', 'Prints the post-release steps', ->
grunt.log.ok 'Push the code and tags: git push && git push --tags'
grunt.log.ok "Go to #{ pkg.homepage }/releases to update the release descriptions and upload the binaries"
grunt.registerTask 'release', 'Builds the distribution files, creates the release binaries, and creates a Git tag', [
'bump-final'
'local'
'dist'
'clean:release'
'release-binaries'
'tag-release'
'release-help'
'bump-patch'
]