Load rake files and run tasks from other files
Asked Answered
P

3

37

Currently I'm trying split up my rake files to organize them better. For this, I've added a rake folder to my assets dir, that holds one rake file for each group of tasks.

As I'm coming from PHP, I've only very basic knowledge of Ruby/Rake and can't get my namespaces default action running after loading the file.

The commmented out Rake :: Task ...-string inside app:init throws an error at the CL at me:

rake aborted! 
uninitialized constant TASK

Here's the namespace/class (if this is the right word).

task :default => [ 'app:init' ]
namespace :app do

    rake_dir   = "#{Dir.pwd}/assets/rake/"
    rake_files = FileList.new( "#{rake_dir}*" )

    desc "Loads rake modules (Default action)"
    task :init do
        puts "\t Importing rake files for processing"

        puts "\t loading..."
        rake_files.each() { |rake|
            puts "\t #{rake}"
            require rake
            # @link rubular.com
            name = rake.split( rake_dir ).last.gsub( /.rb\z/, '' )
            puts "\t #{name}"
            #Rake :: Task[ "#{name}:default" ].invoke
        }
    end
end

Thanks in advance.

Edit: At least, I can be sure the file gets loaded, as the plain puts "file loaded" at the beginning of those files gets echoed. The problem seems to only be that the :default action for the namespace in the loaded rake file isn't loading.

Pumping answered 7/12, 2012 at 11:26 Comment(0)
M
67

You can either put your tasks into rakelib/ folder which rake loads by default or add a specific folder in your Rakefile via:

Rake.add_rakelib 'lib/tasks'
Maria answered 6/2, 2015 at 9:5 Comment(2)
One improvement: You could mention that single files can easily be loaded using load "file.rb". Then this should be the accepted answer.Cheney
This answer should be protected. I never knew about this rakelib/ directoryPomposity
A
40

If your goal is to load rake tasks from an external file, then you can do that as follows. First, let's say you have a rake task in a file called <project>/lib/tasks/hello.rake that looks like this:

desc "Say hello"
task :hello do 
  puts "Hello World!"
end

Then you can create a simple Rakefile in your <project> directory to load it like this:

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

desc "Say goodbye"
task :goodbye do
  puts "See you later!"
end

Of course, this will load all files ending with the rake extension. You can simply load hello.rake like this:

load './lib/tasks/hello.rake'

desc "Say goodbye"
task :goodbye do
  puts "See you later!"
end

To see all the tasks that have been loaded use rake -T. Note that I've used lib/tasks since that's the standard approach taken by Rails applications. You could use assets or whatever you prefer, though I prefer lib/tasks even in non-Rails projects. I also tend to separate my task files based on their namespace.

Atonality answered 27/5, 2013 at 22:35 Comment(2)
why does it have to be load and not require?Liaison
require is part of Ruby proper, but load is part of the Rake DSL. It doens't have to be load, though. Rake provides other, perhaps more explicit, ways to do this. For example: Dir.glob('tasks/**/*.rake').each { |r| Rake.application.add_import r }Atonality
R
5

You can always use Rake.add_rakelib 'tasks', as @splattael said. One thing you need to know, the files in 'tasks' directory need have an extension of '.rake' instead of '.rb', otherwise, rake won't load it for you.

Sample file:

After you do all of the above, use rake -T to check your job to see whether rake has load your tasks successfully.

Radioelement answered 4/1, 2016 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.