Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Cake tasks #4440

Merged
merged 9 commits into from
Feb 18, 2017
142 changes: 83 additions & 59 deletions Cakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,45 @@ header = """
*/
"""

# Used in folder names like docs/v1
# Used in folder names like `docs/v1`.
majorVersion = parseInt CoffeeScript.VERSION.split('.')[0], 10


# Build the CoffeeScript language from source.
build = (cb) ->
buildParser = ->
helpers.extend global, require 'util'
require 'jison'
parser = require('./lib/coffee-script/grammar').parser.generate()
# Patch Jison’s output, until https://github.com/zaach/jison/pull/339 is accepted,
# to ensure that require('fs') is only called where it exists.
parser = parser.replace "var source = require('fs')", """
var source = '';
var fs = require('fs');
if (typeof fs !== 'undefined' && fs !== null)
source = fs"""
fs.writeFileSync 'lib/coffee-script/parser.js', parser

buildExceptParser = (cb) ->
files = fs.readdirSync 'src'
files = ('src/' + file for file in files when file.match(/\.(lit)?coffee$/))
run ['-c', '-o', 'lib/coffee-script'].concat(files), cb

build = (cb) ->
buildParser()
buildExceptParser cb

testBuiltCode = (watch = no) ->
csPath = './lib/coffee-script'
csDir = path.dirname require.resolve csPath

for mod of require.cache when csDir is mod[0 ... csDir.length]
delete require.cache[mod]

testResults = runTests require csPath
unless watch
process.exit 1 unless testResults


# Run a CoffeeScript through our node/coffee interpreter.
run = (args, cb) ->
proc = spawn 'node', ['bin/coffee'].concat(args)
Expand All @@ -41,62 +71,23 @@ run = (args, cb) ->
process.exit(1) if status isnt 0
cb() if typeof cb is 'function'


# Log a message with a color.
log = (message, color, explanation) ->
console.log color + message + reset + ' ' + (explanation or '')

option '-p', '--prefix [DIR]', 'set the installation prefix for `cake install`'

task 'install', 'install CoffeeScript into /usr/local (or --prefix)', (options) ->
base = options.prefix or '/usr/local'
lib = "#{base}/lib/coffee-script"
bin = "#{base}/bin"
node = "~/.node_libraries/coffee-script"
console.log "Installing CoffeeScript to #{lib}"
console.log "Linking to #{node}"
console.log "Linking 'coffee' to #{bin}/coffee"
exec([
"mkdir -p #{lib} #{bin}"
"cp -rf bin lib LICENSE README.md package.json src #{lib}"
"ln -sfn #{lib}/bin/coffee #{bin}/coffee"
"ln -sfn #{lib}/bin/cake #{bin}/cake"
"mkdir -p ~/.node_libraries"
"ln -sfn #{lib}/lib/coffee-script #{node}"
].join(' && '), (err, stdout, stderr) ->
if err then console.log stderr.trim() else log 'done', green
)


task 'build', 'build the CoffeeScript language from source', build

task 'build:full', 'rebuild the source twice, and run the tests', ->
build ->
build ->
csPath = './lib/coffee-script'
csDir = path.dirname require.resolve csPath

for mod of require.cache when csDir is mod[0 ... csDir.length]
delete require.cache[mod]
task 'build', 'build the CoffeeScript compiler from source', build

unless runTests require csPath
process.exit 1
task 'build:parser', 'build the Jison parser only', buildParser

task 'build:except-parser', 'build the CoffeeScript compiler, except for the Jison parser', buildExceptParser

task 'build:parser', 'rebuild the Jison parser (run build first)', ->
helpers.extend global, require 'util'
require 'jison'
parser = require('./lib/coffee-script/grammar').parser.generate()
# Patch Jison’s output, until https://github.com/zaach/jison/pull/339 is accepted,
# to ensure that require('fs') is only called where it exists.
parser = parser.replace "var source = require('fs')", """
var source = '';
var fs = require('fs');
if (typeof fs !== 'undefined' && fs !== null)
source = fs"""
fs.writeFileSync 'lib/coffee-script/parser.js', parser

task 'build:full', 'build the CoffeeScript compiler from source twice, and run the tests', ->
build ->
build testBuiltCode

task 'build:browser', 'rebuild the merged script for inclusion in the browser', ->
task 'build:browser', 'build the merged script for inclusion in the browser', ->
code = """
require['../../package.json'] = (function() {
return #{fs.readFileSync "./package.json"};
Expand Down Expand Up @@ -138,7 +129,7 @@ task 'build:browser', 'rebuild the merged script for inclusion in the browser',
invoke 'test:browser'


task 'doc:site', 'watch and continually rebuild the documentation for the website', ->
buildDocs = (watch = no) ->
# Constants
indexFile = 'documentation/index.html'
versionedSourceFolder = "documentation/v#{majorVersion}"
Expand Down Expand Up @@ -211,12 +202,19 @@ task 'doc:site', 'watch and continually rebuild the documentation for the websit
fs.symlinkSync "v#{majorVersion}/index.html", 'docs/index.html'
catch exception

for target in [indexFile, versionedSourceFolder, examplesSourceFolder, sectionsSourceFolder]
fs.watch target, interval: 200, renderIndex
log 'watching...' , green
if watch
for target in [indexFile, versionedSourceFolder, examplesSourceFolder, sectionsSourceFolder]
fs.watch target, interval: 200, renderIndex
log 'watching...', green

task 'doc:site', 'build the documentation for the website', ->
buildDocs()

task 'doc:site:watch', 'watch and continually rebuild the documentation for the website', ->
buildDocs yes

task 'doc:test', 'watch and continually rebuild the browser-based tests', ->

buildDocTests = (watch = no) ->
# Constants
testFile = 'documentation/test.html'
testsSourceFolder = 'test'
Expand Down Expand Up @@ -254,14 +252,40 @@ task 'doc:test', 'watch and continually rebuild the browser-based tests', ->
fs.writeFileSync "#{outputFolder}/test.html", output
log 'compiled', green, "#{testFile} → #{outputFolder}/test.html"

for target in [testFile, testsSourceFolder]
fs.watch target, interval: 200, renderTest
log 'watching...' , green
if watch
for target in [testFile, testsSourceFolder]
fs.watch target, interval: 200, renderTest
log 'watching...', green

task 'doc:test', 'build the browser-based tests', ->
buildDocTests()

task 'doc:test:watch', 'watch and continually rebuild the browser-based tests', ->
buildDocTests yes


buildAnnotatedSource = (watch = no) ->
do generateAnnotatedSource = ->
exec "node_modules/docco/bin/docco src/*.*coffee --output docs/v#{majorVersion}/annotated-source", (err) -> throw err if err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most minor thing ever ... I know that you didn't change anything here, but I got curious. I always thought ./node_modules/.bin/whatever was the way to go when invoking "binaries" installed locally with npm. I guess it doesn't matter. :)

log 'generated', green, "annotated source in docs/v#{majorVersion}/annotated-source/"

if watch
fs.watch 'src/', interval: 200, generateAnnotatedSource
log 'watching...', green

task 'doc:source', 'build the annotated source documentation', ->
buildAnnotatedSource()

task 'doc:source:watch', 'watch and continually rebuild the annotated source documentation', ->
buildAnnotatedSource yes

task 'doc:source', 'rebuild the annotated source documentation', ->
exec "node_modules/docco/bin/docco src/*.*coffee --output docs/v#{majorVersion}/annotated-source", (err) -> throw err if err

task 'release', 'build and test the CoffeeScript source, and build the documentation', ->
invoke 'build:full'
invoke 'build:browser'
invoke 'doc:site'
invoke 'doc:test'
invoke 'doc:source'

task 'bench', 'quick benchmark of compilation time', ->
{Rewriter} = require './lib/coffee-script/rewriter'
Expand Down
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,10 @@ CoffeeScript is a little language that compiles into JavaScript.
If you have the node package manager, npm, installed:

```shell
npm install -g coffee-script
npm install --global coffee-script
```

Leave off the `-g` if you don't wish to install globally. If you don't wish to use npm:

```shell
git clone https://github.com/jashkenas/coffeescript.git
sudo coffeescript/bin/cake install
```
Leave off the `--global` if you don’t wish to install globally.

## Getting Started

Expand All @@ -53,7 +48,7 @@ For documentation, usage, and examples, see: http://coffeescript.org/

To suggest a feature or report a bug: http://github.com/jashkenas/coffeescript/issues

If you'd like to chat, drop by #coffeescript on Freenode IRC.
If youd like to chat, drop by #coffeescript on Freenode IRC.

The source repository: https://github.com/jashkenas/coffeescript.git

Expand Down
6 changes: 3 additions & 3 deletions docs/v1/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2584,9 +2584,9 @@ <h2>Resources</h2><ul>
<li><p><a href="http://github.com/jashkenas/coffeescript/">Source Code</a><br>
Use <code>bin/coffee</code> to test your changes,<br>
<code>bin/cake test</code> to run the test suite,<br>
<code>bin/cake build</code> to rebuild the CoffeeScript compiler, and<br>
<code>bin/cake build:parser</code> to regenerate the Jison parser if you’re working on the grammar.</p>
<p><code>git checkout lib &amp;&amp; bin/cake build:full</code> is a good command to run when you’re working on the core language. It’ll refresh the lib directory (in case you broke something), build your altered compiler, use that to rebuild itself (a good sanity test) and then run all of the tests. If they pass, there’s a good chance you’ve made a successful change.</p>
<code>bin/cake build</code> to rebuild the full CoffeeScript compiler, and<br>
<code>bin/cake build:except-parser</code> to recompile much faster if you’re not editing <code>grammar.coffee</code>.</p>
<p><code>git checkout lib &amp;&amp; bin/cake build:full</code> is a good command to run when you’re working on the core language. It’ll refresh the <code>lib</code> folder (in case you broke something), build your altered compiler, use that to rebuild itself (a good sanity test) and then run all of the tests. If they pass, there’s a good chance you’ve made a successful change.</p>
</li>
<li><a href="v1/test.html">Browser Tests</a><br>
Run CoffeeScript’s test suite in your current browser.</li>
Expand Down
6 changes: 3 additions & 3 deletions documentation/sections/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* [Source Code](http://github.com/jashkenas/coffeescript/)<br>
Use `bin/coffee` to test your changes,<br>
`bin/cake test` to run the test suite,<br>
`bin/cake build` to rebuild the CoffeeScript compiler, and<br>
`bin/cake build:parser` to regenerate the Jison parser if you’re working on the grammar.
`bin/cake build` to rebuild the full CoffeeScript compiler, and<br>
`bin/cake build:except-parser` to recompile much faster if you’re not editing `grammar.coffee`.

`git checkout lib && bin/cake build:full` is a good command to run when you’re working on the core language. It’ll refresh the lib directory (in case you broke something), build your altered compiler, use that to rebuild itself (a good sanity test) and then run all of the tests. If they pass, there’s a good chance you’ve made a successful change.
`git checkout lib && bin/cake build:full` is a good command to run when you’re working on the core language. It’ll refresh the `lib` folder (in case you broke something), build your altered compiler, use that to rebuild itself (a good sanity test) and then run all of the tests. If they pass, there’s a good chance you’ve made a successful change.
* [Browser Tests](v<%= majorVersion %>/test.html)<br>
Run CoffeeScript’s test suite in your current browser.
* [CoffeeScript Issues](http://github.com/jashkenas/coffeescript/issues)<br>
Expand Down