Allow extensions to optionally register to be automatically activated before configuration.
This commit is contained in:
parent
f89a76747e
commit
5d4cae2a06
|
@ -86,6 +86,10 @@ module Middleman
|
||||||
|
|
||||||
::Middleman::Extension.clear_after_extension_callbacks
|
::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'
|
if ENV['AUTOLOAD_SPROCKETS'] != 'false'
|
||||||
begin
|
begin
|
||||||
require 'middleman-sprockets'
|
require 'middleman-sprockets'
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require 'middleman-core/extension'
|
require 'middleman-core/extension'
|
||||||
|
require 'set'
|
||||||
|
|
||||||
module Middleman
|
module Middleman
|
||||||
# The Extensions module is used to handle global registration and loading of Middleman Extensions.
|
# 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`.
|
# `middleman-core/core_extensions/extensions.rb`.
|
||||||
module Extensions
|
module Extensions
|
||||||
@registered = {}
|
@registered = {}
|
||||||
|
@auto_activate_before_configuration = Set.new
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
# @api private
|
||||||
# A hash of all registered extensions. Registered extensions are not necessarily active - this
|
# A hash of all registered extensions. Registered extensions are not necessarily active - this
|
||||||
# is the set of all extensions that are known to Middleman.
|
# 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.
|
# @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
|
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
|
# Register a new extension. Choose a name which will be
|
||||||
# used to activate the extension in `config.rb`, like this:
|
# 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 [Symbol] name The name of the extension
|
||||||
# @param [Class<Middleman::Extension>] extension_class The extension class (Must inherit from {Middleman::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
|
# @yield Instead of passing a module in namespace, you can provide
|
||||||
# a block which returns your extension class. This gives
|
# a block which returns your extension class. This gives
|
||||||
# you the ability to require other files only when the
|
# you the ability to require other files only when the
|
||||||
# extension is first activated.
|
# extension is first activated.
|
||||||
# @return [void]
|
# @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)
|
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
|
# 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)
|
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?
|
registered[name] = if block_given?
|
||||||
block
|
block
|
||||||
elsif extension_class && extension_class.ancestors.include?(::Middleman::Extension)
|
elsif extension_class && extension_class.ancestors.include?(::Middleman::Extension)
|
||||||
|
@ -49,6 +65,8 @@ module Middleman
|
||||||
else
|
else
|
||||||
raise 'You must provide a Middleman::Extension or a block that returns a Middleman::Extension'
|
raise 'You must provide a Middleman::Extension or a block that returns a Middleman::Extension'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@auto_activate_before_configuration << name if options[:auto_activate_before_configuration]
|
||||||
end
|
end
|
||||||
|
|
||||||
# @api private
|
# @api private
|
||||||
|
|
|
@ -63,6 +63,8 @@ module Middleman
|
||||||
extension_config = {}
|
extension_config = {}
|
||||||
|
|
||||||
@middleman.inst.extensions.each do |ext_name, extension|
|
@middleman.inst.extensions.each do |ext_name, extension|
|
||||||
|
next if ::Middleman::Extension.auto_activate_before_configuration.include? ext_name
|
||||||
|
|
||||||
if extension.is_a?(Hash)
|
if extension.is_a?(Hash)
|
||||||
# Multiple instance extension
|
# Multiple instance extension
|
||||||
if extension.size == 1
|
if extension.size == 1
|
||||||
|
@ -72,10 +74,8 @@ module Middleman
|
||||||
extension_config["#{ext_name} (#{inst})"] = extension_options(ext)
|
extension_config["#{ext_name} (#{inst})"] = extension_options(ext)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elsif extension.is_a?(::Middleman::Extension)
|
|
||||||
extension_config[ext_name] = extension_options(extension)
|
|
||||||
else
|
else
|
||||||
extension_config[ext_name] = nil
|
extension_config[ext_name] = extension_options(extension)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue