From cae80a77169bdebc980dbed7fe8f84a78175b053 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 19 Apr 2012 21:42:35 -0700 Subject: [PATCH] Make Vagrant::Plugin a module --- lib/vagrant/environment.rb | 2 +- lib/vagrant/plugin.rb | 83 ++------------------------------ lib/vagrant/plugin/gem_loader.rb | 82 +++++++++++++++++++++++++++++++ lib/vagrant/plugin/v1.rb | 2 +- 4 files changed, 87 insertions(+), 82 deletions(-) create mode 100644 lib/vagrant/plugin/gem_loader.rb diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index b4d057725..f6a0459d3 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -519,7 +519,7 @@ module Vagrant ::Gem.clear_paths # Load the plugins - Plugin.load! + Plugin::GemLoader.load! end end end diff --git a/lib/vagrant/plugin.rb b/lib/vagrant/plugin.rb index 2237cfd9b..bbfcdbfe5 100644 --- a/lib/vagrant/plugin.rb +++ b/lib/vagrant/plugin.rb @@ -1,84 +1,7 @@ -require "rubygems" - -require "log4r" - module Vagrant - # Represents a single plugin and also manages loading plugins from - # RubyGems. If a plugin has a `vagrant_init.rb` file somewhere on its - # load path, then this class will find it and load it. For logging purposes - # (for debugging), the list of loaded plugins is stored in the {plugins} - # array. - class Plugin - # XXX: Make Plugin a module at some point - autoload :V1, 'vagrant/plugin/v1' + module Plugin + autoload :GemLoader, "vagrant/plugin/gem_loader" - # The array of gem specifications that were loaded as plugins. - @@plugins = [] - - # Loads all the plugins for Vagrant. Plugins are currently - # gems which have a "vagrant_init.rb" somewhere on their - # load path. This file is loaded to kick off the load sequence - # for that plugin. - def self.load! - logger = Log4r::Logger.new("vagrant::plugin") - logger.info("Searching and loading any available plugins...") - - # Our version is used for checking dependencies - our_version = Gem::Version.create(Vagrant::VERSION) - - # RubyGems 1.8.0 deprecated `source_index`. Gem::Specification is the - # new replacement. For now, we support both, but special-case 1.8.x - # so that we avoid deprecation messages. - index = Gem::VERSION >= "1.8.0" ? Gem::Specification : Gem.source_index - - # Stupid hack since Rails 2.3.x overrides Gem.source_index with their - # own incomplete replacement which causes issues. - index = [index.installed_source_index, index.vendor_source_index] if defined?(Rails::VendorGemSourceIndex) && index.is_a?(Rails::VendorGemSourceIndex) - - # Look for a vagrant_init.rb in all the gems, but only the - # latest version of the gems. - [index].flatten.each do |source| - # In 1.6.0, added the option of including prerelease gems, which is - # useful for developers. - specs = Gem::VERSION >= "1.6.0" ? source.latest_specs(true) : source.latest_specs - - specs.each do |spec| - if @@plugins.include?(spec) - logger.debug("Plugin already loaded, not loading again: #{spec.name}") - next - end - - # If this gem depends on Vagrant, verify this is a valid release of - # Vagrant for this gem to load into. - vagrant_dep = spec.dependencies.find { |d| d.name == "vagrant" } - if vagrant_dep && !vagrant_dep.requirement.satisfied_by?(our_version) - logger.debug("Plugin Vagrant dependency mismatch: #{spec.name} (#{spec.version})") - next - end - - # Find a vagrant_init.rb to verify if this is a plugin - file = nil - if Gem::VERSION >= "1.8.0" - file = spec.matches_for_glob("**/vagrant_init.rb").first - else - file = Gem.searcher.matching_files(spec, "vagrant_init.rb").first - end - - next if !file - - logger.info("Loading plugin: #{spec.name} (#{spec.version})") - @@plugins << spec - load file - end - - logger.info("Loaded #{@@plugins.length} plugins.") - end - end - - # Returns the array of plugins which are currently loaded by - # Vagrant. - # - # @return [Array] - def self.plugins; @@plugins; end + autoload :V1, "vagrant/plugin/v1" end end diff --git a/lib/vagrant/plugin/gem_loader.rb b/lib/vagrant/plugin/gem_loader.rb new file mode 100644 index 000000000..ca95eec60 --- /dev/null +++ b/lib/vagrant/plugin/gem_loader.rb @@ -0,0 +1,82 @@ +require "rubygems" + +require "log4r" + +module Vagrant + module Plugin + # Manages loading plugins from RubyGems. If a RubyGem has a `vagrant_init.rb` + # file somewhere on its load path, then this class will find it and load it. + # For logging purposes and debugging, the list of loaded plugins is + # stored in the {plugins} array. + class GemLoader + # The array of gem specifications that were loaded as plugins. + @@plugins = [] + + # Loads all the plugins for Vagrant. Plugins are currently + # gems which have a "vagrant_init.rb" somewhere on their + # load path. This file is loaded to kick off the load sequence + # for that plugin. + def self.load! + logger = Log4r::Logger.new("vagrant::plugin") + logger.info("Searching and loading any available plugins...") + + # Our version is used for checking dependencies + our_version = Gem::Version.create(Vagrant::VERSION) + + # RubyGems 1.8.0 deprecated `source_index`. Gem::Specification is the + # new replacement. For now, we support both, but special-case 1.8.x + # so that we avoid deprecation messages. + index = Gem::VERSION >= "1.8.0" ? Gem::Specification : Gem.source_index + + # Stupid hack since Rails 2.3.x overrides Gem.source_index with their + # own incomplete replacement which causes issues. + index = [index.installed_source_index, index.vendor_source_index] if defined?(Rails::VendorGemSourceIndex) && index.is_a?(Rails::VendorGemSourceIndex) + + # Look for a vagrant_init.rb in all the gems, but only the + # latest version of the gems. + [index].flatten.each do |source| + # In 1.6.0, added the option of including prerelease gems, which is + # useful for developers. + specs = Gem::VERSION >= "1.6.0" ? source.latest_specs(true) : source.latest_specs + + specs.each do |spec| + if @@plugins.include?(spec) + logger.debug("Plugin already loaded, not loading again: #{spec.name}") + next + end + + # If this gem depends on Vagrant, verify this is a valid release of + # Vagrant for this gem to load into. + vagrant_dep = spec.dependencies.find { |d| d.name == "vagrant" } + if vagrant_dep && !vagrant_dep.requirement.satisfied_by?(our_version) + logger.debug("Plugin Vagrant dependency mismatch: #{spec.name} (#{spec.version})") + next + end + + # Find a vagrant_init.rb to verify if this is a plugin + file = nil + if Gem::VERSION >= "1.8.0" + file = spec.matches_for_glob("**/vagrant_init.rb").first + else + file = Gem.searcher.matching_files(spec, "vagrant_init.rb").first + end + + next if !file + + logger.info("Loading plugin: #{spec.name} (#{spec.version})") + @@plugins << spec + load file + end + + logger.info("Loaded #{@@plugins.length} plugins.") + end + end + + # Returns the array of plugins which are currently loaded by + # Vagrant. + # + # @return [Array] + def self.plugins; @@plugins; end + end + end +end diff --git a/lib/vagrant/plugin/v1.rb b/lib/vagrant/plugin/v1.rb index a04ec0053..0b1004569 100644 --- a/lib/vagrant/plugin/v1.rb +++ b/lib/vagrant/plugin/v1.rb @@ -1,7 +1,7 @@ require "log4r" module Vagrant - class Plugin + module Plugin # The superclass for version 1 plugins. class V1 LOGGER = Log4r::Logger.new("vagrant::plugin::v1")