Including Gem Rake Tasks in Sinatra

I learned today that Sinatra doesn’t automatically load Rake tasks from included gems (Rails has Railties, which make it easy for a gem builder to add Rake tasks from a gem into your Rails project). Some searching the interwebs turned up an old gem (Alltasks), which unfortunately didn’t work, but had some hints. The solution turned out to be pretty straight forward. Add the following the the Rakefile file in your Sinatra application:

# Rakefile

require 'bundler'
Bundler.setup
Bundler.load.specs.each do |spec|
  spec.load_paths.each do |load_path|
    Dir.glob("#{load_path}/**/*.rake").each do |rake_file|
      load rake_file
    end
  end
end

The above code will load Bundler, step through all the gems you’ve included in your Gemfile, and load any Rake files found within any of your gems. Running rake -T should display tasks from gems you are using in your project.

This entry was posted in Gems, ruby, Sinatra. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *