diff --git a/lib/vagrant/plugin/v2/components.rb b/lib/vagrant/plugin/v2/components.rb index d5c2c4d62..c99d14967 100644 --- a/lib/vagrant/plugin/v2/components.rb +++ b/lib/vagrant/plugin/v2/components.rb @@ -11,6 +11,13 @@ module Vagrant # @return [Hash] attr_reader :action_hooks + # This contains all the command plugins by name, and returns + # the command class and options. The command class is wrapped + # in a Proc so that it can be lazy loaded. + # + # @return [Registry>] + attr_reader :commands + # This contains all the configuration plugins by scope. # # @return [Hash] @@ -51,6 +58,7 @@ module Vagrant # The action hooks hash defaults to [] @action_hooks = Hash.new { |h, k| h[k] = [] } + @commands = Registry.new @configs = Hash.new { |h, k| h[k] = Registry.new } @guests = Registry.new @guest_capabilities = Hash.new { |h, k| h[k] = Registry.new } diff --git a/lib/vagrant/plugin/v2/plugin.rb b/lib/vagrant/plugin/v2/plugin.rb index f2e82445f..d9a6bfdb0 100644 --- a/lib/vagrant/plugin/v2/plugin.rb +++ b/lib/vagrant/plugin/v2/plugin.rb @@ -81,21 +81,18 @@ module Vagrant # "vagrant foo" becomes available. # # @param [String] name Subcommand key. - def self.command(name=UNSET_VALUE, &block) - data[:command] ||= Registry.new - - if name != UNSET_VALUE - # Validate the name of the command - if name.to_s !~ /^[-a-z0-9]+$/i - raise InvalidCommandName, "Commands can only contain letters, numbers, and hyphens" - end - - # Register a new command class only if a name was given. - data[:command].register(name.to_sym, &block) + def self.command(name, **opts, &block) + # Validate the name of the command + if name.to_s !~ /^[-a-z0-9]+$/i + raise InvalidCommandName, "Commands can only contain letters, numbers, and hyphens" end - # Return the registry - data[:command] + # Register the command + components.commands.register(name.to_sym) do + [block, opts] + end + + nil end # Defines additional communicators to be available. Communicators diff --git a/test/unit/vagrant/plugin/v2/plugin_test.rb b/test/unit/vagrant/plugin/v2/plugin_test.rb index f6472e762..6522aa54e 100644 --- a/test/unit/vagrant/plugin/v2/plugin_test.rb +++ b/test/unit/vagrant/plugin/v2/plugin_test.rb @@ -53,7 +53,20 @@ describe Vagrant::Plugin::V2::Plugin do command("foo") { "bar" } end - plugin.command[:foo].should == "bar" + expect(plugin.components.commands.keys).to be_include(:foo) + expect(plugin.components.commands[:foo][0].call).to eql("bar") + end + + it "should register command classes with options" do + plugin = Class.new(described_class) do + command("foo", opt: :bar) { "bar" } + end + + expect(plugin.components.commands.keys).to be_include(:foo) + expect(plugin.components.commands[:foo][0].call).to eql("bar") + expect(plugin.components.commands[:foo][1]).to eql({ + opt: :bar, + }) end ["spaces bad", "sym^bols"].each do |bad| @@ -79,8 +92,8 @@ describe Vagrant::Plugin::V2::Plugin do # Now verify when we actually get the command key that # a proper error is raised. expect { - plugin.command[:foo] - }.to raise_error(StandardError) + plugin.components.commands[:foo][0].call + }.to raise_error(StandardError, "FAIL!") end end