2010-08-05 16 views
18

क्या नामस्थान में कार्यों की सूची प्राप्त करने के लिए एक रेक कार्य के भीतर संभव है? एक तरह का प्रोग्रामेटिक 'रेक-टी डीबी'?क्या नामस्थान में सभी उपलब्ध रेक कार्यों की सूची प्राप्त करना संभव है?

 
tasks = Rake.application.tasks 

यह रैक की एक सरणी वापस आ जाएगी :: टास्क वस्तुओं है कि जांच की जा सकती:

उत्तर

16

मैं जवाब में पता चला है। http://rake.rubyforge.org/

+0

जहां तक ​​मैंने देखा है, एक गुंजाइश के भीतर चीजों को खोजने के लिए कोई सहायक नहीं है, लेकिन मुझे लगता है कि यह इतना आसान होना चाहिए: Rake.application.tasks.reject {| task | task.scope! = [: your,: scope]} – phoet

+1

मेरे मामले में (रेल), 'Rake.application.tasks' – joshfindit

1

पर अधिक जानकारी आप आप ने लिखा इस

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

को पॉप्युलेट करने के लिए' AppName :: Application.load_tasks' को चलाने के लिए भी आवश्यक था, यह वास्तव में केवल एकमात्र है एक गुंजाइश के भीतर कार्यों को खोजने के लिए संभावित समाधान ?! – phoet

+0

यह पहली बात है जो मेरे दिमाग में आई थी। – garno

+0

उत्कृष्ट उदाहरण, धन्यवाद –

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 
संबंधित मुद्दे