Search code examples
rubyrakeruby-on-rails-5rake-task

Don't know how to build task -- for parent task that calls other tasks


I have a rake task called setup_a_new_set_of_snippets, that looks like this:

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

I am calling it like this:

$ 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)

This is the task it gets hung up on:

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, when I run the fetch_and_create_snippets task by itself...it works like a charm.

What could be causing this?


Solution

  • You're not passing arguments to the rake task correctly. See How to pass command line arguments to a rake task

    Specifically, right here:

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

    it looks like you're using the syntax for Rake.application.invoke_task whereas the proper syntax for Rake::Task[name].invoke would be:

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