如何多次使用参数执行Rake任务?

时间:2022-11-10 20:56:16

It's not possible to invoke the same rake task from within a loop more than once. But, I want to be able to call rake first and loop through an array and invoke second on each iteration with different arguments. Since invoke only gets executed the first time around, I tried to use execute, but Rake::Task#execute doesn't use the splat (*) operator and only takes a single argument.

从循环中多次调用同一个rake任务是不可能的。但是,我希望能够首先调用rake,然后在一个数组中循环,然后在每个迭代中使用不同的参数调用second。因为调用只在第一次执行时执行,所以我尝试使用execute,但是Rake:::Task#execute不使用splat(*)操作符,只接受一个参数。

desc "first task"
task :first do 
  other_arg = "bar"
  [1,2,3,4].each_with_index do |n,i|
    if i == 0 
      Rake::Task["foo:second"].invoke(n,other_arg)
    else
      # this doesn't work
      Rake::Task["foo:second"].execute(n,other_arg)
    end
  end
end

task :second, [:first_arg, :second_arg] => :prerequisite_task do |t,args|
  puts args[:first_arg]
  puts args[:second_arg]
  # ...
end

One hack around it is to put the arguments to execute into an array and in second examine the structure of args, but that seems, well, hackish. Is there another (better?) way to accomplish what I'd like to do?

绕过它的一种方法是将参数执行到一个数组中,然后在第二次检查args的结构,但这似乎是一种黑客行为。有没有更好的方法来完成我想做的事情?

2 个解决方案

#1


19  

You can use Rake::Task#reenable to allow it to be invoked again.

您可以使用Rake::Task#reenable允许再次调用它。

desc "first task"
task :first do 
  other_arg = "bar"
  [1,2,3,4].each_with_index do |n,i|
    if i == 0 
      Rake::Task["second"].invoke(n,other_arg)
    else
      # this does work
      Rake::Task["second"].reenable
      Rake::Task["second"].invoke(n,other_arg)
    end
  end
end

task :second, [:first_arg, :second_arg]  do |t,args|
  puts args[:first_arg]
  puts args[:second_arg]
  # ...
end

$ rake first

美元耙第一

1
bar
2
bar
3
bar
4
bar

#2


4  

The execute function asks for a Rake::TaskArguments as a parameter, this is why it only accepts one argument.

执行函数要求Rake::TaskArguments作为参数,这就是为什么它只接受一个参数。

You could use

您可以使用

stuff_args = {:match => "HELLO", :freq => '100' }
Rake::Task["stuff:sample"].execute(Rake::TaskArguments.new(stuff_args.keys, stuff_args.values))

However there is another difference between invoke and execute, execute doesn't run the :prerequisite_task when invoke does this first, so invoke and reenable or execute doesn't have exactly the same meaning.

然而,调用和执行之间还有另一个区别,在调用首先执行时,execute不会运行:先决条件_task,因此调用和重新启用或执行的含义并不完全相同。

#1


19  

You can use Rake::Task#reenable to allow it to be invoked again.

您可以使用Rake::Task#reenable允许再次调用它。

desc "first task"
task :first do 
  other_arg = "bar"
  [1,2,3,4].each_with_index do |n,i|
    if i == 0 
      Rake::Task["second"].invoke(n,other_arg)
    else
      # this does work
      Rake::Task["second"].reenable
      Rake::Task["second"].invoke(n,other_arg)
    end
  end
end

task :second, [:first_arg, :second_arg]  do |t,args|
  puts args[:first_arg]
  puts args[:second_arg]
  # ...
end

$ rake first

美元耙第一

1
bar
2
bar
3
bar
4
bar

#2


4  

The execute function asks for a Rake::TaskArguments as a parameter, this is why it only accepts one argument.

执行函数要求Rake::TaskArguments作为参数,这就是为什么它只接受一个参数。

You could use

您可以使用

stuff_args = {:match => "HELLO", :freq => '100' }
Rake::Task["stuff:sample"].execute(Rake::TaskArguments.new(stuff_args.keys, stuff_args.values))

However there is another difference between invoke and execute, execute doesn't run the :prerequisite_task when invoke does this first, so invoke and reenable or execute doesn't have exactly the same meaning.

然而,调用和执行之间还有另一个区别,在调用首先执行时,execute不会运行:先决条件_task,因此调用和重新启用或执行的含义并不完全相同。