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

发布于 2024-09-25 14:32:53 字数 361 浏览 1 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遥远的绿洲 2024-10-02 14:32:53

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

在此处查看更多信息:rake with params

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

洒一地阳光 2024-10-02 14:32:53

我知道我参加聚会迟到了,但我遇到了同样的问题,并想出了一些不使用环境变量的方法。您可以使用 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

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
天赋异禀 2024-10-02 14:32:53

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

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

它将被调用为:

$ rake name first_name=John last_name=Smith

ENV 数组数据也可用于 pre_name 任务。

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:

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.

北凤男飞 2024-10-02 14:32:53
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文