From d5fbf29ec02f82548fa8b2dee54402b718f69038 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 14 Sep 2010 23:10:51 -0600 Subject: [PATCH] Plugins. Documentation coming shortly. --- CHANGELOG.md | 4 ++++ lib/vagrant.rb | 4 +++- lib/vagrant/plugin.rb | 40 +++++++++++++++++++++++++++++++++++++ test/vagrant/plugin_test.rb | 9 +++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 lib/vagrant/plugin.rb create mode 100644 test/vagrant/plugin_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index c3851a5cc..8cd80624b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## 0.6.0 (unreleased) + - **Plugins** have landed. Plugins are simply gems which have a `vagrant_init.rb` + file somewhere in their load path. Please read the documentation on + vagrantup.com before attempting to create a plugin (which is very easy) + for more information on how it all works and also some guidelines. - `vagrant package` now takes a `--vagrantfile` option to specify a Vagrantfile to package. The `--include` approach for including a Vagrantfile no longer works (previously built boxes will continue to work). diff --git a/lib/vagrant.rb b/lib/vagrant.rb index ea2268090..e1a9ece92 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -14,6 +14,7 @@ module Vagrant autoload :Config, 'vagrant/config' autoload :DataStore, 'vagrant/data_store' autoload :Errors, 'vagrant/errors' + autoload :Plugin, 'vagrant/plugin' autoload :Util, 'vagrant/util' module Command @@ -48,5 +49,6 @@ Vagrant::Util::GlobLoader.glob_require(libdir, %w{ downloaders/base provisioners/base provisioners/chef systems/base hosts/base}) -# Initialize the built-in actions +# Initialize the built-in actions and load the plugins. Vagrant::Action.builtin! +Vagrant::Plugin.load! diff --git a/lib/vagrant/plugin.rb b/lib/vagrant/plugin.rb new file mode 100644 index 000000000..3a83426eb --- /dev/null +++ b/lib/vagrant/plugin.rb @@ -0,0 +1,40 @@ +require "rubygems" + +module Vagrant + class Plugin + # The array of loaded plugins. + @@plugins = [] + + attr_reader :gemspec + attr_reader :file + + # 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! + # Look for a vagrant_init.rb in all the gems, but only the + # latest version of the gems. + Gem.source_index.latest_specs.each do |spec| + file = Gem.searcher.matching_files(spec, "vagrant_init.rb").first + next if !file + + @@plugins << new(spec, file) + end + end + + # Returns the array of plugins which are currently loaded by + # Vagrant. + def self.plugins; @@plugins; end + + # Initializes a new plugin, given a Gemspec and the path to the + # gem's `vagrant_init.rb` file. This should never be called manually. + # Instead {load!} creates all the instances. + def initialize(spec, file) + @gemspec = spec + @file = file + + load file + end + end +end diff --git a/test/vagrant/plugin_test.rb b/test/vagrant/plugin_test.rb new file mode 100644 index 000000000..028e7279b --- /dev/null +++ b/test/vagrant/plugin_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +class PluginTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Plugin + end + + # This is a pretty tough class to test. TODO. +end