| 187 | | if configuration.plugins.nil? |
|---|
| 188 | | # a nil value implies we don't care about plugins; load 'em all in a reliable order |
|---|
| 189 | | find_plugins(configuration.plugin_paths).sort.each { |path| load_plugin path } |
|---|
| 190 | | elsif !configuration.plugins.empty? |
|---|
| 191 | | # we've specified a config.plugins array, so respect that order |
|---|
| 192 | | plugin_paths = find_plugins(configuration.plugin_paths) |
|---|
| 193 | | configuration.plugins.each do |name| |
|---|
| 194 | | path = plugin_paths.find { |p| File.basename(p) == name } |
|---|
| 195 | | raise(LoadError, "Cannot find the plugin '#{name}'!") if path.nil? |
|---|
| 196 | | load_plugin path |
|---|
| 197 | | end |
|---|
| 198 | | end |
|---|
| 199 | | $LOAD_PATH.uniq! |
|---|
| | 190 | Plugin::Locater.new(self).each do |plugin| |
|---|
| | 191 | plugin.load |
|---|
| | 192 | end |
|---|
| | 193 | $LOAD_PATH.uniq! |
|---|
| 346 | | |
|---|
| 347 | | protected |
|---|
| 348 | | # Return a list of plugin paths within base_path. A plugin path is |
|---|
| 349 | | # a directory that contains either a lib directory or an init.rb file. |
|---|
| 350 | | # This recurses into directories which are not plugin paths, so you |
|---|
| 351 | | # may organize your plugins within the plugin path. |
|---|
| 352 | | def find_plugins(*base_paths) |
|---|
| 353 | | base_paths.flatten.inject([]) do |plugins, base_path| |
|---|
| 354 | | Dir.glob(File.join(base_path, '*')).each do |path| |
|---|
| 355 | | if plugin_path?(path) |
|---|
| 356 | | plugins << path if plugin_enabled?(path) |
|---|
| 357 | | elsif File.directory?(path) |
|---|
| 358 | | plugins += find_plugins(path) |
|---|
| 359 | | end |
|---|
| 360 | | end |
|---|
| 361 | | plugins |
|---|
| 362 | | end |
|---|
| 363 | | end |
|---|
| 364 | | |
|---|
| 365 | | def plugin_path?(path) |
|---|
| 366 | | File.directory?(path) and (File.directory?(File.join(path, 'lib')) or File.file?(File.join(path, 'init.rb'))) |
|---|
| 367 | | end |
|---|
| 368 | | |
|---|
| 369 | | def plugin_enabled?(path) |
|---|
| 370 | | configuration.plugins.nil? || configuration.plugins.include?(File.basename(path)) |
|---|
| 371 | | end |
|---|
| 372 | | |
|---|
| 373 | | # Load the plugin at <tt>path</tt> unless already loaded. |
|---|
| 374 | | # |
|---|
| 375 | | # Each plugin is initialized: |
|---|
| 376 | | # * add its +lib+ directory, if present, to the beginning of the load path |
|---|
| 377 | | # * evaluate <tt>init.rb</tt> if present |
|---|
| 378 | | # |
|---|
| 379 | | # Returns <tt>true</tt> if the plugin is successfully loaded or |
|---|
| 380 | | # <tt>false</tt> if it is already loaded (similar to Kernel#require). |
|---|
| 381 | | # Raises <tt>LoadError</tt> if the plugin is not found. |
|---|
| 382 | | def load_plugin(directory) |
|---|
| 383 | | name = File.basename(directory) |
|---|
| 384 | | return false if loaded_plugins.include?(name) |
|---|
| 385 | | |
|---|
| 386 | | # Catch nonexistent and empty plugins. |
|---|
| 387 | | raise LoadError, "No such plugin: #{directory}" unless plugin_path?(directory) |
|---|
| 388 | | |
|---|
| 389 | | lib_path = File.join(directory, 'lib') |
|---|
| 390 | | init_path = File.join(directory, 'init.rb') |
|---|
| 391 | | has_lib = File.directory?(lib_path) |
|---|
| 392 | | has_init = File.file?(init_path) |
|---|
| 393 | | |
|---|
| 394 | | # Add lib to load path *after* the application lib, to allow |
|---|
| 395 | | # application libraries to override plugin libraries. |
|---|
| 396 | | if has_lib |
|---|
| 397 | | application_lib_index = $LOAD_PATH.index(File.join(RAILS_ROOT, "lib")) || 0 |
|---|
| 398 | | $LOAD_PATH.insert(application_lib_index + 1, lib_path) |
|---|
| 399 | | Dependencies.load_paths << lib_path |
|---|
| 400 | | Dependencies.load_once_paths << lib_path |
|---|
| 401 | | end |
|---|
| 402 | | |
|---|
| 403 | | # Allow plugins to reference the current configuration object |
|---|
| 404 | | config = configuration |
|---|
| 405 | | |
|---|
| 406 | | # Add to set of loaded plugins before 'name' collapsed in eval. |
|---|
| 407 | | loaded_plugins << name |
|---|
| 408 | | |
|---|
| 409 | | # Evaluate init.rb. |
|---|
| 410 | | silence_warnings { eval(IO.read(init_path), binding, init_path) } if has_init |
|---|
| 411 | | |
|---|
| 412 | | true |
|---|
| 413 | | end |
|---|