加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 20:30:14 所属栏目:百科 来源:网络整理
导读:不能在循环 more than once中调用相同的耙子任务.但是,我想要能够先调用rake并循环遍历一个数组,并在每个迭代中调用不同的参数.由于invoke只是第一次被执行,我试图使用execute,但是 Rake::Task#execute不使用splat(*)运算符,只需要一个参数. desc "first tas
不能在循环 more than once中调用相同的耙子任务.但是,我想要能够先调用rake并循环遍历一个数组,并在每个迭代中调用不同的参数.由于invoke只是第一次被执行,我试图使用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

围绕它的一个缺点是将参数执行到一个数组中,并且第二次检查args的结构,但是这似乎是黑客.还有另一个(更好的)方式来完成我想做的事情吗?

解决方法

可以使用Rake :: Task#reenable来重新调用它.
desc "first task"
task :first do 
  other_arg = "bar"
  [1,i|
    if i == 0 
      Rake::Task["second"].invoke(n,other_arg)
    else
      # this does work
      Rake::Task["second"].reenable
      Rake::Task["second"].invoke(n,:second_arg]  do |t,args|
  puts args[:first_arg]
  puts args[:second_arg]
  # ...
end

$rake第一

1
bar
2
bar
3
bar
4
bar

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读