Rake默认任务和命名空间 [英] Rake Default Task and Namespaces

查看:85
本文介绍了Rake默认任务和命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了文档并查看了很多示例,但是我不清楚默认值和名称空间. (使用rake,版本10.0.3)

I have read through the docs and looked at quite a few examples but I am not clear on defaults and namespaces. (using rake, version 10.0.3)

首先,尽管我不记得明确看到过这一点,但无论定义了多少个默认任务,都只能有一个默认任务.显然,加载顺序(PROJECT_NAME :: Application.load_tasks)确定了获胜者.当我努力创建命名空间的默认值时,我发现有时会覆盖Rails应用程序的常规默认值,其中:

First it seems, though I do not recall seeing this explicitly, that there can be only ONE default task regardless of how many are defined. Apparently the load order (PROJECT_NAME::Application.load_tasks) determines the winner. When I have struggled to create a namespaced default I have found that I have sometimes overridden the normal default for a rails app where:

rake

默认运行测试.

首先是"rake -T"命令:

First here is the "rake -T" command:

$ rake -T a_name
rake a_name:a_task_1         # a_task_1
rake a_name:a_task_2         # a_task_2
rake a_name:b_name:b_task_1  # b_task_1
rake a_name:b_name:b_task_2  # b_task_2
rake a_name:default          # This is hopefully a namespaced default

当我运行命名空间时,我只希望得到默认"名称:

When I run the namespace only which I am hoping is the "default" I get:

$ rake a_name
rake aborted!
Don't know how to build task 'a_name'

(See full trace by running task with --trace)

我希望它能在b_name命名空间中运行b_task_1,因为我已将其声明为默认值

I was expecting this to run the b_task_1 in the b_name namespace because I have declared it as the default

但是,如果我明确地使用默认"一词,我会得到:

However, If I explicitly tack on the word "default" I get this:

$  rake a_name:default
a_task_1

无论如何我还是很困惑.谁能帮我澄清一下...

Anyway I am thoroughly confused. Can anyone help clarify this for me...

namespace :a_name do

  desc "a_task_1"
  task :a_task_1 do
    puts "a_task_1"
  end

  desc "a_task_2"
  task :a_task_2 do
    puts "a_task_2"
  end

  namespace :b_name do

    desc "b_task_1"
    task :b_task_1 do
      puts "b_task_1"
    end

    desc "b_task_2"
      task :b_task_2 do
    puts "b_task_2"
    end

  end

  desc "This is hopefully a namespaced default"
  task :default => 'b_name:b_task_1'
end

推荐答案

您可以定义一个与名称空间同名的任务.我认为它没有在名称空间内部定义默认任务那样漂亮.

You can define a task with the same name as your namespace. It is not as pretty as having the default task defined inside the namespace itself I think.

desc "runs bar & baz in foo"
task foo: ["foo:bar", "foo:baz"]

namespace :foo do
  desc "bar in foo"
  task :bar do
    puts "bar"
  end

  desc "baz in foo"
  task :baz do
    puts "baz"
  end
end

这就是他们被列出的方式:

And that's how they get listed:

rake foo                               # runs bar & baz in foo
rake foo:bar                           # bar in foo
rake foo:baz                           # baz in foo

这篇关于Rake默认任务和命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆