2007-11-08 00:29:44 -05:00
|
|
|
require "rails/plugin"
|
|
|
|
|
2007-03-01 19:20:32 -05:00
|
|
|
module Rails
|
2007-11-08 00:29:44 -05:00
|
|
|
class Plugin
|
2007-03-01 19:20:32 -05:00
|
|
|
class Loader
|
2007-11-08 00:29:44 -05:00
|
|
|
attr_reader :initializer
|
|
|
|
|
|
|
|
# Creates a new Plugin::Loader instance, associated with the given
|
|
|
|
# Rails::Initializer. This default implementation automatically locates
|
|
|
|
# all plugins, and adds all plugin load paths, when it is created. The plugins
|
|
|
|
# are then fully loaded (init.rb is evaluated) when load_plugins is called.
|
|
|
|
#
|
2007-12-05 13:54:41 -05:00
|
|
|
# It is the loader's responsibility to ensure that only the plugins specified
|
2007-11-08 00:29:44 -05:00
|
|
|
# in the configuration are actually loaded, and that the order defined
|
|
|
|
# is respected.
|
|
|
|
def initialize(initializer)
|
2007-03-01 19:20:32 -05:00
|
|
|
@initializer = initializer
|
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
# Returns the plugins to be loaded, in the order they should be loaded.
|
|
|
|
def plugins
|
|
|
|
@plugins ||= all_plugins.select { |plugin| should_load?(plugin) }.sort { |p1, p2| order_plugins(p1, p2) }
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2007-11-08 00:29:44 -05:00
|
|
|
|
2008-11-26 14:03:25 -05:00
|
|
|
# Returns the plugins that are in engine-form (have an app/ directory)
|
|
|
|
def engines
|
|
|
|
@engines ||= plugins.select(&:engine?)
|
|
|
|
end
|
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
# Returns all the plugins that could be found by the current locators.
|
|
|
|
def all_plugins
|
|
|
|
@all_plugins ||= locate_plugins
|
|
|
|
@all_plugins
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
def load_plugins
|
2009-02-22 11:39:56 -05:00
|
|
|
plugins.each do |plugin|
|
2007-11-08 00:29:44 -05:00
|
|
|
plugin.load(initializer)
|
|
|
|
register_plugin_as_loaded(plugin)
|
|
|
|
end
|
2008-11-26 08:41:20 -05:00
|
|
|
|
2008-11-27 12:59:24 -05:00
|
|
|
configure_engines
|
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
ensure_all_registered_plugins_are_loaded!
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
# Adds the load paths for every plugin into the $LOAD_PATH. Plugin load paths are
|
|
|
|
# added *after* the application's <tt>lib</tt> directory, to ensure that an application
|
|
|
|
# can always override code within a plugin.
|
|
|
|
#
|
2009-02-22 11:39:56 -05:00
|
|
|
# Plugin load paths are also added to Dependencies.load_paths, and Dependencies.load_once_paths.
|
2007-11-08 00:29:44 -05:00
|
|
|
def add_plugin_load_paths
|
|
|
|
plugins.each do |plugin|
|
|
|
|
plugin.load_paths.each do |path|
|
|
|
|
$LOAD_PATH.insert(application_lib_index + 1, path)
|
2008-11-26 08:41:20 -05:00
|
|
|
|
|
|
|
ActiveSupport::Dependencies.load_paths << path
|
|
|
|
|
2009-02-22 11:39:56 -05:00
|
|
|
unless configuration.reload_plugins?
|
2008-06-03 20:19:08 -04:00
|
|
|
ActiveSupport::Dependencies.load_once_paths << path
|
2008-03-31 02:53:44 -04:00
|
|
|
end
|
2007-11-08 00:29:44 -05:00
|
|
|
end
|
|
|
|
end
|
2008-11-26 08:41:20 -05:00
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
$LOAD_PATH.uniq!
|
2008-11-26 14:03:25 -05:00
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2009-02-22 14:23:04 -05:00
|
|
|
def engine_metal_paths
|
|
|
|
engines.collect(&:metal_path)
|
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
protected
|
2008-11-27 12:59:24 -05:00
|
|
|
def configure_engines
|
|
|
|
if engines.any?
|
|
|
|
add_engine_routing_configurations
|
|
|
|
add_engine_controller_paths
|
|
|
|
add_engine_view_paths
|
|
|
|
end
|
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2008-11-27 12:59:24 -05:00
|
|
|
def add_engine_routing_configurations
|
|
|
|
engines.select(&:routed?).collect(&:routing_file).each do |routing_file|
|
|
|
|
ActionController::Routing::Routes.add_configuration_file(routing_file)
|
|
|
|
end
|
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2008-11-27 12:59:24 -05:00
|
|
|
def add_engine_controller_paths
|
|
|
|
ActionController::Routing.controller_paths += engines.collect(&:controller_path)
|
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2008-11-27 12:59:24 -05:00
|
|
|
def add_engine_view_paths
|
|
|
|
# reverse it such that the last engine can overwrite view paths from the first, like with routes
|
2009-02-22 11:39:56 -05:00
|
|
|
paths = ActionView::PathSet.new(engines.collect(&:view_path).reverse)
|
|
|
|
ActionController::Base.view_paths.concat(paths)
|
|
|
|
ActionMailer::Base.view_paths.concat(paths) if configuration.frameworks.include?(:action_mailer)
|
2008-11-27 12:59:24 -05:00
|
|
|
end
|
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
# The locate_plugins method uses each class in config.plugin_locators to
|
|
|
|
# find the set of all plugins available to this Rails application.
|
|
|
|
def locate_plugins
|
2008-11-26 08:41:20 -05:00
|
|
|
configuration.plugin_locators.map do |locator|
|
2007-11-08 00:29:44 -05:00
|
|
|
locator.new(initializer).plugins
|
2008-11-26 08:41:20 -05:00
|
|
|
end.flatten
|
2007-11-08 00:29:44 -05:00
|
|
|
# TODO: sorting based on config.plugins
|
|
|
|
end
|
2007-09-21 18:31:19 -04:00
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
def register_plugin_as_loaded(plugin)
|
|
|
|
initializer.loaded_plugins << plugin
|
2007-03-02 18:39:29 -05:00
|
|
|
end
|
2007-11-08 00:29:44 -05:00
|
|
|
|
|
|
|
def configuration
|
|
|
|
initializer.configuration
|
2007-09-21 18:31:19 -04:00
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
def should_load?(plugin)
|
|
|
|
# uses Plugin#name and Plugin#valid?
|
|
|
|
enabled?(plugin) && plugin.valid?
|
2007-03-02 18:39:29 -05:00
|
|
|
end
|
2007-11-08 00:29:44 -05:00
|
|
|
|
|
|
|
def order_plugins(plugin_a, plugin_b)
|
|
|
|
if !explicit_plugin_loading_order?
|
|
|
|
plugin_a <=> plugin_b
|
|
|
|
else
|
|
|
|
if !explicitly_enabled?(plugin_a) && !explicitly_enabled?(plugin_b)
|
|
|
|
plugin_a <=> plugin_b
|
|
|
|
else
|
|
|
|
effective_order_of(plugin_a) <=> effective_order_of(plugin_b)
|
2009-02-22 11:39:56 -05:00
|
|
|
end
|
2007-11-08 00:29:44 -05:00
|
|
|
end
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
def effective_order_of(plugin)
|
|
|
|
if explicitly_enabled?(plugin)
|
2009-02-22 11:39:56 -05:00
|
|
|
registered_plugin_names.index(plugin.name)
|
2007-11-08 00:29:44 -05:00
|
|
|
else
|
|
|
|
registered_plugin_names.index('all')
|
2009-02-22 11:39:56 -05:00
|
|
|
end
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2007-11-08 00:29:44 -05:00
|
|
|
|
|
|
|
def application_lib_index
|
|
|
|
$LOAD_PATH.index(File.join(RAILS_ROOT, 'lib')) || 0
|
2009-02-22 11:39:56 -05:00
|
|
|
end
|
2007-11-08 00:29:44 -05:00
|
|
|
|
|
|
|
def enabled?(plugin)
|
|
|
|
!explicit_plugin_loading_order? || registered?(plugin)
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2007-11-08 00:29:44 -05:00
|
|
|
|
|
|
|
def explicit_plugin_loading_order?
|
|
|
|
!registered_plugin_names.nil?
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2007-11-08 00:29:44 -05:00
|
|
|
|
|
|
|
def registered?(plugin)
|
|
|
|
explicit_plugin_loading_order? && registered_plugins_names_plugin?(plugin)
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2007-11-08 00:29:44 -05:00
|
|
|
|
|
|
|
def explicitly_enabled?(plugin)
|
|
|
|
!explicit_plugin_loading_order? || explicitly_registered?(plugin)
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2007-11-08 00:29:44 -05:00
|
|
|
|
|
|
|
def explicitly_registered?(plugin)
|
|
|
|
explicit_plugin_loading_order? && registered_plugin_names.include?(plugin.name)
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
def registered_plugins_names_plugin?(plugin)
|
|
|
|
registered_plugin_names.include?(plugin.name) || registered_plugin_names.include?('all')
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
# The plugins that have been explicitly listed with config.plugins. If this list is nil
|
2009-02-22 11:39:56 -05:00
|
|
|
# then it means the client does not care which plugins or in what order they are loaded,
|
2007-11-08 00:29:44 -05:00
|
|
|
# so we load all in alphabetical order. If it is an empty array, we load no plugins, if it is
|
|
|
|
# non empty, we load the named plugins in the order specified.
|
|
|
|
def registered_plugin_names
|
|
|
|
configuration.plugins ? configuration.plugins.map(&:to_s) : nil
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
def loaded?(plugin_name)
|
|
|
|
initializer.loaded_plugins.detect { |plugin| plugin.name == plugin_name.to_s }
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
def ensure_all_registered_plugins_are_loaded!
|
2007-03-02 18:39:29 -05:00
|
|
|
if explicit_plugin_loading_order?
|
2007-11-08 00:29:44 -05:00
|
|
|
if configuration.plugins.detect {|plugin| plugin != :all && !loaded?(plugin) }
|
2009-02-09 10:29:15 -05:00
|
|
|
missing_plugins = configuration.plugins - (plugins.map{|p| p.name.to_sym} + [:all])
|
2009-02-27 08:22:39 -05:00
|
|
|
raise LoadError, "Could not locate the following plugins: #{missing_plugins.to_sentence(:locale => :en)}"
|
2007-09-21 18:31:19 -04:00
|
|
|
end
|
2007-03-02 18:39:29 -05:00
|
|
|
end
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
2009-02-22 11:39:56 -05:00
|
|
|
|
2007-03-01 19:20:32 -05:00
|
|
|
end
|
|
|
|
end
|
2009-02-22 14:23:04 -05:00
|
|
|
end
|