How do I execute Rake tasks with arguments multiple times?

前端 未结 4 1234
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-19 04:39

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

4条回答
  •  攒了一身酷
    2021-02-19 05:12

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

    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
    

提交回复
热议问题