2011-05-12 4 views
2

간단한 함수를 실행하고 네임 스페이스의 모든 레이크 작업에 enter_to_continue를 실행하고 싶습니다. 현재 모든 단일 태스크의 끝에이 함수를 추가합니다.rake 명령을 사용하여 네임 스페이스의 모든 작업에 함수를 실행하는 방법

그러나이 함수는 enter_to_continue를 실행하여 네임 스페이스 'mytest'의 모든 작업이 끝날 때마다 자동으로 실행되도록하는 방법이 있습니다. 어떤 메타 프로그래밍을하고 있니?

namespace :mytest do 
    task :foo do 
    system "date" 
    enter_to_continue 
    end 

    task :bar do 
    system "ls" 
    enter_to_continue 
    end 

    # ... 
    # Let's say 10 more tasks ends with enter_to_continue comes after 
    # ... 
end 

def enter_to_continue 
    STDIN.gets.chomp 
end 

답변

1

당신은 레이크을 시도 할 수 있습니다 : 작업 #이 향상 :

require 'rake' 

ns = namespace :mytest do 

    task :foo do |t| 
    puts "You called task #{t}" 
    end 

    task :bar do |t| 
    puts "You called task #{t}" 
    end 

    # ... 
    # Let's say 10 more tasks ends with enter_to_continue comes after 
    # ... 
end 

# 
#Add actions to each task (e.g. enter_to_continue) 
# 
ns.tasks.each{|tsk| 
    tsk.enhance { puts "\treached method #{__method__}" } 
} 

#Test the result 
task :default => "mytest:foo" 
task :default => "mytest:bar" 
if $0 == __FILE__ 
    Rake.application[:default].invoke 
end 

편집 : 당신은 정의 블록 내부 또한 작업을 향상시킬 수 :

namespace :mytest do |ns| 
    # 
    #...Task definitions... 
    # 
    ns.tasks.each{|tsk| 
    tsk.enhance { puts "\treached method #{__method__}" } 
    } 
end 
관련 문제