Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a rake task that accepts parameters as a prerequisite

Tags:

ruby

rake

According to http://rake.rubyforge.org/files/doc/rakefile_rdoc.html, you can create a task that accepts parameters and also has prerequisites:

task :name, [:first_name, :last_name] => [:pre_name] do |t, args|

But what if :pre_name is a task that also accepts parameters? What is the syntax for passing parameters to :pre_name when it is used as a prerequisite?

like image 546
Mike Conigliaro Avatar asked Sep 29 '10 18:09

Mike Conigliaro


2 Answers

It's actually pretty simple - the :pre task will receive the same parameters as the original task. All you need to do is make sure that the signature is similar - for instance if the first task receives :a,:b the :pre task needs to receive them as well.

See more here: rake with params

like image 182
Gregory Mostizky Avatar answered Nov 15 '22 15:11

Gregory Mostizky


I know I'm late to the party, but I had the same problem and figured something out that didn't use environment variables. You can use Rake::Task.invoke to do this. Here's an example for a database backup rake task:

namespace :db do
    task :dump_db, [:dump_file, :rails_env] do |t, args|
        puts "dumping to #{args[:dump_file]} with rails env = #{args[:rails_env]}"
    end

    task :stop_slave do
        puts "stopping slave"
    end

    task :start_slave do
        puts "starting slave"
    end

    task :upload_dump, [:dump_file] do |t, args|
        puts "uploading #{args[:dump_file]}"
    end

    task :backup_to_s3, [:dump_file, :rails_env] do |t, args|
        Rake::Task["db:stop_slave"].invoke()
        Rake::Task["db:dump_db"].invoke(args[:dump_file], args[:rails_env])
        Rake::Task["db:start_slave"].invoke()
        Rake::Task["db:upload_dump"].invoke(args[:dump_file])
    end
end
like image 34
Scott Patten Avatar answered Nov 15 '22 15:11

Scott Patten