Rake task outside the namespace

I have a rake file configured like this:

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 

If I run rake setup:create , I get the expected:

 I'm in setup I'm in create 

However, if I run rake run , I get:

 I'm in setup I'm in run 

From what I can say in the manuals , this is unexpected, as indicated here:

When searching for a task name, the rake will begin with the current namespace and try to find the name there. If he cannot find the name in the current namespace, he will search for the parent namespaces until a match is found (or an error occurs if there is no match).

Didnโ€™t this suggest that rake starts in the current namespace and then goes on to search for something. In my example, I do not provide the current name file, but it jumps to setup , although all I gave was run .

What am I missing?

+5
source share
1 answer

The puts "I'm in setup" not part of any task - it will be executed regardless of the task you set, even nonexistent, because the file is processed (strictly speaking, when Ruby does not analyze the file, but how it is executed and configures rake tasks):

 $ rake foo I'm in setup rake aborted! Don't know how to build task 'foo' (See full trace by running task with --trace) 

Only after the file has been read does the task search, and this is what this quote refers to.

If you need common code for all namespace tasks, you will need to create a task for it and do all the other tasks in the namespace, for example:

 namespace :setup do task :create => :default do puts "I'm in create" end task :default do puts "I'm in setup" end end 
+4
source

Source: https://habr.com/ru/post/1210500/


All Articles