From bedc005d67c1dc52a0a4909c439a6f6347738213 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 16 Feb 2010 20:42:10 -0500 Subject: [PATCH] Adding a CoffeeScript equivalent to Rake/Make/Jake (Cake, naturally), and implementing all of our build and test tasks in the Cakefile. Run bin/cake to see the tasks. --- tasks.coffee => Cakefile | 42 ++++++++++------------- bin/cake | 7 ++++ lib/coffee_script/cake.js | 70 +++++++++++++++++++++++++++++++++++++++ src/cake.coffee | 39 ++++++++++++++++++++++ 4 files changed, 134 insertions(+), 24 deletions(-) rename tasks.coffee => Cakefile (51%) mode change 100755 => 100644 create mode 100755 bin/cake create mode 100755 lib/coffee_script/cake.js create mode 100644 src/cake.coffee diff --git a/tasks.coffee b/Cakefile old mode 100755 new mode 100644 similarity index 51% rename from tasks.coffee rename to Cakefile index 75d94673..f0f0268e --- a/tasks.coffee +++ b/Cakefile @@ -1,7 +1,3 @@ -# Custom build scripts, replacing the Rakefile. To invoke (for example): -# -# bin/node_coffee -r tasks.coffee -- parser - fs: require 'fs' coffee: require 'coffee-script' @@ -10,39 +6,37 @@ run: (args) -> proc: process.createChildProcess 'bin/node_coffee', args proc.addListener 'error', (err) -> if err then puts err -# Print the usage message for the build scripts. -usage: -> - puts "tasks.coffee usage goes here..." -# Build the CoffeeScript source code -- if you're editing the parser, run -# this before you run "build parser". -build_compiler: -> +task 'build:compiler', 'build the CoffeeScript Compiler source code', -> fs.readdir('src').addCallback (files) -> files: 'src/' + file for file in files when file.match(/\.coffee$/) run ['-o', 'lib/coffee_script'].concat(files) -# Rebuild the Jison parser from the compiled lib/grammar.js file. -build_parser: -> + +task 'build:parser', 'rebuild the Jison parser', -> + invoke 'build:compiler' parser: require('grammar').parser js: parser.generate() parser_path: 'lib/coffee_script/parser.js' fs.open(parser_path, process.O_CREAT | process.O_WRONLY | process.O_TRUNC, parseInt('0755', 8)).addCallback (fd) -> fs.write(fd, js) -# Run the CoffeeScript test suite. -run_tests: -> + +task 'test', 'run the CoffeeScript language test suite', -> process.mixin require 'assert' + test_count: 0 + start_time: new Date() + original_ok: ok + process.mixin { + ok: (args...) -> + test_count += 1 + original_ok(args...) + } + process.addListener 'exit', -> + time: ((new Date() - start_time) / 1000).toFixed(2) + puts '\033[0;32mpassed ' + test_count + ' tests in ' + time + ' seconds\033[0m' fs.readdir('test').addCallback (files) -> for file in files fs.cat('test/' + file).addCallback (source) -> js: coffee.compile source - process.compile js, file - - -switch process.ARGV[0] - when undefined then usage() - when 'compiler' then build_compiler() - when 'parser' then build_parser() - when 'test' then run_tests() - when 'highlighter' then build_highlighter() - when 'underscore' then build_underscore() \ No newline at end of file + process.compile js, file \ No newline at end of file diff --git a/bin/cake b/bin/cake new file mode 100755 index 00000000..f74d3bed --- /dev/null +++ b/bin/cake @@ -0,0 +1,7 @@ +#!/usr/bin/env node -- + +process.mixin(require('sys')); + +require.paths.unshift('./lib/coffee_script'); + +require('cake').run(); diff --git a/lib/coffee_script/cake.js b/lib/coffee_script/cake.js new file mode 100755 index 00000000..f8257ad9 --- /dev/null +++ b/lib/coffee_script/cake.js @@ -0,0 +1,70 @@ +(function(){ + var coffee, fs, path, print_tasks, tasks; + var __hasProp = Object.prototype.hasOwnProperty; + // `cake` is a simplified version of Make (Rake, Jake) for CoffeeScript. + fs = require('fs'); + path = require('path'); + coffee = require('coffee-script'); + tasks = {}; + // Mixin the Cake functionality. + process.mixin({ + // Define a task with a name, a description, and the action itself. + task: function task(name, description, action) { + return tasks[name] = { + name: name, + description: description, + action: action + }; + }, + // Invoke another task in the Cakefile. + invoke: function invoke(name) { + return tasks[name].action(); + } + }); + // Display the list of Cake tasks. + print_tasks = function print_tasks() { + var _a, _b, _c, _d, _e, _f, _g, i, name, spaces, task; + _a = []; _b = tasks; + for (name in _b) if (__hasProp.call(_b, name)) { + task = _b[name]; + _a.push((function() { + spaces = 20 - name.length; + spaces = spaces > 0 ? ((function() { + _c = []; _f = 0; _g = spaces; + for (_e=0, i=_f; (_f <= _g ? i <= _g : i >= _g); (_f <= _g ? i += 1 : i -= 1), _e++) { + _c.push(' '); + } + return _c; + }).call(this)).join('') : ''; + return puts("cake " + name + spaces + ' # ' + task.description); + }).call(this)); + } + return _a; + }; + // The CommandLine handles all of the functionality of the `coffee` utility. + exports.run = function run() { + return path.exists('Cakefile', function(exists) { + var args; + if (!(exists)) { + throw new Error('Cakefile not found in ' + process.cwd()); + } + args = process.ARGV.slice(2, process.ARGV.length); + return fs.cat('Cakefile').addCallback(function(source) { + var _a, _b, _c, arg; + eval(coffee.compile(source)); + if (!(args.length)) { + return print_tasks(); + } + _a = []; _b = args; + for (_c = 0; _c < _b.length; _c++) { + arg = _b[_c]; + if (!(tasks[arg])) { + throw new Error('No such task: "' + arg + '"'); + } + tasks[arg].action(); + } + return _a; + }); + }); + }; +})(); \ No newline at end of file diff --git a/src/cake.coffee b/src/cake.coffee new file mode 100644 index 00000000..8d6c38f1 --- /dev/null +++ b/src/cake.coffee @@ -0,0 +1,39 @@ +# `cake` is a simplified version of Make (Rake, Jake) for CoffeeScript. + +fs: require 'fs' +path: require 'path' +coffee: require 'coffee-script' + +tasks: {} + +# Mixin the Cake functionality. +process.mixin { + + # Define a task with a name, a description, and the action itself. + task: (name, description, action) -> + tasks[name]: {name: name, description: description, action: action} + + # Invoke another task in the Cakefile. + invoke: (name) -> + tasks[name].action() +} + +# Display the list of Cake tasks. +print_tasks: -> + for name, task of tasks + spaces: 20 - name.length + spaces: if spaces > 0 then (' ' for i in [0..spaces]).join('') else '' + puts "cake " + name + spaces + ' # ' + task.description + +# The CommandLine handles all of the functionality of the `coffee` utility. +exports.run: -> + path.exists 'Cakefile', (exists) -> + throw new Error('Cakefile not found in ' + process.cwd()) unless exists + args: process.ARGV[2...process.ARGV.length] + fs.cat('Cakefile').addCallback (source) -> + eval coffee.compile source + return print_tasks() unless args.length + for arg in args + throw new Error('No such task: "' + arg + '"') unless tasks[arg] + tasks[arg].action() +