2010-08-05 19 views

回答

16

我已經找到了答案:

 
tasks = Rake.application.tasks 

這將返回耙數組::可以檢查任務的對象。在http://rake.rubyforge.org/

+0

據我所看到的,沒有幫手的範圍內,尋找的東西,但我認爲,這應該是很容易:Rake.application.tasks.reject {|任務| task.scope =![:你,:範圍]} – phoet 2011-02-28 09:46:58

+1

在我的情況(滑軌),但也有必要執行'AppName的:: Application.load_tasks'填充'Rake.application.tasks' – joshfindit 2016-08-19 13:57:51

1

更多詳細信息您可以使用grep命令這樣

desc 'Test' 
task :test do 
    # You can change db: by any other namespaces 
    result = %x[rake -T | sed -n '/db:/{/grep/!p;}' | awk '{print$2}'] 
    result.each_line do |t| 
     puts t # Where t is your task name 
    end 
end 
+0

這真的是唯一的在範圍內尋找任務的可能解決方案?! – phoet 2011-02-28 09:39:37

+0

這是我想起來的第一件事。 – garno 2011-03-02 14:48:35

+0

很好的例子,謝謝 – 2014-06-02 03:57:48

11

正如你寫,用Rake.application.tasks你得到的所有任務。

但命名空間中,您可以選擇命名空間(任務mytest的:任務列表)的唯一任務

而且你可能會限制該任務命名空間(任務tasklist_mytest)。

require 'rake' 

namespace :mytest do |ns| 

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

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

    desc 'Get tasks inside actual namespace' 
    task :tasklist do 
    puts 'All tasks of "mytest":' 
    puts ns.tasks #ns is defined as block-argument 
    end 

end 

desc 'Get all tasks' 
task :tasklist do 
    puts 'All tasks:' 
    puts Rake.application.tasks 
end 

desc 'Get tasks outside the namespace' 
task :tasklist_mytest do 
    puts 'All tasks of "mytest":' 
    Rake.application.in_namespace(:mytest){|x| 
    puts x.tasks 
    } 
end 

if $0 == __FILE__ 
    Rake.application['tasklist'].invoke() #all tasks 
    Rake.application['mytest:tasklist'].invoke() #tasks of mytest 
    Rake.application['tasklist_mytest'].invoke() #tasks of mytest 
end 
相關問題