Vagrant.require_plugin [GH-916]

This commit is contained in:
Mitchell Hashimoto 2012-05-06 14:01:10 -07:00
parent d230f0a41c
commit bc0643613a
5 changed files with 39 additions and 1 deletions

View File

@ -7,7 +7,8 @@
not be _functional_ in future versions of Vagrant). not be _functional_ in future versions of Vagrant).
- Plugins no longer "autoload" by simply gem installing them. This increases - Plugins no longer "autoload" by simply gem installing them. This increases
Vagrant's initial startup time considerably. To replace this, you must now Vagrant's initial startup time considerably. To replace this, you must now
explicitly require plugins in the `~/.vagrantrc` file. explicitly require plugins in the `~/.vagrantrc` file, using
`Vagrant.require_plugin`.
- Improve the SSH "ready?" check. [GH-841] - Improve the SSH "ready?" check. [GH-841]
- Human friendly error if connection times out for HTTP downloads. [GH-849] - Human friendly error if connection times out for HTTP downloads. [GH-849]
- Detect when the VirtualBox installation is incomplete and error. [GH-846] - Detect when the VirtualBox installation is incomplete and error. [GH-846]

View File

@ -107,6 +107,22 @@ module Vagrant
# Raise an error that the plugin version is invalid # Raise an error that the plugin version is invalid
raise ArgumentError, "Invalid plugin version API: #{version}" raise ArgumentError, "Invalid plugin version API: #{version}"
end end
# This should be used instead of Ruby's built-in `require` in order to
# load a Vagrant plugin. This will load the given plugin by first doing
# a normal `require`, giving a nice error message if things go wrong,
# and second by verifying that a Vagrant plugin was actually defined in
# the process.
#
# @param [String] name Name of the plugin to load.
def self.require_plugin(name)
# Attempt the normal require
begin
require name
rescue LoadError
raise Errors::PluginLoadError, :plugin => name
end
end
end end
# # Default I18n to load the en locale # # Default I18n to load the en locale

View File

@ -298,6 +298,11 @@ module Vagrant
error_key(:dotfile_error, "vagrant.actions.vm.persist") error_key(:dotfile_error, "vagrant.actions.vm.persist")
end end
class PluginLoadError < VagrantError
status_code(81)
error_key(:plugin_load_error)
end
class SCPUnavailable < VagrantError class SCPUnavailable < VagrantError
status_code(56) status_code(56)
error_key(:scp_unavailable) error_key(:scp_unavailable)

View File

@ -83,6 +83,9 @@ en:
no_env: |- no_env: |-
A Vagrant environment is required to run this command. Run `vagrant init` A Vagrant environment is required to run this command. Run `vagrant init`
to set one up. to set one up.
plugin_load_error: |-
The plugin "%{plugin}" could not be found. Please make sure that it is
properly installed via `vagrant gem`.
port_collision_resume: |- port_collision_resume: |-
This VM cannot be resumed, because the forwarded ports would collide with This VM cannot be resumed, because the forwarded ports would collide with
another running virtual machine. Normally, Vagrant will attempt to fix this another running virtual machine. Normally, Vagrant will attempt to fix this

View File

@ -15,4 +15,17 @@ describe Vagrant do
to raise_error(ArgumentError) to raise_error(ArgumentError)
end end
end end
describe "requiring plugins" do
it "should require the plugin given" do
# For now, just require a stdlib
expect { described_class.require_plugin "set" }.
to_not raise_error
end
it "should raise an error if the file doesn't exist" do
expect { described_class.require_plugin("i_dont_exist") }.
to raise_error(Vagrant::Errors::PluginLoadError)
end
end
end end