不知道如何构建任务 - 用于调用其他任务的父任务

时间:2016-07-29 18:36:36

标签: ruby rake ruby-on-rails-5 rake-task

我有一个名为setup_a_new_set_of_snippets的rake任务,如下所示:

load './lib/tasks/fetch_and_create_snippets.rake'
load './lib/tasks/generate_diffs_for_snippets.rake'
load './lib/tasks/cleanup_snippets_with_empty_diffs.rake'

desc "Setup a new set of Snippets"
task :setup_a_new_set_of_snippets, [:repo, :path, :entry_id, :framework_id, :method_name] => :environment do |task, args|
  repo = args[:repo]
  path = args[:path]
  entry_id = args[:entry_id]
  framework_id = args[:framework_id]
  method_name = args[:method_name]
  Rake::Task["fetch_and_create_snippets[#{repo},#{path},#{entry_id},#{framework_id},#{method_name}]"].invoke
  Rake::Task["generate_diffs_for_snippets"].invoke
  Rake::Task["cleanup_snippets_with_empty_diffs"].invoke
end

我这样称呼它:

$ rake setup_a_new_set_of_snippets["some/repo","some/viable/path",1,1,has_many]
** Invoke setup_a_new_set_of_snippets (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute setup_a_new_set_of_snippets
rake aborted!
Don't know how to build task 'fetch_and_create_snippets[some/repo,some/viable/path,1,1,has_many]' (see --tasks)
/lib/tasks/setup_new_snippet_set.rake:12:in `block in <top (required)>'
/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/bin/ruby_executable_hooks:15:in `eval'
/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => setup_a_new_set_of_snippets
(See full trace by running task with --trace)

这是它被挂起的任务:

desc 'Fetch and Create Snippets from Github'

task :fetch_and_create_snippets, [:repo, :path, :entry_id, :framework_id, :method_name] => :environment do |task, args|
    # truncated for brevity
end

FWIW,当我单独运行fetch_and_create_snippets任务时......它就像一个魅力。

导致这种情况的原因是什么?

2 个答案:

答案 0 :(得分:1)

您没有正确地将参数传递给rake任务。见How to pass command line arguments to a rake task

具体来说,就在这里:

  Rake::Task["fetch_and_create_snippets[#{repo},#{path},#{entry_id},#{framework_id},#{method_name}]"].invoke

您似乎正在使用Rake.application.invoke_task的语法,而Rake::Task[name].invoke的正确语法是:

Rake::Task["fetch_and_create_snippets"].invoke(repo, path, entry_id, framework_id, method_name)

答案 1 :(得分:1)

将参数传递给rake任务的语法是:

Rake::Task['fetch_and_create_snippets'].invoke(
  repo, path, entry_id, framework_id, method_name
)