-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathCakefile
117 lines (108 loc) · 5.54 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
# Cakefile
# batman
# Copyright Shopify, 2011
muffin = require 'muffin'
path = require 'path'
q = require 'q'
glob = require 'glob'
{exec, fork, spawn} = require 'child_process'
option '-w', '--watch', 'continue to watch the files and rebuild them when they change'
option '-c', '--commit', 'operate on the git index instead of the working tree'
option '-d', '--dist', 'compile minified versions of the platform dependent code into lib/dist (build task only)'
option '-m', '--compare', 'compare to git refs (stat task only)'
option '-s', '--coverage', 'run jscoverage during tests and report coverage (test task only)'
task 'build', 'compile Batman.js and all the tools', (options) ->
files = glob.sync('./src/**/*').concat(glob.sync('./tests/lib/*'))
muffin.run
files: files
options: options
map:
'src/batman\.coffee' : (matches) -> muffin.compileScript(matches[0], 'lib/batman.js', options)
'src/batman\.(.+)\.coffee' : (matches) -> muffin.compileScript(matches[0], "lib/batman.#{matches[1]}.js", options)
'src/extras/(.+)\.coffee' : (matches) -> muffin.compileScript(matches[0], "lib/extras/#{matches[1]}.js", options)
'src/tools/batman\.coffee' : (matches) ->
source = muffin.readFile(matches[0], options).then (source) ->
compiled = muffin.compileString(source, options)
compiled = "#!/usr/bin/env node\n\n" + compiled
muffin.writeFile "tools/batman", compiled, muffin.extend({}, options, {mode: 0755})
'src/tools/(.+)\.coffee' : (matches) -> muffin.compileScript(matches[0], "tools/#{matches[1]}.js", options)
'tests/run\.coffee' : (matches) -> muffin.compileScript(matches[0], 'tests/run.js', options)
if options.dist
temp = require 'temp'
tmpdir = temp.mkdirSync()
distDir = "lib/dist"
developmentTransform = require('./tools/build/remove_development_transform').removeDevelopment
# Compile the scripts to the distribution folder by:
# 1. Finding each platform specific batman file of the form `batman.platform.coffee`
# 2. Concatenating each platform specific file with the main Batman source, and storing that in memory
# 3. Compiling each complete platform specific batman distribution into JavaScript in `./lib/dist`
# 4. Minify each complete platform specific distribution in to a .min.js file in `./lib/dist`
compileDist = (platformName) ->
return if platformName in ['node']
joinedCoffeePath = "#{tmpdir}/batman.#{platformName}.coffee"
# Read the platform specific code
platform = muffin.readFile "src/batman.#{platformName}.coffee", options
standard = muffin.readFile 'src/batman.coffee', options
q.join platform, standard, (platformSource, standardSource) ->
# Compile the joined coffeescript to JS
js = muffin.compileString(standardSource + "\n" + platformSource, options)
destination = "#{distDir}/batman#{if platformName is 'solo' then '' else '.' + platformName}.js"
# Write the unminified javascript.
muffin.writeFile(destination, js, options).then ->
options.transform = developmentTransform
muffin.minifyScript(destination, options).then( ->
muffin.notify(destination, "File #{destination} minified.")
)
# Run a task which concats the coffeescript, compiles it, and then minifies it
first = true
muffin.run
files: './src/**/*'
options: options
map:
'src/batman\.(.+)\.coffee': (matches) -> compileDist(matches[1])
'src/batman.coffee' : (matches) ->
done = false
if first
first = false
return
# When the the root batman file changes, compile all the platform files.
platformFiles = glob.sync('./src/batman.*.coffee')
for file in platformFiles
matches = /src\/batman.(.+).coffee/.exec(file)
done = q.wait(done, compileDist(matches[1]))
console.warn done
done
task 'doc', 'build the Docco documentation', (options) ->
muffin.run
files: './src/**/*'
options: options
map:
'src/batman.coffee': (matches) -> muffin.doccoFile(matches[0], options)
task 'test', 'compile Batman.js and the tests and run them on the command line', (options) ->
running = false
muffin.run
files: glob.sync('./src/**/*.coffee').concat(glob.sync('./tests/**/*.coffee'))
options: options
map:
'src/batman(.node)?.coffee' : (matches) -> true
'src/extras/(.+).coffee' : (matches) -> true
'tests/batman/(.+)_(test|helper).coffee' : (matches) -> true
'tests/run.coffee' : (matches) -> muffin.compileScript(matches[0], 'tests/run.js', options)
after: ->
# Spawn a run of the tests in a separate process so if watch mode is enabled the globals used by the runner
# don't leak over.
runPath = path.join(__dirname, 'tests/run.js')
if !running
running = true
child = spawn 'node', ['tests/run.js']
process.on 'exit', exitListener = ->
child.kill()
child.stdout.on 'data', (data) -> process.stdout.write data
child.stderr.on 'data', (data) -> process.stderr.write data
child.on 'exit', (code) ->
process.removeListener 'exit', exitListener
running = false
if !options.watch
process.exit code
task 'stats', 'compile the files and report on their final size', (options) ->
muffin.statFiles(glob.sync('./src/**/*.coffee').concat(glob.sync('./lib/**/*.js')), options)