From 5922241fcba55c14f1e37cddcb01c5641f2fde9d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 7 Jan 2014 18:38:49 -0800 Subject: [PATCH] core: Modify plugin interfaces and API for hosts --- lib/vagrant/plugin/v2/components.rb | 12 ++++++++++ lib/vagrant/plugin/v2/manager.rb | 21 ++++++++++++++--- lib/vagrant/plugin/v2/plugin.rb | 26 +++++++++++++++------ test/unit/vagrant/plugin/v2/manager_test.rb | 23 +++++++++++++++--- test/unit/vagrant/plugin/v2/plugin_test.rb | 12 +++++++++- 5 files changed, 80 insertions(+), 14 deletions(-) diff --git a/lib/vagrant/plugin/v2/components.rb b/lib/vagrant/plugin/v2/components.rb index f66fdce43..d5c2c4d62 100644 --- a/lib/vagrant/plugin/v2/components.rb +++ b/lib/vagrant/plugin/v2/components.rb @@ -26,6 +26,16 @@ module Vagrant # @return [Hash] attr_reader :guest_capabilities + # This contains all the hosts and their parents. + # + # @return [Registry>] + attr_reader :hosts + + # This contains all the registered host capabilities. + # + # @return [Hash] + attr_reader :host_capabilities + # This contains all the provider plugins by name, and returns # the provider class and options. # @@ -44,6 +54,8 @@ module Vagrant @configs = Hash.new { |h, k| h[k] = Registry.new } @guests = Registry.new @guest_capabilities = Hash.new { |h, k| h[k] = Registry.new } + @hosts = Registry.new + @host_capabilities = Hash.new { |h, k| h[k] = Registry.new } @providers = Registry.new @synced_folders = Registry.new end diff --git a/lib/vagrant/plugin/v2/manager.rb b/lib/vagrant/plugin/v2/manager.rb index 748586428..c6f44db6c 100644 --- a/lib/vagrant/plugin/v2/manager.rb +++ b/lib/vagrant/plugin/v2/manager.rb @@ -88,18 +88,33 @@ module Vagrant results end - - # This returns all registered host classes. + # This returns all the registered guests. # # @return [Hash] def hosts Registry.new.tap do |result| @registered.each do |plugin| - result.merge!(plugin.host) + result.merge!(plugin.components.hosts) end end end + # This returns all the registered host capabilities. + # + # @return [Hash] + def host_capabilities + results = Hash.new { |h, k| h[k] = Registry.new } + + @registered.each do |plugin| + plugin.components.host_capabilities.each do |host, caps| + results[host].merge!(caps) + end + end + + results + end + + # This returns all registered providers. # # @return [Hash] diff --git a/lib/vagrant/plugin/v2/plugin.rb b/lib/vagrant/plugin/v2/plugin.rb index 6ffb860a1..f2e82445f 100644 --- a/lib/vagrant/plugin/v2/plugin.rb +++ b/lib/vagrant/plugin/v2/plugin.rb @@ -135,7 +135,7 @@ module Vagrant # # @param [String] name Name of the guest. # @param [String] parent Name of the parent guest (if any) - def self.guest(name=UNSET_VALUE, parent=nil, &block) + def self.guest(name, parent=nil, &block) components.guests.register(name.to_sym) do parent = parent.to_sym if parent @@ -160,14 +160,26 @@ module Vagrant # the given key. # # @param [String] name Name of the host. - def self.host(name=UNSET_VALUE, &block) - data[:hosts] ||= Registry.new + # @param [String] parent Name of the parent host (if any) + def self.host(name, parent=nil, &block) + components.hosts.register(name.to_sym) do + parent = parent.to_sym if parent - # Register a new host class only if a name was given - data[:hosts].register(name.to_sym, &block) if name != UNSET_VALUE + [block.call, parent] + end + nil + end - # Return the registry - data[:hosts] + # Defines a capability for the given host. The block should return + # a class/module that has a method with the capability name, ready + # to be executed. This means that if it is an instance method, + # the block should return an instance of the class. + # + # @param [String] host The name of the host + # @param [String] cap The name of the capability + def self.host_capability(host, cap, &block) + components.host_capabilities[host.to_sym].register(cap.to_sym, &block) + nil end # Registers additional providers to be available. diff --git a/test/unit/vagrant/plugin/v2/manager_test.rb b/test/unit/vagrant/plugin/v2/manager_test.rb index 67dc989e1..4e5c4fc35 100644 --- a/test/unit/vagrant/plugin/v2/manager_test.rb +++ b/test/unit/vagrant/plugin/v2/manager_test.rb @@ -126,15 +126,32 @@ describe Vagrant::Plugin::V2::Manager do end pB = plugin do |p| - p.host("bar") { "baz" } + p.host("bar", "foo") { "baz" } end instance.register(pA) instance.register(pB) instance.hosts.to_hash.length.should == 2 - instance.hosts[:foo].should == "bar" - instance.hosts[:bar].should == "baz" + instance.hosts[:foo].should == ["bar", nil] + instance.hosts[:bar].should == ["baz", :foo] + end + + it "should enumerate registered host capabilities" do + pA = plugin do |p| + p.host_capability("foo", "foo") { "bar" } + end + + pB = plugin do |p| + p.host_capability("bar", "foo") { "baz" } + end + + instance.register(pA) + instance.register(pB) + + instance.host_capabilities.length.should == 2 + instance.host_capabilities[:foo][:foo].should == "bar" + instance.host_capabilities[:bar][:foo].should == "baz" end it "should enumerate registered provider classes" do diff --git a/test/unit/vagrant/plugin/v2/plugin_test.rb b/test/unit/vagrant/plugin/v2/plugin_test.rb index 71597988c..f6472e762 100644 --- a/test/unit/vagrant/plugin/v2/plugin_test.rb +++ b/test/unit/vagrant/plugin/v2/plugin_test.rb @@ -192,7 +192,7 @@ describe Vagrant::Plugin::V2::Plugin do host("foo") { "bar" } end - plugin.host[:foo].should == "bar" + plugin.components.hosts[:foo].should == ["bar", nil] end it "should lazily register host classes" do @@ -214,6 +214,16 @@ describe Vagrant::Plugin::V2::Plugin do end end + describe "host capabilities" do + it "should register host capabilities" do + plugin = Class.new(described_class) do + host_capability("foo", "bar") { "baz" } + end + + plugin.components.host_capabilities[:foo][:bar].should == "baz" + end + end + describe "providers" do it "should register provider classes" do plugin = Class.new(described_class) do