Save information about extensions in a discoverable way.

This allows us to display extension config information at /__middleman/config/, and also allows users to modify extension
settings after they're activated in config.rb with a statement like "extensions[:asset_hash].options.exts += ['.js']".
This commit is contained in:
Ben Hollis 2013-04-12 21:54:28 -07:00
parent 6bf4059f4a
commit 8001dab258

View file

@ -62,22 +62,12 @@ module Middleman
send("#{env}_config", &block) send("#{env}_config", &block)
end end
# Alias `extensions` to access registered extensions
#
# @return [Array<Module>]
def extensions
@extensions ||= []
end
# Register a new extension # Register a new extension
# #
# @param [Module] extension Extension modules to register # @param [Module] extension Extension modules to register
# @param [Hash] options Per-extension options hash # @param [Hash] options Per-extension options hash
# @return [void] # @return [void]
def register(extension, options={}, &block) def register(extension, options={}, &block)
@extensions ||= []
@extensions += [extension]
if extension.instance_of? Module if extension.instance_of? Module
extend extension extend extension
if extension.respond_to?(:registered) if extension.respond_to?(:registered)
@ -87,6 +77,7 @@ module Middleman
extension.registered(self, options, &block) extension.registered(self, options, &block)
end end
end end
extension
elsif extension.instance_of?(Class) && extension.ancestors.include?(::Middleman::Extension) elsif extension.instance_of?(Class) && extension.ancestors.include?(::Middleman::Extension)
extension.new(self, options, &block) extension.new(self, options, &block)
end end
@ -107,17 +98,24 @@ module Middleman
ext_module = if ext.is_a?(Module) ext_module = if ext.is_a?(Module)
ext ext
else else
::Middleman::Extensions.load(ext.to_sym) ::Middleman::Extensions.load(ext)
end end
if ext_module.nil? if ext_module.nil?
logger.error "== Unknown Extension: #{ext}" logger.error "== Unknown Extension: #{ext}"
else else
logger.debug "== Activating: #{ext}" logger.debug "== Activating: #{ext}"
self.class.register(ext_module, options, &block) extensions[ext] = self.class.register(ext_module, options, &block)
end end
end end
# Access activated extensions
#
# @return [Hash<Symbol,Middleman::Extension|Module>]
def extensions
@extensions ||= {}
end
# Load features before starting server # Load features before starting server
def initialize def initialize
super super
@ -149,7 +147,7 @@ module Middleman
run_hook :after_configuration run_hook :after_configuration
logger.debug "Loaded extensions:" logger.debug "Loaded extensions:"
self.class.extensions.each do |ext| self.extensions.each do |ext,_|
logger.debug "== Extension: #{ext}" logger.debug "== Extension: #{ext}"
end end
end end