cake.coffee | |
---|---|
Running | |
External dependencies. | fs: require 'fs'
path: require 'path'
optparse: require 'optparse'
CoffeeScript: require 'coffee-script' |
Keep track of the list of defined tasks, the accepted options, and so on. | tasks: {}
options: {}
switches: []
oparse: null |
Mixin the top-level Cake functions for Cakefiles to use directly. | process.mixin { |
Define a Cake task with a short name, a sentence description, and the function to run as the action itself. | task: (name, description, action) ->
tasks[name]: {name: name, description: description, action: action} |
Define an option that the Cakefile accepts. The parsed options hash, containing all of the command-line options passed, will be made available as the first argument to the action. | option: (letter, flag, description) ->
switches.push [letter, flag, description] |
Invoke another task in the current Cakefile. | invoke: (name) ->
no_such_task name unless tasks[name]
tasks[name].action options
} |
Run | exports.run: ->
path.exists 'Cakefile', (exists) ->
throw new Error("Cakefile not found in ${process.cwd()}") unless exists
args: process.argv[2...process.argv.length]
CoffeeScript.run fs.readFileSync('Cakefile'), {source: 'Cakefile'}
oparse: new optparse.OptionParser switches
return print_tasks() unless args.length
options: oparse.parse(args)
invoke arg for arg in options.arguments |
Display the list of Cake tasks in a format similar to | print_tasks: ->
puts ''
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}"
puts oparse.help() if switches.length |
Print an error and exit when attempting to all an undefined task. | no_such_task: (task) ->
process.stdio.writeError "No such task: \"$task\"\n"
process.exit 1
|