cake.coffee | |
---|---|
Running | |
External dependencies. | fs = require 'fs'
path = require 'path'
helpers = require './helpers'
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. | helpers.extend global, |
Define a Cake task with a short name, an optional sentence description, and the function to run as the action itself. | task: (name, description, action) ->
[action, description] = [description, action] unless action
tasks[name] = {name, description, 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) ->
missingTask name unless tasks[name]
tasks[name].action options |
Run | exports.run = ->
global.__originalDirname = fs.realpathSync '.'
process.chdir cakefileDirectory __originalDirname
args = process.argv.slice 2
CoffeeScript.run fs.readFileSync('Cakefile').toString(), filename: 'Cakefile'
oparse = new optparse.OptionParser switches
return printTasks() unless args.length
try
options = oparse.parse(args)
catch e
return fatalError "#{e}"
invoke arg for arg in options.arguments |
Display the list of Cake tasks in a format similar to | printTasks = ->
cakefilePath = path.join path.relative(__originalDirname, process.cwd()), 'Cakefile'
console.log "#{cakefilePath} defines the following tasks:\n"
for name, task of tasks
spaces = 20 - name.length
spaces = if spaces > 0 then Array(spaces + 1).join(' ') else ''
desc = if task.description then "# #{task.description}" else ''
console.log "cake #{name}#{spaces} #{desc}"
console.log oparse.help() if switches.length |
Print an error and exit when attempting to use an invalid task/option. | fatalError = (message) ->
console.error message + '\n'
console.log 'To see a list of all tasks/options, run "cake"'
process.exit 1
missingTask = (task) -> fatalError "No such task: #{task}" |
When | cakefileDirectory = (dir) ->
return dir if path.existsSync path.join dir, 'Cakefile'
parent = path.normalize path.join dir, '..'
return cakefileDirectory parent unless parent is dir
throw new Error "Cakefile not found in #{process.cwd()}"
|