Permalink
Browse files

Add the ability to have plugins load rake tasks.

  • Loading branch information...
1 parent 9a650a6 commit c02f2782631a1893e2e880e3ccc65fb9a734567a Carl Lerche committed Dec 29, 2009
View
5 railties/lib/rails/application.rb
@@ -72,7 +72,12 @@ def reload_routes!
def load_tasks
require "rails/tasks"
+ # Load all extension rake tasks
+ plugins.each(&:load_tasks)
+ # Load all plugin tasks
Dir["#{root}/vendor/plugins/*/**/tasks/**/*.rake"].sort.each { |ext| load ext }
+ # Load all application tasks
+ # TODO: extract out the path to the rake tasks
Dir["#{root}/lib/tasks/**/*.rake"].sort.each { |ext| load ext }
task :environment do
$rails_rake_task = true
View
10 railties/lib/rails/plugin.rb
@@ -25,6 +25,16 @@ def self.config
Configuration.default
end
+ def self.rake_tasks(&blk)
+ @rake_tasks ||= []
+ @rake_tasks << blk
+ end
+
+ def self.load_tasks
+ return unless @rake_tasks
+ @rake_tasks.each { |blk| blk.call }
+ end
+
# Creates an initializer which includes all given modules to the given class.
#
# module Rails
View
27 railties/test/plugins/framework_extension_test.rb
@@ -5,6 +5,33 @@ class FrameworkExtensionTest < Test::Unit::TestCase
def setup
build_app
boot_rails
+ require "rails"
+ end
+
+ test "rake_tasks block is executed when MyApp.load_tasks is called" do
+ $ran_block = false
+
+ class MyPlugin < Rails::Plugin
+ rake_tasks do
+ $ran_block = true
+ end
+ end
+
+ require "#{app_path}/config/environment"
+
+ assert !$ran_block
+ require 'rake'
+ require 'rake/testtask'
+ require 'rake/rdoctask'
+ AppTemplate::Application.load_tasks
+ assert $ran_block
+ end
+ end
+
+ class ActiveRecordExtensionTest < Test::Unit::TestCase
+ def setup
+ build_app
+ boot_rails
end
test "active_record extensions are applied to ActiveRecord" do

3 comments on commit c02f278

@KieranP

Maybe it can also extract rake tasks from a gem? You might search in [gem_root]/tasks and [gem_root]/lib/tasks . It's the one thing I've noticed that causes issues converting from plugin to gem.

@carllerche

I'm probably using the wrong terminology, but by "plugin" I mean any rails extension (in gems or whatever). So, what you ask for is possible on master right now. See activerecord/lib/active_record/rails.rb

@KieranP

I see. So bundled libs define a plugin structure, in which contains a 'rake_tasks' call with a block with load calls to each rake task, and any initializers it needs.... very nice idea.

Please sign in to comment.