Default task for namespace in Rake

前端 未结 8 1523
醉酒成梦
醉酒成梦 2020-12-12 23:05

Given something like:

namespace :my_tasks do
  task :foo do
    do_something
  end

  task :bar do
    do_something_else
  end

  task :all => [:foo, :bar         


        
8条回答
  •  死守一世寂寞
    2020-12-12 23:50

    The way I'm reading obvio171's question is that he is asking1) for a systematic way to invoke a certain task in a namespace by invoking the namespace as a task.

    I've frequently encountered the same need. I like to logically group tasks into namespaces. Often that grouping resembles a hierarchy. Hence the desire to invoke the group makes very much sense to me.

    Here's my take:

    module Rake::DSL
      def group(name, &block)
        ns = namespace name, &block
        default = ns[:default]
        task name => "#{name}:default" if default
        ns
      end
    end
    
    group :foo do
      task :foo1 do |t| puts t.name end
      task :foo2 do |t| puts t.name end
      task :default => [:foo1, :foo2]
    end
    
    task :default => :foo
    

    1)...or was asking, years ago. Nonetheless a still interesting question.

提交回复
热议问题