Be gone glob loader! Move everything to autoload or explicit require.

This commit is contained in:
Mitchell Hashimoto 2011-01-07 01:12:16 -08:00
parent 9a158cf45a
commit 8d529931ef
14 changed files with 198 additions and 107 deletions

View File

@ -4,25 +4,22 @@ require 'i18n'
require 'virtualbox' require 'virtualbox'
module Vagrant module Vagrant
# TODO: Move more classes over to the autoload model. We'll autoload :Action, 'vagrant/action'
# start small, but slowly move everything over.
autoload :Box, 'vagrant/box' autoload :Box, 'vagrant/box'
autoload :BoxCollection, 'vagrant/box_collection' autoload :BoxCollection, 'vagrant/box_collection'
autoload :CLI, 'vagrant/cli' autoload :CLI, 'vagrant/cli'
autoload :Config, 'vagrant/config' autoload :Config, 'vagrant/config'
autoload :DataStore, 'vagrant/data_store' autoload :DataStore, 'vagrant/data_store'
autoload :Downloaders, 'vagrant/downloaders'
autoload :Environment, 'vagrant/environment'
autoload :Errors, 'vagrant/errors' autoload :Errors, 'vagrant/errors'
autoload :Hosts, 'vagrant/hosts'
autoload :Plugin, 'vagrant/plugin' autoload :Plugin, 'vagrant/plugin'
autoload :SSH, 'vagrant/ssh'
autoload :TestHelpers, 'vagrant/test_helpers' autoload :TestHelpers, 'vagrant/test_helpers'
autoload :UI, 'vagrant/ui'
autoload :Util, 'vagrant/util' autoload :Util, 'vagrant/util'
autoload :VM, 'vagrant/vm'
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
# The source root is the path to the root directory of # The source root is the path to the root directory of
# the Vagrant gem. # the Vagrant gem.
@ -34,13 +31,9 @@ end
# Default I18n to load the en locale # Default I18n to load the en locale
I18n.load_path << File.expand_path("templates/locales/en.yml", Vagrant.source_root) 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 # Load the things which must be loaded before anything else
# is not that day. Low hanging fruit for anyone wishing to do it. require 'vagrant/command'
libdir = File.expand_path("lib/vagrant", Vagrant.source_root) require 'vagrant/provisioners'
Vagrant::Util::GlobLoader.glob_require(libdir, %w{ require 'vagrant/systems'
downloaders/base provisioners/base provisioners/chef systems/base
hosts/base})
# Initialize the built-in actions and load the plugins.
Vagrant::Action.builtin! Vagrant::Action.builtin!
Vagrant::Plugin.load! Vagrant::Plugin.load!

View File

@ -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 module Vagrant
# Manages action running and registration. Every Vagrant environment # Manages action running and registration. Every Vagrant environment
# has an instance of {Action} to allow for running in the context of # 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. # Where `:name` is the name of the registered action.
# #
class Action class Action
autoload :Environment, 'vagrant/action/environment'
autoload :Warden, 'vagrant/action/warden'
include Util include Util
@@reported_interrupt = false @@reported_interrupt = false

11
lib/vagrant/action/box.rb Normal file
View File

@ -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

View File

@ -0,0 +1,7 @@
module Vagrant
class Action
module Env
autoload :Set, 'vagrant/action/env/set'
end
end
end

View File

@ -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

30
lib/vagrant/action/vm.rb Normal file
View File

@ -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

25
lib/vagrant/command.rb Normal file
View File

@ -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'

View File

@ -1,5 +1,13 @@
require 'vagrant/config/base' require 'vagrant/config/base'
require 'vagrant/config/error_recorder' 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 module Vagrant
# The config class is responsible for loading Vagrant configurations, which # The config class is responsible for loading Vagrant configurations, which
@ -112,69 +120,4 @@ module Vagrant
end end
end 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 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'

59
lib/vagrant/config/top.rb Normal file
View File

@ -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

View File

@ -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

7
lib/vagrant/hosts.rb Normal file
View File

@ -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

View File

@ -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'

5
lib/vagrant/systems.rb Normal file
View File

@ -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'

View File

@ -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<String>] 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