Performing tasks in other files

I'm trying to use rake in a project, and if I put everything in a Rakefile, it will be huge and hard to read / find things, so I tried to insert each name file into my own file in lib / rake, I added this at the top of my rake file:

Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map { |f| require f }

it downloads the file without problems, but has no tasks. I have only one .rake file as a test, which is now called "servers.rake", and it looks like this:

namespace :server do
    task :test do
        puts "test"
    end
end

so when I run rake server:testid, I expect to see one line with the message "test", instead I get

rake aborted!
Don't know how to build task 'server:test'

At first I thought my codes were wrong, but if I copy the contents of lib / rake / servers.rake to Rakefile, it works fine.

How do I get rake tasks to work that are in another file?

+5
1

Dir.glob('lib/rake/*.rake').each { |r| import r }
+7

All Articles