2010-04-04 3 views
13

나는 인수가있는 작업으로 구성된 레이크 (Rake) 스크립트를 작성 중이다. 저는 인수를 전달하는 방법과 다른 태스크에 의존하는 태스크를 만드는 방법을 알아 냈습니다.상위 작업의 인수를 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 
  • 나는 자식 작업에 상위 작업에서 인수를 전달할 수 있습니까?
  • 하위 작업을 비공개로 설정하여 개별적으로 호출 할 수 없습니까?

답변

26

실제로 레이크 작업간에 인수를 전달하는 세 가지 방법을 생각할 수 있습니다.

  1. 사용 레이크에 내장 된 인수에 대한 지원 :

    # 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] 
    
  2. 직접 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] 
    
  3. 또 다른 해결책은 원숭이 패치 레이크의 주요 응용 프로그램 개체 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 
    
0

설정 속성도 마법처럼 작동하는 것 같다.

작업 종속성이 필요한 특성을 설정하는 작업으로 설정되어 있는지 확인하십시오.

# set the attribute I want to use in another task 
task :buy_puppy, [:name] do |_, args| 
    name = args[:name] || 'Rover' 
    @dog = Dog.new(name) 
end 

# task I actually want to run 
task :walk_dog => :buy_puppy do 
    @dog.walk 
end 
관련 문제