Pregunta

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.

¿Fue útil?

Solución

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'

Otros consejos

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.

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top