core: Modify plugin interfaces and API for hosts
This commit is contained in:
parent
04a5e6bcd2
commit
5922241fcb
|
@ -26,6 +26,16 @@ module Vagrant
|
|||
# @return [Hash<Symbol, Registry>]
|
||||
attr_reader :guest_capabilities
|
||||
|
||||
# This contains all the hosts and their parents.
|
||||
#
|
||||
# @return [Registry<Symbol, Array<Class, Symbol>>]
|
||||
attr_reader :hosts
|
||||
|
||||
# This contains all the registered host capabilities.
|
||||
#
|
||||
# @return [Hash<Symbol, Registry>]
|
||||
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
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue