fs = require 'fs'
path = require 'path'
helpers = require './helpers'
optparse = require './optparse'
CoffeeScript = require './'
外部依存関係。
fs = require 'fs'
path = require 'path'
helpers = require './helpers'
optparse = require './optparse'
CoffeeScript = require './'
.coffee拡張子を登録する。
CoffeeScript.register()
定義済みのタスク、受け入れられるオプションなどのリストを追跡する。
tasks = {}
options = {}
switches = []
oparse = null
Cakefileが直接使用するために最上位のCake機能をミックスする。
helpers.extend global,
短い名前、オプションの文章による説明、アクション自体として実行する関数でCakeタスクを定義する。
task: (name, description, action) ->
[action, description] = [description, action] unless action
tasks[name] = {name, description, action}
Cakefileが受け付けるオプションを定義する。渡されたすべてのコマンドラインオプションを含む解析されたオプションハッシュは、アクションに対する最初の引数として使用可能になる。
option: (letter, flag, description) ->
switches.push [letter, flag, description]
現在のCakefileで別のタスクを呼び出す。
invoke: (name) ->
missingTask name unless tasks[name]
tasks[name].action options
cake
を実行する。順番に渡されたすべてのタスクを実行する。Nodeの非同期処理により、タスクが期待とは異なる順序で実行される可能性があることに注意してください。タスクが渡されないと、ヘルプ画面を出力します。サブディレクトリからCakeタスクを実行するときは、元のディレクトリ名の参照を保持します。
exports.run = ->
global.__originalDirname = fs.realpathSync '.'
process.chdir cakefileDirectory __originalDirname
args = process.argv[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
Cakeタスクのリストを、rake -T
に似た形式で表示する。
printTasks = ->
relative = path.relative or path.resolve
cakefilePath = path.join 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
無効なタスク/オプションを使用しようとしている場合はエラーを表示して終了します。
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}"
cake
が呼び出されたら、現在およびすべての親ディレクトリを検索して関連するCakefileを見つけます。
cakefileDirectory = (dir) ->
return dir if fs.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()}"