Default task for namespace in Rake

前端 未结 8 1515
醉酒成梦
醉酒成梦 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:44

    Add the following task outside of the namespace:

    desc "Run all my tasks"
    task :my_tasks => ["my_tasks:all"]
    

    Keep in mind, that you can have a task with the same name as the namespace.

    And hier a bigger example, that shows, how you can make use of tasks, which have the same name as the namespace, even when nesting namespaces:

    namespace :job1 do
      task :do_something1 do
            puts "job1:do_something1"
        end
    
      task :do_something2 do
            puts "job1:do_something2"
        end
      task :all => [:do_something1, :do_something2]
    end
    
    desc "Job 1"
    task :job1 => ["job1:all"]
    
    # You do not need the "all"-task, but it might be handier to have one.
    namespace :job2 do
      task :do_something1 do
            puts "job2:do_something1"
        end
    
      task :do_something2 do
            puts "job2:do_something2"
        end
    end
    
    desc "Job 2"
    task :job2 => ["job2:do_something1", "job2:do_something2"]
    
    namespace :superjob do
        namespace :job1 do
            task :do_something1 do
                puts "superjob:job1:do_something1"
            end
    
            task :do_something2 do
                puts "superjob:job1:do_something2"
            end
        end
    
        desc "Job 1 in Superjob"
        task :job1 => ["job1:do_something1", "job1:do_something2"]
    
        namespace :job2 do
            task :do_something1 do
                puts "superjob:job2:do_something1"
            end
    
            task :do_something2 do
                puts "superjob:job2:do_something2"
            end
        end
    
        desc "Job 2 in Superjob"
        task :job2 => ["job2:do_something1", "job2:do_something2"]
    end
    
    desc "My Super Job"
    task :superjob => ["superjob:job1", "superjob:job2"]
    
    # Do them all just by calling "$ rake"
    task :default => [:job1, :job2, :superjob]
    

    Just copy it and try it out.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-12 23:51

    Combining Szymon Lipiński's and Shyam Habarakada's answers, here is what I think is the most idiomatic and consise answer:

    namespace :my_tasks do
      task :foo do
        do_something
      end
    
      task :bar do
        do_something_else
      end
    
    end
    
    task :my_tasks => ["my_tasks:foo", "my_tasks:bar"]
    

    allows you to do rake my_tasks while avoiding cumbersome invocation of the subtasks.

    0 讨论(0)
  • 2020-12-12 23:53

    Based on Rocky's solution Default task for namespace in Rake

    And this dexter's answer Is there a way to know the current rake task?

    namespace :root do
      namespace :foo do
      end
    
      namespace :target do
        task :all do |task_all|
          Rake.application.in_namespace(task_all.scope.path) do |ns|
            ns.tasks.each { |task| task.invoke unless task.name == task_all.name } 
          end
        end
    
        task :one do
        end
    
        task :another do
        end
      end
    end
    
    0 讨论(0)
  • 2020-12-12 23:55

    I use this Rakefile for cucumber:

    require 'cucumber'
    require 'cucumber/rake/task'
    
    namespace :features do
      Cucumber::Rake::Task.new(:fast) do |t|
        t.profile = 'fast'
      end
    
      Cucumber::Rake::Task.new(:slow) do |t|
        t.profile = 'slow'
      end
    
      task :ci => [:fast, :slow]
    end
    
    task :default => "features:ci"
    

    Then if I type just:

    rake
    

    It runs the default task, which runs both fast and slow tests.

    I learned this from Cheezy's blog.

    0 讨论(0)
  • 2020-12-12 23:57

    I suggest you to use this if you have lots of tasks in the namespace.

    task :my_tasks do
      Rake.application.in_namespace(:my_tasks){|x| x.tasks.each{|t| t.invoke}}
    end
    

    And then you can run all tasks in the namespace by:

    rake my_tasks
    

    With this, you don't need to worry to change your :all task when you add new tasks into that namespace.

    0 讨论(0)
提交回复
热议问题