T5i
T5i

Reputation: 1530

A rake task by .rake file

I have the following Rakefile in a Ruby 1.9.3 project:

require 'rake/testtask'
require 'json'

Rake::TestTask.new do |t|
  t.pattern = "spec/**/*_spec.rb"
  t.verbose = true
end

task :default => :test

namespace :omglol do
  namespace :file_a do
    task :foo do
      # do some stuff
    end
  end

  namespace :file_b do
    task :bar do
      # do some stuff
    end
  end
end

As you can see, the first part of this file allow to run tests, just using rake command. And the second part contains some tasks.

Actually, I have a lot of tasks inside omglol:file_a and omglol:file_b namespaces. That's why I would like to move each of them inside a file, for instance tasks/omglol/file_a.rake and tasks/omglol/file_b.rake.

Is there a best way to do so? Thanks.

Upvotes: 0

Views: 1425

Answers (1)

jchilders
jchilders

Reputation: 376

Yes. Simply move the logic into the appropriate files and then require them.

Example Rakefile:

require 'rake/testtask'
require 'json'
require 'lib/tasks/omglol/file_a.rake' # <= contains your subtasks

Rake::TestTask.new do |t|
  t.pattern = "spec/**/*_spec.rb"
  t.verbose = true
end

task :default => :test

Then in lib/tasks/omglol/file_a.rake simply define your tasks as normal:

namespace :omglol do
  namespace :file_a do
    task :foo do
      # do some stuff
    end
  end
end

The pattern would be the same for file_b.rake.

Upvotes: 3

Related Questions