命名空间外的 Rake 任务
Rake task outside of namespace
我有一个这样设置的 rake 文件:
require 'rake'
namespace :setup do
puts "I'm in setup"
task :create do
puts "I'm in create"
end
end
task :run do
puts "I'm in run"
end
如果我运行 rake setup:create
我会得到预期的结果:
I'm in setup
I'm in create
但是,如果我运行 rake run
,我得到:
I'm in setup
I'm in run
据我所知in the guides,这是出乎意料的,如下所述:
When looking up a task name, rake will start with the current
namespace and attempt to find the name there. If it fails to find a
name in the current namespace, it will search the parent namespaces
until a match is found (or an error occurs if there is no match).
这不会假设 rake 从当前命名空间开始,然后继续寻找某些东西。在我的示例中,我没有提供当前的命名空间,但它跳转到 setup
,即使我给它的只是 run
。
我错过了什么?
行 puts "I'm in setup"
不是任何任务的一部分 - 它会在您指定的任何任务中执行,即使是不存在的任务,因为正在解析文件(严格来说不是 Ruby 正在解析文件,但在执行和设置 rake 任务时):
$ rake foo
I'm in setup
rake aborted!
Don't know how to build task 'foo'
(See full trace by running task with --trace)
只有在读取文件后才会进行任务查找,这就是引文所指的内容。
如果您想要一个命名空间的所有任务的一些通用代码,您将需要为其创建一个任务并使命名空间中的所有其他任务都依赖于它,例如:
namespace :setup do
task :create => :default do
puts "I'm in create"
end
task :default do
puts "I'm in setup"
end
end
我有一个这样设置的 rake 文件:
require 'rake'
namespace :setup do
puts "I'm in setup"
task :create do
puts "I'm in create"
end
end
task :run do
puts "I'm in run"
end
如果我运行 rake setup:create
我会得到预期的结果:
I'm in setup
I'm in create
但是,如果我运行 rake run
,我得到:
I'm in setup
I'm in run
据我所知in the guides,这是出乎意料的,如下所述:
When looking up a task name, rake will start with the current namespace and attempt to find the name there. If it fails to find a name in the current namespace, it will search the parent namespaces until a match is found (or an error occurs if there is no match).
这不会假设 rake 从当前命名空间开始,然后继续寻找某些东西。在我的示例中,我没有提供当前的命名空间,但它跳转到 setup
,即使我给它的只是 run
。
我错过了什么?
行 puts "I'm in setup"
不是任何任务的一部分 - 它会在您指定的任何任务中执行,即使是不存在的任务,因为正在解析文件(严格来说不是 Ruby 正在解析文件,但在执行和设置 rake 任务时):
$ rake foo
I'm in setup
rake aborted!
Don't know how to build task 'foo'
(See full trace by running task with --trace)
只有在读取文件后才会进行任务查找,这就是引文所指的内容。
如果您想要一个命名空间的所有任务的一些通用代码,您将需要为其创建一个任务并使命名空间中的所有其他任务都依赖于它,例如:
namespace :setup do
task :create => :default do
puts "I'm in create"
end
task :default do
puts "I'm in setup"
end
end