Tutorial :Rake tasks in other files



Question:

Im trying to use rake in a project, and if I put everything into Rakefile it will be huge and hard to read/find things, so I tried to stick each namesapce in its own file in lib/rake, i added this to the top of my rake file:

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

it loads the file no problem, but doesn't have the tasks. I only have one .rake file as a test for 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:test id expect to see one line appear saying "test", instead I get

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

at first I thought my codes wrong but if I copy the contents of lib/rake/servers.rake into Rakefile it works fine.

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


Solution:1

Needed to change the line in the rake file to

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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »