不知道如何构建任务——对于调用其他任务的父任务
Don't know how to build task -- for parent task that calls other tasks
我有一个名为 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
任务时......它就像一个魅力。
可能是什么原因造成的?
您没有正确地向 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)
将参数传递给 rake 任务的语法是:
Rake::Task['fetch_and_create_snippets'].invoke(
repo, path, entry_id, framework_id, method_name
)
我有一个名为 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
任务时......它就像一个魅力。
可能是什么原因造成的?
您没有正确地向 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)
将参数传递给 rake 任务的语法是:
Rake::Task['fetch_and_create_snippets'].invoke(
repo, path, entry_id, framework_id, method_name
)