jsharpe
jsharpe

Reputation: 2679

Including rake tasks in gems

1) Is there a 'best' place for rake tasks inside of gems? I've seen them in /tasks, /lib/tasks, and I've seen them written as *.rb and *.rake -- not sure which (if any) is 'correct'

2) How do I make them available to the app once the gem is configured in the environment?

Upvotes: 62

Views: 18507

Answers (4)

stevec
stevec

Reputation: 52967

Something that helped me a lot was to find a small gem to use as an example.

I used ryanb's letter_opener gem as an example, because it's a small gem and is easy to trace through the code.

Other tips

  • When you're just getting started, create a test rake task called zzzzzzzzzz, that way, after you build and install your gem and run something like rake -T -A, you'll be able to spot whether it's there without scrolling.
  • Don't forget to build and re-install your gem after each change. Something like:
gem build my_gem.gemspec 
gem install my_gem 

# Or, if you're testing in the context of a rails app, add this to Gemfile
# gem "my_app", path: "../my_app"
# then bundle after each change to the gem

Resources

  • Reading about railties here.
  • This guide.
    • Similar guide here.

Upvotes: 0

edebill
edebill

Reputation: 7715

On Rails 3, you do this via Railties. Here's the code to do it for a gem I just made:

class BackupTask < Rails::Railtie
  rake_tasks do
    Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
  end
end

So you basically create a class that inherits from Rails::Railtie, then within that class you have a rake_tasks block that loads the relevant files. You must load instead of require if you want to use a .rake extension.

I found that I need to specify the full path to Dir (hence the File.join gymnastics). If I just wanted to list the file explicitly then I could get away with just saying load 'tasks/foo.rake' because the /lib dir of my gem was in the load path.

Upvotes: 45

Simone Carletti
Simone Carletti

Reputation: 176552

Either /tasks and lib/tasks are a good place. I use /tasks when the tasks are meant to be used for working or managing the Gem itself, I use lib/tasks when the tasks are meant to be loaded by Gem users.

This is because users might have some problem in loading ruby files outside the lib folder which is automatically appended to the load path when the Gem is requires.

Also, I suggest you to use the *.rake extension. Nowadays almost every IDE associates .rake extension to ruby files and most advanced IDE can even parse the content as rake scripts. The .rake extension is a better choice for developers too because you can provide an immediate overview of the content inside the file.

About your second question, currently there's no way to have Rails loading your rake tasks when the plugin is packaged as Gem. You need to include the rake file in your application (in an other rake file or in the main Rakefile) or clone the rake tasks in your project.

If you install the plugin in the vendor folder, all *.rake files in both tasks and lib/tasks directories are automatically loaded into your Rails scope by the following two lines.

Dir["#{RAILS_ROOT}/vendor/plugins/*/tasks/**/*.rake"].sort.each { |ext| load ext }
Dir["#{RAILS_ROOT}/vendor/plugins/*/lib/tasks/**/*.rake"].sort.each { |ext| load ext }

Upvotes: 31

Steven Soroka
Steven Soroka

Reputation: 19902

for Rails 3 support, the top line of your lib/.rb file can be:

Dir["tasks/**/*.rake"].each { |ext| load ext } if defined?(Rake)

for Rails 2 support, the gem installer will have to edit their Rakefile and add:

Dir["#{Gem.searcher.find('<gemname>').full_gem_path}/lib/tasks/**/*.rake"].each { |ext| load ext }

after the require statements.

NOTES:

  • Be sure to replace with the name of your actual gem.
  • This assumes all Rails-visible rake tasks are in lib/tasks/

I haven't found anything easier yet. Comments?

Upvotes: 12

Related Questions