Load rake files and run tasks from other files

kaiser picture kaiser · Dec 7, 2012 · Viewed 20.4k times · Source

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.

Answer

splattael picture splattael · Feb 6, 2015

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'