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

ruby – 如何将参数从父任务传递给Rake中的子任务?

发布时间:2020-12-16 19:44:46 所属栏目:百科 来源:网络整理
导读:我正在写一个由参数组成的任务的Rake脚本.我想出了如何传递参数,以及如何使任务依赖于其他任务. task :parent,[:parent_argument1,:parent_argument2,:parent_argument3] = [:child1,:child2] do # Perform Parent Task Functionalitiesendtask :child1,[:ch
我正在写一个由参数组成的任务的Rake脚本.我想出了如何传递参数,以及如何使任务依赖于其他任务.
task :parent,[:parent_argument1,:parent_argument2,:parent_argument3] => [:child1,:child2] do
  # Perform Parent Task Functionalities
end

task :child1,[:child1_argument1,:child1_argument2] do |t,args|
  # Perform Child1 Task Functionalities
end

task :child2,[:child2_argument1,:child2_argument2] do |t,args|
  # Perform Child2 Task Functionalities
end

>我可以将参数从父任务传递给子任务吗?
>有没有办法让孩子的任务是私人的,所以不能独立调用?

解决方法

我实际上可以想到在Rake任务之间传递参数的三种方式.

>使用Rake对参数的内置支持:

# accepts argument :one and depends on the :second task.
task :first,[:one] => :second do |t,args|
  puts args.inspect  # => '{ :one => "one" }'
end

# argument :one was automagically passed from task :first.
task :second,:one do |t,args|
  puts args.inspect  # => '{ :one => "one" }'
end

$rake first[one]

>通过Rake::Task#invoke直接调用任务:

# accepts arguments :one,:two and passes them to the :second task.
task :first,:one,:two do |t,args|
  puts args.inspect  # => '{ :one => "1",:two => "2" }'
  task(:second).invoke(args[:one],args[:two])
end

# accepts arguments :third,:fourth which got passed via #invoke.
# notice that arguments are passed by position rather than name.
task :second,:third,:fourth do |t,args|
  puts args.inspect  # => '{ :third => "1",:fourth => "2" }'
end

$rake first[1,2]

>另一个解决方案是将猕猴补丁的主要应用对象Rake::Application
并使用它来存储任意值:

class Rake::Application
  attr_accessor :my_data
end

task :first => :second do
  puts Rake.application.my_data  # => "second"
end

task :second => :third do
  puts Rake.application.my_data  # => "third"
  Rake.application.my_data = "second"
end

task :third do
  Rake.application.my_data = "third"
end

$rake first

(编辑:李大同)

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

    推荐文章
      热点阅读