自定义 Rake 任务无法在同一个 Rakefile unike 中被调用两次 Rake::TestTask

Custom Rake task unable to be called twice in same Rakefile unike Rake::TestTask

我最近读了 an "older", 2009, article about how to make a Custom Rake tasks. So far it works for the first iteration, but I saw that Rake::TestTask can be called twice,所以我想我可以做到,但是我的 name attr_accessor 没有接收到我传递给它的符号。

require 'rake'
require 'rake/tasklib'

module Phil
  class FooTask < Rake::TaskLib
    attr_accessor :name
    attr_accessor :data
    attr_accessor :task_dependencies

    def initialize(name = :task, task_dependencies)
      @name = name
      @data = nil
      yield self if block_given?
      @task_dependencies = task_dependencies
      define
    end

    def define
      desc "Run the #{@name} task"
      task @name => @task_dependencies do
        puts 'Some Test being Printed'
        puts @data
        sh 'echo blah'
      end
      self
    end
  end
end

Phil::FooTask.new :foo, [:call_me_first, :call_me_second]

task :call_me_first do
  puts 'I am called first because I am a dependency'
end

task :call_me_second do
  puts 'I am called second because Im also a dependency'
end

Phil::FooTask.new(:stuff) do |t|
  t.data = 'I am a stuff task.'
end

以下是我得到的结果

C:\Users\user01\Desktop
λ rake --tasks
rake foo    # Run the foo task
rake stuff  # Run the stuff task

C:\Users\user01\Desktop
λ rake foo
I am called first because I am a dependency
I am called second because Im also a dependency
Some Test being Printed

echo blah
blah

C:\Users\user01\Desktop
λ rake stuff --trace
** Invoke stuff (first_time)
rake aborted!
Don't know how to build task '{}' (see --tasks)
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task_manager.rb:58:in `[]'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:61:in `lookup_prerequisite'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:57:in `block in prerequisite_tasks'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:57:in `map'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:57:in `prerequisite_tasks'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:214:in `invoke_prerequisites'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:193:in `block in invoke_with_call_chain'
C:/tools/ruby23/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:187:in `invoke_with_call_chain'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:180:in `invoke'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:152:in `invoke_task'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `block (2 levels) in top_level'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `each'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:108:in `block in top_level'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:117:in `run_with_threads'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:102:in `top_level'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:80:in `block in run'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:77:in `run'
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
C:/tools/ruby23/bin/rake:22:in `load'
C:/tools/ruby23/bin/rake:22:in `<main>'
Tasks: TOP => stuff

我不确定 Rake 任务失败的原因。即使阅读了 Rake's TestTask 代码,似乎我几乎每件事都做对了。我还要说我不是 Ruby 和所有小技巧方面的专业人士。

在方法声明的末尾保留带默认值的参数!

rake stuff 引发错误的原因是您的 Phil::FooTask.new(:stuff) do |t| 调用。您的 initialize 方法需要两个参数。错误出在您声明 name = :task 的构造函数中,它的默认值为 :task。但是,第二个参数 task_dependencies 是预期的。这是您在使用名称 :stuff 初始化任务时缺少的第二个参数。

按如下方式修改您的 initialize 方法声明:

def initialize(name = :task, task_dependencies = [])

那么你应该在调用 rake -T:

时看到正确的任务
rake foo    # Run the foo task
rake stuff  # Run the stuff task