From 8d529931ef4e3fbe8f99c5c18c10cd46c075b805 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 7 Jan 2011 01:12:16 -0800 Subject: [PATCH] Be gone glob loader! Move everything to autoload or explicit require. --- lib/vagrant.rb | 29 +++++-------- lib/vagrant/action.rb | 12 ++++++ lib/vagrant/action/box.rb | 11 +++++ lib/vagrant/action/env.rb | 7 ++++ lib/vagrant/action/general.rb | 8 ++++ lib/vagrant/action/vm.rb | 30 ++++++++++++++ lib/vagrant/command.rb | 25 +++++++++++ lib/vagrant/config.rb | 73 ++++----------------------------- lib/vagrant/config/top.rb | 59 ++++++++++++++++++++++++++ lib/vagrant/downloaders.rb | 7 ++++ lib/vagrant/hosts.rb | 7 ++++ lib/vagrant/provisioners.rb | 8 ++++ lib/vagrant/systems.rb | 5 +++ lib/vagrant/util/glob_loader.rb | 24 ----------- 14 files changed, 198 insertions(+), 107 deletions(-) create mode 100644 lib/vagrant/action/box.rb create mode 100644 lib/vagrant/action/env.rb create mode 100644 lib/vagrant/action/general.rb create mode 100644 lib/vagrant/action/vm.rb create mode 100644 lib/vagrant/command.rb create mode 100644 lib/vagrant/config/top.rb create mode 100644 lib/vagrant/downloaders.rb create mode 100644 lib/vagrant/hosts.rb create mode 100644 lib/vagrant/provisioners.rb create mode 100644 lib/vagrant/systems.rb delete mode 100644 lib/vagrant/util/glob_loader.rb diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 6c6f50eff..9e4b8ce16 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -4,25 +4,22 @@ require 'i18n' require 'virtualbox' module Vagrant - # TODO: Move more classes over to the autoload model. We'll - # start small, but slowly move everything over. - + autoload :Action, 'vagrant/action' autoload :Box, 'vagrant/box' autoload :BoxCollection, 'vagrant/box_collection' autoload :CLI, 'vagrant/cli' autoload :Config, 'vagrant/config' autoload :DataStore, 'vagrant/data_store' + autoload :Downloaders, 'vagrant/downloaders' + autoload :Environment, 'vagrant/environment' autoload :Errors, 'vagrant/errors' + autoload :Hosts, 'vagrant/hosts' autoload :Plugin, 'vagrant/plugin' + autoload :SSH, 'vagrant/ssh' autoload :TestHelpers, 'vagrant/test_helpers' + autoload :UI, 'vagrant/ui' autoload :Util, 'vagrant/util' - - module Command - autoload :Base, 'vagrant/command/base' - autoload :GroupBase, 'vagrant/command/group_base' - autoload :Helpers, 'vagrant/command/helpers' - autoload :NamedBase, 'vagrant/command/named_base' - end + autoload :VM, 'vagrant/vm' # The source root is the path to the root directory of # the Vagrant gem. @@ -34,13 +31,9 @@ end # Default I18n to load the en locale I18n.load_path << File.expand_path("templates/locales/en.yml", Vagrant.source_root) -# Load them up. One day we'll convert this to autoloads. Today -# is not that day. Low hanging fruit for anyone wishing to do it. -libdir = File.expand_path("lib/vagrant", Vagrant.source_root) -Vagrant::Util::GlobLoader.glob_require(libdir, %w{ - downloaders/base provisioners/base provisioners/chef systems/base - hosts/base}) - -# Initialize the built-in actions and load the plugins. +# Load the things which must be loaded before anything else +require 'vagrant/command' +require 'vagrant/provisioners' +require 'vagrant/systems' Vagrant::Action.builtin! Vagrant::Plugin.load! diff --git a/lib/vagrant/action.rb b/lib/vagrant/action.rb index 367d93492..6854e97ec 100644 --- a/lib/vagrant/action.rb +++ b/lib/vagrant/action.rb @@ -1,3 +1,12 @@ +require 'vagrant/action/builder' +require 'vagrant/action/builtin' + +# The builtin middlewares +require 'vagrant/action/box' +require 'vagrant/action/env' +require 'vagrant/action/general' +require 'vagrant/action/vm' + module Vagrant # Manages action running and registration. Every Vagrant environment # has an instance of {Action} to allow for running in the context of @@ -44,6 +53,9 @@ module Vagrant # Where `:name` is the name of the registered action. # class Action + autoload :Environment, 'vagrant/action/environment' + autoload :Warden, 'vagrant/action/warden' + include Util @@reported_interrupt = false diff --git a/lib/vagrant/action/box.rb b/lib/vagrant/action/box.rb new file mode 100644 index 000000000..97efea94d --- /dev/null +++ b/lib/vagrant/action/box.rb @@ -0,0 +1,11 @@ +module Vagrant + class Action + module Box + autoload :Destroy, 'vagrant/action/box/destroy' + autoload :Download, 'vagrant/action/box/download' + autoload :Package, 'vagrant/action/box/package' + autoload :Unpackage, 'vagrant/action/box/unpackage' + autoload :Verify, 'vagrant/action/box/verify' + end + end +end diff --git a/lib/vagrant/action/env.rb b/lib/vagrant/action/env.rb new file mode 100644 index 000000000..e9b119901 --- /dev/null +++ b/lib/vagrant/action/env.rb @@ -0,0 +1,7 @@ +module Vagrant + class Action + module Env + autoload :Set, 'vagrant/action/env/set' + end + end +end diff --git a/lib/vagrant/action/general.rb b/lib/vagrant/action/general.rb new file mode 100644 index 000000000..9df43458e --- /dev/null +++ b/lib/vagrant/action/general.rb @@ -0,0 +1,8 @@ +module Vagrant + class Action + module General + autoload :Package, 'vagrant/action/general/package' + autoload :Validate, 'vagrant/action/general/validate' + end + end +end diff --git a/lib/vagrant/action/vm.rb b/lib/vagrant/action/vm.rb new file mode 100644 index 000000000..5e862a690 --- /dev/null +++ b/lib/vagrant/action/vm.rb @@ -0,0 +1,30 @@ +module Vagrant + class Action + module VM + autoload :Boot, 'vagrant/action/vm/boot' + autoload :CheckBox, 'vagrant/action/vm/check_box' + autoload :CheckGuestAdditions, 'vagrant/action/vm/check_guest_additions' + autoload :CleanMachineFolder, 'vagrant/action/vm/clean_machine_folder' + autoload :ClearForwardedPorts, 'vagrant/action/vm/clear_forwarded_ports' + autoload :ClearNFSExports, 'vagrant/action/vm/clear_nfs_exports' + autoload :ClearSharedFolders, 'vagrant/action/vm/clear_shared_folders' + autoload :Customize, 'vagrant/action/vm/customize' + autoload :Destroy, 'vagrant/action/vm/destroy' + autoload :DestroyUnusedNetworkInterfaces, 'vagrant/action/vm/destroy_unused_network_interfaces' + autoload :DiscardState, 'vagrant/action/vm/discard_state' + autoload :Export, 'vagrant/action/vm/export' + autoload :ForwardPorts, 'vagrant/action/vm/forward_ports' + autoload :Halt, 'vagrant/action/vm/halt' + autoload :Import, 'vagrant/action/vm/import' + autoload :MatchMACAddress, 'vagrant/action/vm/match_mac_address' + autoload :Network, 'vagrant/action/vm/network' + autoload :NFS, 'vagrant/action/vm/nfs' + autoload :Package, 'vagrant/action/vm/package' + autoload :PackageVagrantfile, 'vagrant/action/vm/package_vagrantfile' + autoload :Provision, 'vagrant/action/vm/provision' + autoload :Resume, 'vagrant/action/vm/resume' + autoload :ShareFolders, 'vagrant/action/vm/share_folders' + autoload :Suspend, 'vagrant/action/vm/suspend' + end + end +end diff --git a/lib/vagrant/command.rb b/lib/vagrant/command.rb new file mode 100644 index 000000000..bf15b1b85 --- /dev/null +++ b/lib/vagrant/command.rb @@ -0,0 +1,25 @@ +module Vagrant + module Command + autoload :Base, 'vagrant/command/base' + autoload :GroupBase, 'vagrant/command/group_base' + autoload :Helpers, 'vagrant/command/helpers' + autoload :NamedBase, 'vagrant/command/named_base' + end +end + +# The built-in commands must always be loaded +require 'vagrant/command/box' +require 'vagrant/command/destroy' +require 'vagrant/command/halt' +require 'vagrant/command/init' +require 'vagrant/command/package' +require 'vagrant/command/provision' +require 'vagrant/command/reload' +require 'vagrant/command/resume' +require 'vagrant/command/ssh' +require 'vagrant/command/ssh_config' +require 'vagrant/command/status' +require 'vagrant/command/suspend' +require 'vagrant/command/up' +require 'vagrant/command/upgrade_to_060' +require 'vagrant/command/version' diff --git a/lib/vagrant/config.rb b/lib/vagrant/config.rb index 6d43c5eb9..0d8b69402 100644 --- a/lib/vagrant/config.rb +++ b/lib/vagrant/config.rb @@ -1,5 +1,13 @@ require 'vagrant/config/base' require 'vagrant/config/error_recorder' +require 'vagrant/config/top' + +# The built-in configuration classes +require 'vagrant/config/vagrant' +require 'vagrant/config/ssh' +require 'vagrant/config/nfs' +require 'vagrant/config/vm' +require 'vagrant/config/package' module Vagrant # The config class is responsible for loading Vagrant configurations, which @@ -112,69 +120,4 @@ module Vagrant end end end - - class Config - # This class is the "top" configure class, which handles registering - # other configuration classes as well as validation of all configured - # classes. This is the object which is returned by {Environment#config} - # and has accessors to all other configuration classes. - # - # If you're looking to create your own configuration class, see {Base}. - class Top < Base - @@configures = {} if !defined?(@@configures) - - class << self - # The list of registered configuration classes as well as the key - # they're registered under. - def configures_list - @@configures ||= {} - end - - # Registers a configuration class with the given key. This method shouldn't - # be called. Instead, inherit from {Base} and call {Base.configures}. - def configures(key, klass) - configures_list[key] = klass - attr_reader key.to_sym - end - end - - def initialize(env=nil) - self.class.configures_list.each do |key, klass| - config = klass.new - config.env = env - config.top = self - instance_variable_set("@#{key}".to_sym, config) - end - - @env = env - end - - # Validates the configuration classes of this instance and raises an - # exception if they are invalid. If you are implementing a custom configuration - # class, the method you want to implement is {Base#validate}. This is - # the method that checks all the validation, not one which defines - # validation rules. - def validate! - # Validate each of the configured classes and store the results into - # a hash. - errors = self.class.configures_list.inject({}) do |container, data| - key, _ = data - recorder = ErrorRecorder.new - send(key.to_sym).validate(recorder) - container[key.to_sym] = recorder if !recorder.errors.empty? - container - end - - return if errors.empty? - raise Errors::ConfigValidationFailed, :messages => Util::TemplateRenderer.render("config/validation_failed", :errors => errors) - end - end - end end - -# The built-in configuration classes -require 'vagrant/config/vagrant' -require 'vagrant/config/ssh' -require 'vagrant/config/nfs' -require 'vagrant/config/vm' -require 'vagrant/config/package' diff --git a/lib/vagrant/config/top.rb b/lib/vagrant/config/top.rb new file mode 100644 index 000000000..a9e7de8b6 --- /dev/null +++ b/lib/vagrant/config/top.rb @@ -0,0 +1,59 @@ +module Vagrant + class Config + # This class is the "top" configure class, which handles registering + # other configuration classes as well as validation of all configured + # classes. This is the object which is returned by {Environment#config} + # and has accessors to all other configuration classes. + # + # If you're looking to create your own configuration class, see {Base}. + class Top < Base + @@configures = {} if !defined?(@@configures) + + class << self + # The list of registered configuration classes as well as the key + # they're registered under. + def configures_list + @@configures ||= {} + end + + # Registers a configuration class with the given key. This method shouldn't + # be called. Instead, inherit from {Base} and call {Base.configures}. + def configures(key, klass) + configures_list[key] = klass + attr_reader key.to_sym + end + end + + def initialize(env=nil) + self.class.configures_list.each do |key, klass| + config = klass.new + config.env = env + config.top = self + instance_variable_set("@#{key}".to_sym, config) + end + + @env = env + end + + # Validates the configuration classes of this instance and raises an + # exception if they are invalid. If you are implementing a custom configuration + # class, the method you want to implement is {Base#validate}. This is + # the method that checks all the validation, not one which defines + # validation rules. + def validate! + # Validate each of the configured classes and store the results into + # a hash. + errors = self.class.configures_list.inject({}) do |container, data| + key, _ = data + recorder = ErrorRecorder.new + send(key.to_sym).validate(recorder) + container[key.to_sym] = recorder if !recorder.errors.empty? + container + end + + return if errors.empty? + raise Errors::ConfigValidationFailed, :messages => Util::TemplateRenderer.render("config/validation_failed", :errors => errors) + end + end + end +end diff --git a/lib/vagrant/downloaders.rb b/lib/vagrant/downloaders.rb new file mode 100644 index 000000000..01f9b6833 --- /dev/null +++ b/lib/vagrant/downloaders.rb @@ -0,0 +1,7 @@ +module Vagrant + module Downloaders + autoload :Base, 'vagrant/downloaders/base' + autoload :File, 'vagrant/downloaders/file' + autoload :HTTP, 'vagrant/downloaders/http' + end +end diff --git a/lib/vagrant/hosts.rb b/lib/vagrant/hosts.rb new file mode 100644 index 000000000..5ed2ebdd9 --- /dev/null +++ b/lib/vagrant/hosts.rb @@ -0,0 +1,7 @@ +module Vagrant + module Hosts + autoload :Base, 'vagrant/hosts/base' + autoload :BSD, 'vagrant/hosts/bsd' + autoload :Linux, 'vagrant/hosts/linux' + end +end diff --git a/lib/vagrant/provisioners.rb b/lib/vagrant/provisioners.rb new file mode 100644 index 000000000..ccb53a43b --- /dev/null +++ b/lib/vagrant/provisioners.rb @@ -0,0 +1,8 @@ +# These aren't autoloaded because they have to register things such +# as configuration classes right away with Vagrant. +require 'vagrant/provisioners/base' +require 'vagrant/provisioners/chef' +require 'vagrant/provisioners/chef_server' +require 'vagrant/provisioners/chef_solo' +require 'vagrant/provisioners/puppet' +require 'vagrant/provisioners/puppet_server' diff --git a/lib/vagrant/systems.rb b/lib/vagrant/systems.rb new file mode 100644 index 000000000..1326d221a --- /dev/null +++ b/lib/vagrant/systems.rb @@ -0,0 +1,5 @@ +# These can't be autoloaded because they have to register functionality +# with Vagrant core. +require 'vagrant/systems/base' +require 'vagrant/systems/linux' +require 'vagrant/systems/solaris' diff --git a/lib/vagrant/util/glob_loader.rb b/lib/vagrant/util/glob_loader.rb deleted file mode 100644 index 48cc34e19..000000000 --- a/lib/vagrant/util/glob_loader.rb +++ /dev/null @@ -1,24 +0,0 @@ -module Vagrant - module Util - # Eases the processes of loading specific files then globbing - # the rest from a specified directory. - module GlobLoader - # Glob requires all ruby files in a directory, optionally loading select - # files initially (since others may depend on them). - # - # @param [String] dir The directory to glob - # @param [Array] initial_files Initial files (relative to `dir`) - # to load - def self.glob_require(dir, initial_files=[]) - initial_files.each do |file| - require File.expand_path(file, dir) - end - - # Glob require the rest - Dir[File.join(dir, "**", "*.rb")].each do |f| - require File.expand_path(f) - end - end - end - end -end