使用接受参数作为先决条件的 rake 任务
根据 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它实际上非常简单 - :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
我知道我参加聚会迟到了,但我遇到了同样的问题,并想出了一些不使用环境变量的方法。您可以使用 Rake::Task.invoke 来执行此操作。以下是数据库备份 rake 任务的示例:
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 任务都不使用参数。 (我想我尝试使用参数,但无法让它们工作。)相反,我依赖 ENV 数组。因此,例如,我将该示例任务编写为:
它将被调用为:
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:
which would be invoked as:
The ENV array data would be available to the pre_name task as well.