【问题标题】:How do I execute Rake tasks with arguments multiple times?如何多次执行带参数的 Rake 任务?
【发布时间】:2012-09-11 22:52:59
【问题描述】:

不可能从循环more than onceinvoke 相同的 rake 任务。但是,我希望能够调用rake first 并循环遍历一个数组,并在每次迭代中使用不同的参数调用second。由于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

解决这个问题的一个技巧是将execute 的参数放入一个数组中,然后在second 中检查args 的结构,但这似乎是,好吧,hackish。还有其他(更好的)方法来完成我想做的事情吗?

【问题讨论】:

    标签: ruby rake


    【解决方案1】:

    FWIW 这可能对某人有帮助,所以我会发布它。

    我希望能够从 CLI 运行一个命令来多次运行一个 Rake 任务(每次都使用新参数,但这并不重要)。

    例子:

    rake my_task[x] my_task[y] my_task[z]
    

    但是,由于 Rake 将所有 my_task 视为同一个任务而不管 args,它只会在第一次调用 my_task[x] 而不会调用 my_task[y]my_task[z]

    使用其他答案中提到的Rake::Task#reenable 方法,我编写了一个reenable Rake 任务,您可以将其定位在任务之后运行以允许它再次运行。

    结果:

    rake my_task[x] reenable[my_task] my_task[y] reenable[my_task] my_task[z]
    

    我不会说这很理想,但它适用于我的情况。

    reenablerake任务来源:

    task :reenable, [:taskname] do |_task, args|
      Rake::Task[args[:taskname]].reenable
      Rake::Task[:reenable].reenable
    end
    

    【讨论】:

      【解决方案2】:

      您可以使用 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
      

      $ 耙第一

      1
      bar
      2
      bar
      3
      bar
      4
      bar
      

      【讨论】:

        【解决方案3】:

        execute 函数要求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))
        

        但是,invoke 和 execute 之间还有另一个区别,execute 不会在 invoke 首先执行此操作时运行 :prerequisite_task,因此 invoke 和 reenable 或 execute 的含义并不完全相同。

        【讨论】:

          【解决方案4】:

          这对我有用,很容易理解你只需要循环你的 bash 命令。

          task :taskname, [:loop] do |t, args|
                  $i = 0
                  $num = args.loop.to_i
                  while $i < $num  do
                  sh 'your bash command''
                  $i +=1
                  end
              end
          

          【讨论】:

            猜你喜欢
            • 2017-08-02
            • 1970-01-01
            • 1970-01-01
            • 2018-06-08
            • 2013-09-10
            • 2013-06-15
            • 2014-10-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多