Skip to content
Newer
Older
100644 248 lines (206 sloc) 8.35 KB
88847df @jashkenas tests are now passing on symbology
authored
1 fs = require 'fs'
bb05e07 @jashkenas light refactors + tweaks
authored
2 path = require 'path'
ab17f41 @timbertson Moved src/*.coffee under src/coffee-script/ so that lib can be added …
timbertson authored
3 {extend} = require './lib/coffee-script/helpers'
88847df @jashkenas tests are now passing on symbology
authored
4 CoffeeScript = require './lib/coffee-script'
5 {spawn, exec} = require 'child_process'
2f389f1 @jashkenas beginnings of a build script
authored
6
e4db6d4 @jashkenas Updated Cakefile from Tim_Smart with colorized, explained 'cake install'
authored
7 # ANSI Terminal Colors.
c20a8ff @disnet using NODE_DISABLE_COLORS instead of flag
disnet authored
8 enableColors = no
9 unless process.platform is 'win32'
10 enableColors = not process.env.NODE_DISABLE_COLORS
11
0ded5b3 @disnet removing old changes
disnet authored
12 bold = red = green = reset = ''
13 if enableColors
3a694d7 @geraldalewis Issue #1547 'use strict' octal escape sequences prohibited
geraldalewis authored
14 bold = '\x1B[0;1m'
15 red = '\x1B[0;31m'
16 green = '\x1B[0;32m'
17 reset = '\x1B[0m'
e4db6d4 @jashkenas Updated Cakefile from Tim_Smart with colorized, explained 'cake install'
authored
18
2c7f6d8 @jashkenas switching to UglifyJS for browser version of CoffeeScript.
authored
19 # Built file header.
20 header = """
21 /**
22 * CoffeeScript Compiler v#{CoffeeScript.VERSION}
23 * http://coffeescript.org
24 *
c44d9ae @jashkenas CoffeeScript 1.0.1
authored
25 * Copyright 2011, Jeremy Ashkenas
2c7f6d8 @jashkenas switching to UglifyJS for browser version of CoffeeScript.
authored
26 * Released under the MIT License
27 */
28 """
29
32bd1df @satyr rewrote "cake bench" to see more consistent results
satyr authored
30 sources = [
c2a43f7 @michaelficarra moved `src/coffee-script/*.coffee` to `src` as per discussion in #1246
michaelficarra authored
31 'coffee-script', 'grammar', 'helpers'
32 'lexer', 'nodes', 'rewriter', 'scope'
33 ].map (filename) -> "src/#{filename}.coffee"
32bd1df @satyr rewrote "cake bench" to see more consistent results
satyr authored
34
448ed36 @jashkenas got a build script for rebuilding the compiler, too
authored
35 # Run a CoffeeScript through our node/coffee interpreter.
d2d02bf @michaelficarra Cakefile: build:full exits unsuccessfully when tests or builds fail;
michaelficarra authored
36 run = (args, cb) ->
7e32c32 @hickford build on Windows - fix spawn command by calling node explicitly
hickford authored
37 proc = spawn 'node', ['bin/coffee'].concat(args)
26a115a @jashkenas Removing the mixed-in sys/util module. Switching from 'puts' to conso…
authored
38 proc.stderr.on 'data', (buffer) -> console.log buffer.toString()
d2d02bf @michaelficarra Cakefile: build:full exits unsuccessfully when tests or builds fail;
michaelficarra authored
39 proc.on 'exit', (status) ->
40 process.exit(1) if status != 0
41 cb() if typeof cb is 'function'
2f389f1 @jashkenas beginnings of a build script
authored
42
e4db6d4 @jashkenas Updated Cakefile from Tim_Smart with colorized, explained 'cake install'
authored
43 # Log a message with a color.
88847df @jashkenas tests are now passing on symbology
authored
44 log = (message, color, explanation) ->
26a115a @jashkenas Removing the mixed-in sys/util module. Switching from 'puts' to conso…
authored
45 console.log color + message + reset + ' ' + (explanation or '')
e4db6d4 @jashkenas Updated Cakefile from Tim_Smart with colorized, explained 'cake install'
authored
46
5c7526a @jashkenas moving some of the fs methods over to sync methods, where it's alrigh…
authored
47 option '-p', '--prefix [DIR]', 'set the installation prefix for `cake install`'
17ea48c @jashkenas first draft of options for Cakefiles, using optparse.coffee, as well …
authored
48
49 task 'install', 'install CoffeeScript into /usr/local (or --prefix)', (options) ->
88847df @jashkenas tests are now passing on symbology
authored
50 base = options.prefix or '/usr/local'
9df3e6a @jashkenas first step towards requiring #{ ... } interpolation -- removing naked…
authored
51 lib = "#{base}/lib/coffee-script"
52 bin = "#{base}/bin"
88847df @jashkenas tests are now passing on symbology
authored
53 node = "~/.node_libraries/coffee-script"
26a115a @jashkenas Removing the mixed-in sys/util module. Switching from 'puts' to conso…
authored
54 console.log "Installing CoffeeScript to #{lib}"
55 console.log "Linking to #{node}"
56 console.log "Linking 'coffee' to #{bin}/coffee"
0490cb2 @jashkenas added experimental (but working) method to install CoffeeScript: 'sud…
authored
57 exec([
9df3e6a @jashkenas first step towards requiring #{ ... } interpolation -- removing naked…
authored
58 "mkdir -p #{lib} #{bin}"
59 "cp -rf bin lib LICENSE README package.json src #{lib}"
6d3f272 @jashkenas ln -sfn ... part of Issue #971
authored
60 "ln -sfn #{lib}/bin/coffee #{bin}/coffee"
61 "ln -sfn #{lib}/bin/cake #{bin}/cake"
ce4e00e @jashkenas mistaken camelCase 'nodeLibraries' from the great under_score purge .…
authored
62 "mkdir -p ~/.node_libraries"
ab17f41 @timbertson Moved src/*.coffee under src/coffee-script/ so that lib can be added …
timbertson authored
63 "ln -sfn #{lib}/lib/coffee-script #{node}"
93f644f @jashkenas finishing the second half of prefix installs. Using readLink to refer…
authored
64 ].join(' && '), (err, stdout, stderr) ->
02e94cd @jashkenas removing forgotten print's
authored
65 if err then console.log stderr.trim() else log 'done', green
93f644f @jashkenas finishing the second half of prefix installs. Using readLink to refer…
authored
66 )
0490cb2 @jashkenas added experimental (but working) method to install CoffeeScript: 'sud…
authored
67
68
d2d02bf @michaelficarra Cakefile: build:full exits unsuccessfully when tests or builds fail;
michaelficarra authored
69 task 'build', 'build the CoffeeScript language from source', build = (cb) ->
88847df @jashkenas tests are now passing on symbology
authored
70 files = fs.readdirSync 'src'
c2a43f7 @michaelficarra moved `src/coffee-script/*.coffee` to `src` as per discussion in #1246
michaelficarra authored
71 files = ('src/' + file for file in files when file.match(/\.coffee$/))
72 run ['-c', '-o', 'lib/coffee-script'].concat(files), cb
448ed36 @jashkenas got a build script for rebuilding the compiler, too
authored
73
bedc005 @jashkenas Adding a CoffeeScript equivalent to Rake/Make/Jake (Cake, naturally),…
authored
74
0ded5b3 @disnet removing old changes
disnet authored
75 task 'build:full', 'rebuild the source twice, and run the tests', ->
d2d02bf @michaelficarra Cakefile: build:full exits unsuccessfully when tests or builds fail;
michaelficarra authored
76 build ->
77 build ->
c88cb5c @michaelficarra Another attempt to fix #1580... still haven't come up with a good way to
michaelficarra authored
78 csPath = './lib/coffee-script'
79 delete require.cache[require.resolve csPath]
0ded5b3 @disnet removing old changes
disnet authored
80 unless runTests require csPath
e5b77b1 @michaelficarra fixes #580 by `delete`ing the compiler from `require.cache` and `requ…
michaelficarra authored
81 process.exit 1
bcf7b3f @jashkenas CoffeeScript 0.5.5, with string interpolation and internal documentation
authored
82
83
ff25361 @jashkenas removing the build:parser -> build dependency in the Cakefile -- the …
authored
84 task 'build:parser', 'rebuild the Jison parser (run build first)', ->
c1c9de4 @jashkenas utils -> util (again)
authored
85 extend global, require('util')
e56af49 @jashkenas removing our vendor'd copy of Jison ... you should install it from NP…
authored
86 require 'jison'
333daf3 fix path to grammar file
Mike Blume authored
87 parser = require('./lib/coffee-script/grammar').parser
8c5027a fix the output path too
Mike Blume authored
88 fs.writeFile 'lib/coffee-script/parser.js', parser.generate()
2f389f1 @jashkenas beginnings of a build script
authored
89
bedc005 @jashkenas Adding a CoffeeScript equivalent to Rake/Make/Jake (Cake, naturally),…
authored
90
b674163 @jashkenas adding build:ultraviolet and build:underscore tasks to the Cakefile
authored
91 task 'build:ultraviolet', 'build and install the Ultraviolet syntax highlighter', ->
7499535 @jashkenas updating the Cakefile for the new tmbundle location.
authored
92 exec 'plist2syntax ../coffee-script-tmbundle/Syntaxes/CoffeeScript.tmLanguage', (err) ->
93 throw err if err
b674163 @jashkenas adding build:ultraviolet and build:underscore tasks to the Cakefile
authored
94 exec 'sudo mv coffeescript.yaml /usr/local/lib/ruby/gems/1.8/gems/ultraviolet-0.10.2/syntax/coffeescript.syntax'
95
96
c1427d6 @jashkenas adding a minified combined coffee-script.js. Include it on the page, …
authored
97 task 'build:browser', 'rebuild the merged script for inclusion in the browser', ->
2c7f6d8 @jashkenas switching to UglifyJS for browser version of CoffeeScript.
authored
98 code = ''
99 for name in ['helpers', 'rewriter', 'lexer', 'parser', 'scope', 'nodes', 'coffee-script', 'browser']
100 code += """
101 require['./#{name}'] = new function() {
102 var exports = this;
ab17f41 @timbertson Moved src/*.coffee under src/coffee-script/ so that lib can be added …
timbertson authored
103 #{fs.readFileSync "lib/coffee-script/#{name}.js"}
2c7f6d8 @jashkenas switching to UglifyJS for browser version of CoffeeScript.
authored
104 };
105 """
7ae284f @jashkenas Allowing an environment variable of MINIFY=false to disable minificat…
authored
106 code = """
3564f51 @samcday Added AMD support.
samcday authored
107 (function(root) {
108 var CoffeeScript = function() {
109 function require(path){ return require[path]; }
110 #{code}
ad39150 @jashkenas Fixing crappy JS AMD code.
authored
111 return require['./coffee-script'];
3564f51 @samcday Added AMD support.
samcday authored
112 }();
113
ad39150 @jashkenas Fixing crappy JS AMD code.
authored
114 if (typeof define === 'function' && define.amd) {
115 define(function() { return CoffeeScript; });
116 } else {
117 root.CoffeeScript = CoffeeScript;
118 }
3564f51 @samcday Added AMD support.
samcday authored
119 }(this));
2c7f6d8 @jashkenas switching to UglifyJS for browser version of CoffeeScript.
authored
120 """
7ae284f @jashkenas Allowing an environment variable of MINIFY=false to disable minificat…
authored
121 unless process.env.MINIFY is 'false'
122 {parser, uglify} = require 'uglify-js'
123 code = uglify.gen_code uglify.ast_squeeze uglify.ast_mangle parser.parse code
2c7f6d8 @jashkenas switching to UglifyJS for browser version of CoffeeScript.
authored
124 fs.writeFileSync 'extras/coffee-script.js', header + '\n' + code
150a8a1 @jashkenas A bit more explanation of what's actually going on with build:browser
authored
125 console.log "built ... running browser tests:"
2c7f6d8 @jashkenas switching to UglifyJS for browser version of CoffeeScript.
authored
126 invoke 'test:browser'
c1427d6 @jashkenas adding a minified combined coffee-script.js. Include it on the page, …
authored
127
128
5b3ef78 @jashkenas adding a doc:source Cake task to document CoffeeScript's internals
authored
129 task 'doc:site', 'watch and continually rebuild the documentation for the website', ->
0e5dd3d @jashkenas First draft of recompiling examples for 0.9.9
authored
130 exec 'rake doc', (err) ->
131 throw err if err
c1427d6 @jashkenas adding a minified combined coffee-script.js. Include it on the page, …
authored
132
133
5b3ef78 @jashkenas adding a doc:source Cake task to document CoffeeScript's internals
authored
134 task 'doc:source', 'rebuild the internal documentation', ->
c2a43f7 @michaelficarra moved `src/coffee-script/*.coffee` to `src` as per discussion in #1246
michaelficarra authored
135 exec 'docco src/*.coffee && cp -rf docs documentation && rm -r docs', (err) ->
e267226 @jashkenas commenting coffee-script.coffee for documentation
authored
136 throw err if err
5b3ef78 @jashkenas adding a doc:source Cake task to document CoffeeScript's internals
authored
137
138
a3e1693 @jashkenas waypoint -- docc'd down to the SplatNode
authored
139 task 'doc:underscore', 'rebuild the Underscore.coffee documentation page', ->
25438be @jashkenas new docs for underscore
authored
140 exec 'docco examples/underscore.coffee && cp -rf docs documentation && rm -r docs', (err) ->
141 throw err if err
a3e1693 @jashkenas waypoint -- docc'd down to the SplatNode
authored
142
0ded5b3 @disnet removing old changes
disnet authored
143 task 'bench', 'quick benchmark of compilation time', ->
ab17f41 @timbertson Moved src/*.coffee under src/coffee-script/ so that lib can be added …
timbertson authored
144 {Rewriter} = require './lib/coffee-script/rewriter'
1a9a48c @satyr rewrote "cake bench" to time per parsing stage
satyr authored
145 co = sources.map((name) -> fs.readFileSync name).join '\n'
835244f @jashkenas slight reformats to cake bench
authored
146 fmt = (ms) -> " #{bold}#{ " #{ms}".slice -4 }#{reset} ms"
bcbf9f7 @satyr "cake bench" now shows total time spent
satyr authored
147 total = 0
1a9a48c @satyr rewrote "cake bench" to time per parsing stage
satyr authored
148 now = Date.now()
bcbf9f7 @satyr "cake bench" now shows total time spent
satyr authored
149 time = -> total += ms = -(now - now = Date.now()); fmt ms
1a9a48c @satyr rewrote "cake bench" to time per parsing stage
satyr authored
150 tokens = CoffeeScript.tokens co, rewrite: false
bcbf9f7 @satyr "cake bench" now shows total time spent
satyr authored
151 console.log "Lex #{time()} (#{tokens.length} tokens)"
1a9a48c @satyr rewrote "cake bench" to time per parsing stage
satyr authored
152 tokens = new Rewriter().rewrite tokens
bcbf9f7 @satyr "cake bench" now shows total time spent
satyr authored
153 console.log "Rewrite#{time()} (#{tokens.length} tokens)"
1a9a48c @satyr rewrote "cake bench" to time per parsing stage
satyr authored
154 nodes = CoffeeScript.nodes tokens
bcbf9f7 @satyr "cake bench" now shows total time spent
satyr authored
155 console.log "Parse #{time()}"
1a9a48c @satyr rewrote "cake bench" to time per parsing stage
satyr authored
156 js = nodes.compile bare: true
bcbf9f7 @satyr "cake bench" now shows total time spent
satyr authored
157 console.log "Compile#{time()} (#{js.length} chars)"
835244f @jashkenas slight reformats to cake bench
authored
158 console.log "total #{ fmt total }"
a3e1693 @jashkenas waypoint -- docc'd down to the SplatNode
authored
159
7e22568 @jashkenas making the 'cake loc' task reflect the lines of code in the compiler …
authored
160 task 'loc', 'count the lines of source code in the CoffeeScript compiler', ->
89cd25a @jashkenas finished moving over string and regex interpolation to use '#' instea…
authored
161 exec "cat #{ sources.join(' ') } | grep -v '^\\( *#\\|\\s*$\\)' | wc -l | tr -s ' '", (err, stdout) ->
02e94cd @jashkenas removing forgotten print's
authored
162 console.log stdout.trim()
7987d4e @jashkenas adding a cake task to check loc
authored
163
164
b9c2236 @jashkenas Merging in MichaelFicarra's refactorTests branch.
authored
165 # Run the CoffeeScript test suite.
0ded5b3 @disnet removing old changes
disnet authored
166 runTests = (CoffeeScript) ->
b9c2236 @jashkenas Merging in MichaelFicarra's refactorTests branch.
authored
167 startTime = Date.now()
168 currentFile = null
09c23a5 @michaelficarra majorly enhanced test output
michaelficarra authored
169 passedTests = 0
b9c2236 @jashkenas Merging in MichaelFicarra's refactorTests branch.
authored
170 failures = []
a4958e7 @jashkenas whitespace
authored
171
683b3af @michaelficarra cake test: count number of complete tests that pass, not assertions
michaelficarra authored
172 global[name] = func for name, func of require 'assert'
31892e1 @jashkenas Issue #959 (and countless others) Removing the loop-block-scoped magi…
authored
173
b9c2236 @jashkenas Merging in MichaelFicarra's refactorTests branch.
authored
174 # Convenience aliases.
159150c @satyr "cake test" now wraps all of the "assert" module to update test counts
satyr authored
175 global.CoffeeScript = CoffeeScript
b9c2236 @jashkenas Merging in MichaelFicarra's refactorTests branch.
authored
176
177 # Our test helper function for delimiting different test cases.
09c23a5 @michaelficarra majorly enhanced test output
michaelficarra authored
178 global.test = (description, fn) ->
b9c2236 @jashkenas Merging in MichaelFicarra's refactorTests branch.
authored
179 try
6421c86 @michaelficarra finished reorganizing test suite
michaelficarra authored
180 fn.test = {description, currentFile}
181 fn.call(fn)
683b3af @michaelficarra cake test: count number of complete tests that pass, not assertions
michaelficarra authored
182 ++passedTests
09c23a5 @michaelficarra majorly enhanced test output
michaelficarra authored
183 catch e
d42f7da @jashkenas Issue #943 -- splices with expressions.
authored
184 e.description = description if description?
185 e.source = fn.toString() if fn.toString?
d2d02bf @michaelficarra Cakefile: build:full exits unsuccessfully when tests or builds fail;
michaelficarra authored
186 failures.push filename: currentFile, error: e
a4958e7 @jashkenas whitespace
authored
187
782de87 @michaelficarra updated comment for arrayEqual function, mentioning that it tests for
michaelficarra authored
188 # See http://wiki.ecmascript.org/doku.php?id=harmony:egal
5ae4e06 @michaelficarra cake test: change `eq` to use egal semantics, paralleling `arrayEq`
michaelficarra authored
189 egal = (a, b) ->
dd11528 @michaelficarra changed occurrences of deepEqual to new, self-defined arrayEqual for
michaelficarra authored
190 if a is b
191 a isnt 0 or 1/a is 1/b
5ae4e06 @michaelficarra cake test: change `eq` to use egal semantics, paralleling `arrayEq`
michaelficarra authored
192 else
193 a isnt a and b isnt b
194
195 # A recursive functional equivalence helper; uses egal for testing equivalence.
196 arrayEgal = (a, b) ->
197 if egal a, b then yes
dd11528 @michaelficarra changed occurrences of deepEqual to new, self-defined arrayEqual for
michaelficarra authored
198 else if a instanceof Array and b instanceof Array
199 return no unless a.length is b.length
5ae4e06 @michaelficarra cake test: change `eq` to use egal semantics, paralleling `arrayEq`
michaelficarra authored
200 return no for el, idx in a when not arrayEgal el, b[idx]
dd11528 @michaelficarra changed occurrences of deepEqual to new, self-defined arrayEqual for
michaelficarra authored
201 yes
202
5ae4e06 @michaelficarra cake test: change `eq` to use egal semantics, paralleling `arrayEq`
michaelficarra authored
203 global.eq = (a, b, msg) -> ok egal(a, b), msg
204 global.arrayEq = (a, b, msg) -> ok arrayEgal(a,b), msg
dd11528 @michaelficarra changed occurrences of deepEqual to new, self-defined arrayEqual for
michaelficarra authored
205
c6c988a @jashkenas Revert "Ensure test failures get fully outputted."
authored
206 # When all the tests have run, collect and print errors.
207 # If a stacktrace is available, output the compiled function source.
208 process.on 'exit', ->
209 time = ((Date.now() - startTime) / 1000).toFixed(2)
0ded5b3 @disnet removing old changes
disnet authored
210 message = "passed #{passedTests} tests in #{time} seconds#{reset}"
c6c988a @jashkenas Revert "Ensure test failures get fully outputted."
authored
211 return log(message, green) unless failures.length
212 log "failed #{failures.length} and #{message}", red
213 for fail in failures
214 {error, filename} = fail
215 jsFilename = filename.replace(/\.coffee$/,'.js')
216 match = error.stack?.match(new RegExp(fail.file+":(\\d+):(\\d+)"))
217 match = error.stack?.match(/on line (\d+):/) unless match
218 [match, line, col] = match if match
219 console.log ''
220 log " #{error.description}", red if error.description
221 log " #{error.stack}", red
222 log " #{jsFilename}: line #{line ? 'unknown'}, column #{col ? 'unknown'}", red
223 console.log " #{error.source}" if error.source
224 return
225
b9c2236 @jashkenas Merging in MichaelFicarra's refactorTests branch.
authored
226 # Run every test in the `test` folder, recording failures.
d2d02bf @michaelficarra Cakefile: build:full exits unsuccessfully when tests or builds fail;
michaelficarra authored
227 files = fs.readdirSync 'test'
228 for file in files when file.match /\.coffee$/i
229 currentFile = filename = path.join 'test', file
230 code = fs.readFileSync filename
231 try
232 CoffeeScript.run code.toString(), {filename}
233 catch error
234 failures.push {filename, error}
235 return !failures.length
f360fba @sstephenson Add test:browser task for running the test suite against the merged b…
sstephenson authored
236
237
0ded5b3 @disnet removing old changes
disnet authored
238 task 'test', 'run the CoffeeScript language test suite', ->
239 runTests CoffeeScript
f360fba @sstephenson Add test:browser task for running the test suite against the merged b…
sstephenson authored
240
241
0ded5b3 @disnet removing old changes
disnet authored
242 task 'test:browser', 'run the test suite against the merged browser script', ->
f360fba @sstephenson Add test:browser task for running the test suite against the merged b…
sstephenson authored
243 source = fs.readFileSync 'extras/coffee-script.js', 'utf-8'
5236bb2 @sstephenson Remove the window hack in test:browser.
sstephenson authored
244 result = {}
9785fd0 @jashkenas Bumping site.
authored
245 global.testingBrowser = yes
5236bb2 @sstephenson Remove the window hack in test:browser.
sstephenson authored
246 (-> eval source).call result
0ded5b3 @disnet removing old changes
disnet authored
247 runTests result.CoffeeScript
Something went wrong with that request. Please try again.