diff options
Diffstat (limited to 'railties/lib/initializer.rb')
-rw-r--r-- | railties/lib/initializer.rb | 98 |
1 files changed, 17 insertions, 81 deletions
diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index b535c8624b..b2b7b3b545 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -2,6 +2,9 @@ require 'logger' require 'set' require File.join(File.dirname(__FILE__), 'railties_path') require File.join(File.dirname(__FILE__), 'rails/version') +require File.join(File.dirname(__FILE__), 'plugin/locater') +require File.join(File.dirname(__FILE__), 'plugin/loader') + RAILS_ENV = (ENV['RAILS_ENV'] || 'development').dup unless defined?(RAILS_ENV) @@ -184,19 +187,10 @@ module Rails # will be loaded in that order. Otherwise, plugins are loaded in alphabetical # order. def load_plugins - if configuration.plugins.nil? - # a nil value implies we don't care about plugins; load 'em all in a reliable order - find_plugins(configuration.plugin_paths).sort.each { |path| load_plugin path } - elsif !configuration.plugins.empty? - # we've specified a config.plugins array, so respect that order - plugin_paths = find_plugins(configuration.plugin_paths) - configuration.plugins.each do |name| - path = plugin_paths.find { |p| File.basename(p) == name } - raise(LoadError, "Cannot find the plugin '#{name}'!") if path.nil? - load_plugin path - end - end - $LOAD_PATH.uniq! + Plugin::Locater.new(self).each do |plugin| + plugin.load + end + $LOAD_PATH.uniq! end # Loads the environment specified by Configuration#environment_path, which @@ -343,74 +337,6 @@ module Rails load(initializer) end end - - protected - # Return a list of plugin paths within base_path. A plugin path is - # a directory that contains either a lib directory or an init.rb file. - # This recurses into directories which are not plugin paths, so you - # may organize your plugins within the plugin path. - def find_plugins(*base_paths) - base_paths.flatten.inject([]) do |plugins, base_path| - Dir.glob(File.join(base_path, '*')).each do |path| - if plugin_path?(path) - plugins << path if plugin_enabled?(path) - elsif File.directory?(path) - plugins += find_plugins(path) - end - end - plugins - end - end - - def plugin_path?(path) - File.directory?(path) and (File.directory?(File.join(path, 'lib')) or File.file?(File.join(path, 'init.rb'))) - end - - def plugin_enabled?(path) - configuration.plugins.nil? || configuration.plugins.include?(File.basename(path)) - end - - # Load the plugin at <tt>path</tt> unless already loaded. - # - # Each plugin is initialized: - # * add its +lib+ directory, if present, to the beginning of the load path - # * evaluate <tt>init.rb</tt> if present - # - # Returns <tt>true</tt> if the plugin is successfully loaded or - # <tt>false</tt> if it is already loaded (similar to Kernel#require). - # Raises <tt>LoadError</tt> if the plugin is not found. - def load_plugin(directory) - name = File.basename(directory) - return false if loaded_plugins.include?(name) - - # Catch nonexistent and empty plugins. - raise LoadError, "No such plugin: #{directory}" unless plugin_path?(directory) - - lib_path = File.join(directory, 'lib') - init_path = File.join(directory, 'init.rb') - has_lib = File.directory?(lib_path) - has_init = File.file?(init_path) - - # Add lib to load path *after* the application lib, to allow - # application libraries to override plugin libraries. - if has_lib - application_lib_index = $LOAD_PATH.index(File.join(RAILS_ROOT, "lib")) || 0 - $LOAD_PATH.insert(application_lib_index + 1, lib_path) - Dependencies.load_paths << lib_path - Dependencies.load_once_paths << lib_path - end - - # Allow plugins to reference the current configuration object - config = configuration - - # Add to set of loaded plugins before 'name' collapsed in eval. - loaded_plugins << name - - # Evaluate init.rb. - silence_warnings { eval(IO.read(init_path), binding, init_path) } if has_init - - true - end end # The Configuration class holds all the parameters for the Initializer and @@ -502,6 +428,11 @@ module Rails # The path to the root of the plugins directory. By default, it is in # <tt>vendor/plugins</tt>. attr_accessor :plugin_paths + + # The class that handles loading each plugin. Defaults to Rails::Plugin::Loader, but + # a sub class would have access to fine grained modification of the loading behavior. See + # the implementation of Rails::Plugin::Loader for more details. + attr_accessor :plugin_loader # Create a new Configuration instance, initialized with the default # values. @@ -518,6 +449,7 @@ module Rails self.whiny_nils = default_whiny_nils self.plugins = default_plugins self.plugin_paths = default_plugin_paths + self.plugin_loader = default_plugin_loader self.database_configuration_file = default_database_configuration_file for framework in default_frameworks @@ -672,6 +604,10 @@ module Rails def default_plugin_paths ["#{root_path}/vendor/plugins"] end + + def default_plugin_loader + Plugin::Loader + end end end |