Module: Pod::HooksManager

Defined in:
lib/cocoapods/hooks_manager.rb

Overview

Provides support for the hook system of CocoaPods. The system is designed especially for plugins. Interested clients can register to notifications by name.

The blocks, to prevent compatibility issues, will receive one and only one argument: a context object. This object should be simple storage of information (a typed hash). Notifications senders are responsible to indicate the class of the object associated with their notification name.

Context object should not remove attribute accessors to not break compatibility with the plugins (this promise will be honoured strictly from CocoaPods 1.0).

Defined Under Namespace

Classes: Hook

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.registrationsHash{Symbol => Array<Hook>} (readonly)

Returns The list of the hooks that are registered for each hook name.

Returns:

  • (Hash{Symbol => Array<Hook>})

    The list of the hooks that are registered for each hook name.



62
63
64
# File 'lib/cocoapods/hooks_manager.rb', line 62

def registrations
  @registrations
end

Class Method Details

.register(plugin_name, hook_name = nil, &block) ⇒ Object

Registers a block for the hook with the given name.

Parameters:

  • plugin_name (String)

    The name of the plugin the hook comes from.

  • hook_name (Symbol) (defaults to: nil)

    The name of the notification.

  • block (Proc)

    The block.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cocoapods/hooks_manager.rb', line 75

def register(plugin_name, hook_name = nil, &block)
  # TODO: Backwards compatibility with nameless plugins from CP 0.34
  if hook_name.nil?
    hook_name = plugin_name
    plugin_name = nil
  end

  @registrations ||= {}
  @registrations[hook_name] ||= []
  @registrations[hook_name] << Hook.new(hook_name, plugin_name, block)
end

.run(name, context, whitelisted_plugins = nil) ⇒ Object

Runs all the registered blocks for the hook with the given name.

Parameters:

  • name (Symbol)

    The name of the hook.

  • context (Object)

    The context object which should be passed to the blocks.

  • whitelisted_plugins (Hash<String, Hash>) (defaults to: nil)

    The plugins that should be run, in the form of a hash keyed by plugin name, where the values are the custom options that should be passed to the hook’s block if it supports taking a second argument.

Raises:

  • (ArgumentError)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cocoapods/hooks_manager.rb', line 101

def run(name, context, whitelisted_plugins = nil)
  raise ArgumentError, 'Missing name' unless name
  raise ArgumentError, 'Missing options' unless context

  if registrations
    hooks = registrations[name]
    if hooks
      UI.message "- Running #{name.to_s.gsub('_', ' ')} hooks" do
        hooks.each do |hook|
          next if whitelisted_plugins && !whitelisted_plugins.key?(hook.plugin_name)
          UI.message "- #{hook.plugin_name || 'unknown plugin'} from " \
                     "`#{hook.block.source_location.first}`" do
            block = hook.block
            if block.arity > 1
              block.call(context, whitelisted_plugins[hook.plugin_name])
            else
              block.call(context)
            end
          end
        end
      end
    end
  end
end