使用rake任务接受参数作为先决条件

时间:2021-07-08 08:01:59

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

根据http://rake.rubyforge.org/files/doc/rakefile_rdoc.html,您可以创建一个接受参数并具有先决条件的任务:

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?

但是如果:pre_name是一个也接受参数的任务呢?在将参数用作先决条件时,将参数传递给:pre_name的语法是什么?

4 个解决方案

#1


9  

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.

它实际上非常简单 - :pre任务将接收与原始任务相同的参数。您需要做的就是确保签名类似 - 例如,如果第一个任务收到:a,:b:pre任务也需要接收它们。

See more here: rake with params

在这里看到更多:rake with params

#2


4  

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:

我知道我已经迟到了,但我遇到了同样的问题并想出了一些没有使用环境变量的东西。您可以使用Rake :: Task.invoke来执行此操作。以下是数据库备份rake任务的示例:

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

#3


0  

I don't have a direct answer, but I do have an alternative solution that might work for you. None of my rake tasks use parameters. (I think I tried to use parameters and had trouble getting them to work.) Instead, I rely on the ENV array. So, for example, I would write that example task as:

我没有直接的答案,但我确实有一个可能适合您的替代解决方案。我的rake任务都没有使用参数。 (我想我试图使用参数并且无法让它们工作。)相反,我依赖于ENV数组。因此,例如,我会将该示例任务编写为:

task :name =>:pre_name do
  do_something_with_name(ENV['first_name'], ENV['last_name'])
end

which would be invoked as:

将被调用为:

$ rake name first_name=John last_name=Smith

The ENV array data would be available to the pre_name task as well.

ENV阵列数据也可用于pre_name任务。

#4


-1  

namespace :shell do

  desc "Local hostname"
  task :hostname do
    puts "Local hostname"
    sh "hostname"
  end

  desc "Local uptime"
  task :uptime do
    puts "Local uptime"
    sh "uptime"
  end

  desc "Echo something"
  task :echo,[:someword] do |t,args|
    puts "--- #{args[:someword]} ---"
  end

end 


desc "Run all tasks"
task :all , [:someword] => ["shell:hostname","shell:uptime","shell:echo"] do
  puts "Done."
end

#1


9  

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.

它实际上非常简单 - :pre任务将接收与原始任务相同的参数。您需要做的就是确保签名类似 - 例如,如果第一个任务收到:a,:b:pre任务也需要接收它们。

See more here: rake with params

在这里看到更多:rake with params

#2


4  

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:

我知道我已经迟到了,但我遇到了同样的问题并想出了一些没有使用环境变量的东西。您可以使用Rake :: Task.invoke来执行此操作。以下是数据库备份rake任务的示例:

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

#3


0  

I don't have a direct answer, but I do have an alternative solution that might work for you. None of my rake tasks use parameters. (I think I tried to use parameters and had trouble getting them to work.) Instead, I rely on the ENV array. So, for example, I would write that example task as:

我没有直接的答案,但我确实有一个可能适合您的替代解决方案。我的rake任务都没有使用参数。 (我想我试图使用参数并且无法让它们工作。)相反,我依赖于ENV数组。因此,例如,我会将该示例任务编写为:

task :name =>:pre_name do
  do_something_with_name(ENV['first_name'], ENV['last_name'])
end

which would be invoked as:

将被调用为:

$ rake name first_name=John last_name=Smith

The ENV array data would be available to the pre_name task as well.

ENV阵列数据也可用于pre_name任务。

#4


-1  

namespace :shell do

  desc "Local hostname"
  task :hostname do
    puts "Local hostname"
    sh "hostname"
  end

  desc "Local uptime"
  task :uptime do
    puts "Local uptime"
    sh "uptime"
  end

  desc "Echo something"
  task :echo,[:someword] do |t,args|
    puts "--- #{args[:someword]} ---"
  end

end 


desc "Run all tasks"
task :all , [:someword] => ["shell:hostname","shell:uptime","shell:echo"] do
  puts "Done."
end