• ジャンプ先 +
    browser.coffee cake.coffee coffee-script.coffee command.coffee grammar.coffee helpers.coffee index.coffee lexer.coffee nodes.coffee optparse.coffee register.coffee repl.coffee rewriter.coffee scope.litcoffee sourcemap.litcoffee
  • cake.coffee

  • ¶

    cake は CoffeeScript の Make (Rake、Jake) の単純化バージョンです。Cakefile の中で名前と説明でタスクを定義でき、コマンドラインから呼び出すか、他のタスクから呼び出すことができます。

    引数なしでcakeを実行すると、現在のディレクトリーの Cakefile にあるすべてのタスクのリストが出力されます。

  • ¶

    外部依存関係。

    fs           = require 'fs'
    path         = require 'path'
    helpers      = require './helpers'
    optparse     = require './optparse'
    CoffeeScript = require './coffee-script'
  • ¶

    .coffee 拡張子を登録します。

    CoffeeScript.register()
  • ¶

    定義済みタスク、受け入れられたオプションなどのリストをトラッキングします。

    tasks     = {}
    options   = {}
    switches  = []
    oparse    = null
  • ¶

    Cakefiles で直接使用できる最上位の Cake 関数をミックスインします。

    helpers.extend global,
  • ¶

    短い名前、1 行の説明文、アクション自身として実行する関数で Cake タスクを定義します。

      task: (name, description, action) ->
        [action, description] = [description, action] unless action
        tasks[name] = {name, description, action}
  • ¶

    Cakefile で受け付けるオプションを定義します。受け渡されるすべてのコマンドラインオプションを含む、解析されたオプションハッシュが、アクションへの第 1 引数として使用できます。

      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
  • ¶

    rake -T と同様の形式で、Cake タスクのリストを表示します。

    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()}"