Allow extensions to optionally register to be automatically activated before configuration.

This commit is contained in:
Ben Hollis 2014-05-22 23:05:42 -07:00
parent f89a76747e
commit 5d4cae2a06
3 changed files with 26 additions and 4 deletions

View file

@ -86,6 +86,10 @@ module Middleman
::Middleman::Extension.clear_after_extension_callbacks
::Middleman::Extensions.auto_activate_before_configuration.each do |ext_name|
activate ext_name
end
if ENV['AUTOLOAD_SPROCKETS'] != 'false'
begin
require 'middleman-sprockets'

View file

@ -1,4 +1,5 @@
require 'middleman-core/extension'
require 'set'
module Middleman
# The Extensions module is used to handle global registration and loading of Middleman Extensions.
@ -7,13 +8,19 @@ module Middleman
# `middleman-core/core_extensions/extensions.rb`.
module Extensions
@registered = {}
@auto_activate_before_configuration = Set.new
class << self
# @api private
# A hash of all registered extensions. Registered extensions are not necessarily active - this
# is the set of all extensions that are known to Middleman.
# @return [Hash{Symbol => Class<Middleman::Extension>, Proc}] A directory of known extensions indexed by the name they were registered under. The value may be a Proc, which can be lazily called to return an extension class.
attr_reader :registered
# @api private
# A list of extensions that should be automatically loaded before `config.rb` is loaded and before the `:before_configuration` hook is run. Only internal, built-in Middleman extensions should be listed here.
attr_reader :auto_activate_before_configuration
# Register a new extension. Choose a name which will be
# used to activate the extension in `config.rb`, like this:
#
@ -32,16 +39,25 @@ module Middleman
#
# @param [Symbol] name The name of the extension
# @param [Class<Middleman::Extension>] extension_class The extension class (Must inherit from {Middleman::Extension})
# @option options [Boolean] :auto_activate_before_configuration If set to true, this extension will be automatically
# activated before `config.rb` is loaded and before the `:before_configuration` hook is run.
# This is intended for use with built-in Middleman extensions and should not be used by third-party extensions.
# @yield Instead of passing a module in namespace, you can provide
# a block which returns your extension class. This gives
# you the ability to require other files only when the
# extension is first activated.
# @return [void]
def register(name, extension_class=nil, &block)
def register(name, extension_class=nil, options={}, &block)
raise 'Extension name must be a symbol' unless name.is_a?(Symbol)
# If we've already got an extension registered under this name, bail out
raise "There is already an extension registered with the name '#{name}'" if registered.key?(name)
# If the extension is defined with a block, grab options out of the "extension_class" parameter.
if extension_class && block_given? && options.empty? && extension_class.is_a?(Hash)
options = extension_class
extension_class = nil
end
registered[name] = if block_given?
block
elsif extension_class && extension_class.ancestors.include?(::Middleman::Extension)
@ -49,6 +65,8 @@ module Middleman
else
raise 'You must provide a Middleman::Extension or a block that returns a Middleman::Extension'
end
@auto_activate_before_configuration << name if options[:auto_activate_before_configuration]
end
# @api private

View file

@ -63,6 +63,8 @@ module Middleman
extension_config = {}
@middleman.inst.extensions.each do |ext_name, extension|
next if ::Middleman::Extension.auto_activate_before_configuration.include? ext_name
if extension.is_a?(Hash)
# Multiple instance extension
if extension.size == 1
@ -72,10 +74,8 @@ module Middleman
extension_config["#{ext_name} (#{inst})"] = extension_options(ext)
end
end
elsif extension.is_a?(::Middleman::Extension)
extension_config[ext_name] = extension_options(extension)
else
extension_config[ext_name] = nil
extension_config[ext_name] = extension_options(extension)
end
end