如何声明依赖于参数化任务的 Rake 任务?

发布于 2024-11-02 07:33:19 字数 418 浏览 1 评论 0原文

我见过一些任务具有参数和依赖项任务的示例,例如:

task :name, [:first_name, :last_name] => [:pre_name] do |t, args|
  args.with_defaults(:first_name => "John", :last_name => "Dough")
  puts "First name is #{args.first_name}"
  puts "Last name is #{args.last_name}"
end

如果它是任务依赖项,您将如何将参数传递给 name 任务,例如:

task :sendLetter => :name
  #do something
end

I have seen examples where a task has parameters and a dependency task like:

task :name, [:first_name, :last_name] => [:pre_name] do |t, args|
  args.with_defaults(:first_name => "John", :last_name => "Dough")
  puts "First name is #{args.first_name}"
  puts "Last name is #{args.last_name}"
end

How would you pass parameters to the name task if it was a task dependency like:

task :sendLetter => :name
  #do something
end

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

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

发布评论

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

评论(2

季末如歌 2024-11-09 07:33:19

参数通过调用堆栈向下传递。您只需要确保您的顶级任务捕获所有依赖项所需的所有参数。在您的情况下,您需要将 first_namelast_name 放在 send_letter 任务中。

下面是一个示例,显示在其他地方定义的命名参数流入依赖项(即使它们没有在依赖项中定义),但与顶级任务参数名称不匹配的参数为零。

desc 'Bar'
task :bar, :nom do |task, args|
  puts "BAR NOM: #{args[:nom]}"
  puts "BAR NAME: #{args[:name]}"
end

desc 'Foo'
task :foo, [:name] => :bar do |task, args|
  puts "FOO NAME: #{args[:name]}"
end

运行 rake foo[baz] 会产生

BAR NOM: 
BAR NAME: baz
FOO NAME: baz

有趣的是,在 foo 任务中使用 args.with_defaults(nom: 'Jaques') 会产生对依赖任务没有影响 - nom 仍然为零。

Rake 版本:rake,版本 10.0.3

Ruby 版本:ruby 1.9.3p125(2012-02-16 修订版 34643)[x86_64-darwin11.3.0]

Args are passed down through the call stack. You just need to make sure your top-level task captures all the arguments all of the dependencies require. In your case you'll want to put first_name and last_name on the send_letter task.

Here is an example that shows named arguments defined elsewhere flowing into the dependency (even if they aren't defined in the dependency), but the argument that doesn't match the name of the top-level task argument is nil.

desc 'Bar'
task :bar, :nom do |task, args|
  puts "BAR NOM: #{args[:nom]}"
  puts "BAR NAME: #{args[:name]}"
end

desc 'Foo'
task :foo, [:name] => :bar do |task, args|
  puts "FOO NAME: #{args[:name]}"
end

Running rake foo[baz] yields

BAR NOM: 
BAR NAME: baz
FOO NAME: baz

It is interesting to note that using args.with_defaults(nom: 'Jaques') in the foo task has no effect on the dependent task -- nom is still nil.

Rake version: rake, version 10.0.3

Ruby version: ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]

虐人心 2024-11-09 07:33:19

您可能会得到的最接近的结果是

task :sendLetter do
  task(:name).invoke("first", "last")
end

或者

task :sendLetter do
  task(:name).execute(["first", "last"])
end

您可以执行类似的操作

task :sendLetter => task(:name).invoke("first", "last")

,但无论您是否请求 sendLetter ,都会调用 name

Task#invoke 仅在尚未调用的情况下调用该任务,并首先调用任何未调用的先决条件。 Task#execute 始终调用任务,但不调用任何先决条件。请注意,这些参数不会影响 Task#invoke 的一次调用性质:如果您调用参数化任务两次,则无论参数是否不同,它都只会运行一次。

The closest you're probably going to get is either

task :sendLetter do
  task(:name).invoke("first", "last")
end

or

task :sendLetter do
  task(:name).execute(["first", "last"])
end

You can do something like

task :sendLetter => task(:name).invoke("first", "last")

but that will invoke name regardless of whether you ask for sendLetter or not.

Task#invoke only calls the task if it hasn't been called and calls any uncalled prereqs first. Task#execute always calls the task but does not call any prereqs. Note that the parameters don't affect the call-once nature of Task#invoke: if you invoke a parameterized task twice, it will only get run once, whether or not the parameters are different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文