Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make rake task from gem available everywhere?

Tags:

ruby

rake

So I'm writing a small gem and I have a '/tasks' dir in it with some specific rake tasks. How do I make those tasks available automatically everywhere, where the gem is required? For example I wish I could run 'rake mygemrake:task' inside my rails root dir after I have the gem installed.

like image 508
snitko Avatar asked Apr 12 '09 23:04

snitko


2 Answers

Check out the rdoctask in rake for an example of how to define a task provided by a gem. The task is defined in ruby instead of the rake build language and can be required like so:

require 'rake'             # the gem
require 'rake/rdoctask'    # the task
like image 39
dstnbrkr Avatar answered Nov 19 '22 18:11

dstnbrkr


For Rails3 applications, you might want to look into making a Railtie for your gem.

You can do so with:

lib/your_gem/railtie.rb

require 'your_gem'
require 'rails'
module YourGem
  class Railtie < Rails::Railtie
    rake_tasks do
      require 'path/to/rake.task'
    end
  end
end

lib/your_gem.rb

module YourGem
  require "lib/your_gem/railtie" if defined?(Rails)
end

Though, I had my share of difficulties with requiring the rake.task file in my railtie.rb. I opted to just define my measley one or two tasks within the rake_tasks block.

like image 102
kelly.dunn Avatar answered Nov 19 '22 16:11

kelly.dunn