Search code examples
ruby-on-railsrakerake-task

Rake: How do I forward params to child Rake calls?


So, in my rake command, when I do --trace, it only does it on the command I manually execute, and none of the rake commands that the custom rake executes.

My Rake command:

namespace :db do
  task :regenesis do
    #because of how devestating this command could be, it's going 
    # to be forced to use the Test Environment
    puts "Re-Generating the Database"
    Rake::Task["db:drop RAILS_ENV=test --trace"].invoke
    Rake::Task["db:create RAILS_ENV=test"].invoke
    Rake::Task["db:create RAILS_ENV=test"].invoke
    Rake::Task["db:bootstrap RAILS_ENV=test"].invoke
  end
end

Twould be ideal to not have to hard code the --trace in there =D

So, I should be able to do rake db:regenesis --trace, and it should append trace onto all of those rake commands.

How do I do that?


Solution

  • Please check this question. If you cannot modify your tasks to add the parameters ( for some reason ), then you can use environment variables, like:

    namespace :db do
      task :regenesis do
        #because of how devestating this command could be, it's going 
        # to be forced to use the Test Environment
        puts "Re-Generating the Database"
        ENV["extra_option"] = "--trace"
        Rake::Task["db:drop RAILS_ENV=test"].invoke
        Rake::Task["db:create RAILS_ENV=test"].invoke
        Rake::Task["db:create RAILS_ENV=test"].invoke
        Rake::Task["db:bootstrap RAILS_ENV=test"].invoke
      end
    end
    

    And in your tasks you'd have to look for ENV["extra_option"]