action_hooks can hook into specific actions

This commit is contained in:
Mitchell Hashimoto 2013-02-22 12:12:55 -08:00
parent 18524628b7
commit be01d57034
6 changed files with 67 additions and 9 deletions

View File

@ -29,7 +29,7 @@ module Vagrant
environment.merge!(options || {}) environment.merge!(options || {})
# Setup the action hooks # Setup the action hooks
hooks = Vagrant.plugin("2").manager.action_hooks hooks = Vagrant.plugin("2").manager.action_hooks(environment[:action_name])
if !hooks.empty? if !hooks.empty?
@logger.info("Preparing hooks for middleware sequence...") @logger.info("Preparing hooks for middleware sequence...")
environment[:action_hooks] = hooks.map do |hook_proc| environment[:action_hooks] = hooks.map do |hook_proc|

View File

@ -8,7 +8,7 @@ module Vagrant
class Components class Components
# This contains all the action hooks. # This contains all the action hooks.
# #
# @return [Array<Proc>] # @return [Hash<Symbol, Array>]
attr_reader :action_hooks attr_reader :action_hooks
# This contains all the configuration plugins by scope. # This contains all the configuration plugins by scope.
@ -17,7 +17,8 @@ module Vagrant
attr_reader :configs attr_reader :configs
def initialize def initialize
@action_hooks = [] # The action hooks hash defaults to []
@action_hooks = Hash.new { |h, k| h[k] = [] }
# Create the configs hash which defaults to a registry # Create the configs hash which defaults to a registry
@configs = Hash.new { |h, k| h[k] = Registry.new } @configs = Hash.new { |h, k| h[k] = Registry.new }

View File

@ -17,11 +17,12 @@ module Vagrant
# This returns all the action hooks. # This returns all the action hooks.
# #
# @return [Array] # @return [Array]
def action_hooks def action_hooks(hook_name)
result = [] result = []
@registered.each do |plugin| @registered.each do |plugin|
result += plugin.components.action_hooks result += plugin.components.action_hooks[Plugin::ALL_ACTIONS]
result += plugin.components.action_hooks[hook_name]
end end
result result

View File

@ -66,11 +66,14 @@ module Vagrant
# bootup, VM provisioning, etc. # bootup, VM provisioning, etc.
# #
# @param [String] name Name of the action. # @param [String] name Name of the action.
# @param [Symbol] hook_name The location to hook. If this isn't
# set, every middleware action is hooked.
# @return [Array] List of the hooks for the given action. # @return [Array] List of the hooks for the given action.
def self.action_hook(name, &block) def self.action_hook(name, hook_name=nil, &block)
# The name is currently not used but we want it for the future. # The name is currently not used but we want it for the future.
components.action_hooks << block hook_name ||= ALL_ACTIONS
components.action_hooks[hook_name] << block
end end
# Defines additional command line commands available by key. The key # Defines additional command line commands available by key. The key

View File

@ -11,6 +11,47 @@ describe Vagrant::Plugin::V2::Manager do
p p
end end
describe "#action_hooks" do
it "should contain globally registered hooks" do
pA = plugin do |p|
p.action_hook("foo") { "bar" }
end
pB = plugin do |p|
p.action_hook("bar") { "baz" }
end
instance.register(pA)
instance.register(pB)
result = instance.action_hooks(nil)
result.length.should == 2
result[0].call.should == "bar"
result[1].call.should == "baz"
end
it "should contain specific hooks with globally registered hooks" do
pA = plugin do |p|
p.action_hook("foo") { "bar" }
p.action_hook("foo", :foo) { "bar_foo" }
p.action_hook("foo", :bar) { "bar_bar" }
end
pB = plugin do |p|
p.action_hook("bar") { "baz" }
end
instance.register(pA)
instance.register(pB)
result = instance.action_hooks(:foo)
result.length.should == 3
result[0].call.should == "bar"
result[1].call.should == "bar_foo"
result[2].call.should == "baz"
end
end
it "should enumerate registered communicator classes" do it "should enumerate registered communicator classes" do
pA = plugin do |p| pA = plugin do |p|
p.communicator("foo") { "bar" } p.communicator("foo") { "bar" }

View File

@ -24,12 +24,24 @@ describe Vagrant::Plugin::V2::Plugin do
end end
describe "action hooks" do describe "action hooks" do
it "should register action hooks" do it "should register on all actions by default" do
plugin = Class.new(described_class) do plugin = Class.new(described_class) do
action_hook("foo") { "bar" } action_hook("foo") { "bar" }
end end
hooks = plugin.components.action_hooks hooks_registry = plugin.components.action_hooks
hooks = hooks_registry[described_class.const_get("ALL_ACTIONS")]
hooks.length.should == 1
hooks[0].call.should == "bar"
end
it "should register for a specific action by default" do
plugin = Class.new(described_class) do
action_hook("foo", :bar) { "bar" }
end
hooks_registry = plugin.components.action_hooks
hooks = hooks_registry[:bar]
hooks.length.should == 1 hooks.length.should == 1
hooks[0].call.should == "bar" hooks[0].call.should == "bar"
end end