Clean up extensions a bit. Removes newest form of registering extensions, more consistently sets and uses an extension's ext_name, and makes a lot of things errors instead of just log messages in hopes that people can't get too far with a messed-up config.

This commit is contained in:
Ben Hollis 2014-03-29 14:29:42 -07:00
parent 10eca91311
commit a6c37f3dd3
11 changed files with 107 additions and 75 deletions

View file

@ -11,6 +11,6 @@ class ExtensionA < ::Middleman::Extension
option :hola, '', '' option :hola, '', ''
end end
ExtensionA.register Middleman::Extensions.register :extension_a, ExtensionA
activate :extension_a, :hello => "world", :hola => "mundo" activate :extension_a, :hello => "world", :hola => "mundo"

View file

@ -16,7 +16,7 @@ class ExtensionOne < ::Middleman::Extension
end end
end end
ExtensionOne.register Middleman::Extensions.register :extension_one, ExtensionOne
class ExtensionTwo < ::Middleman::Extension class ExtensionTwo < ::Middleman::Extension
helpers do helpers do
@ -36,7 +36,7 @@ class ExtensionTwo < ::Middleman::Extension
end end
end end
ExtensionTwo.register Middleman::Extensions.register :extension_two, ExtensionTwo
activate :extension_one activate :extension_one
activate :extension_two activate :extension_two

View file

@ -196,8 +196,10 @@ module Middleman
activate :default_helpers activate :default_helpers
activate :lorem activate :lorem
if defined?(Middleman::CoreExtensions::Compass) begin
activate :compass activate :compass
rescue LoadError
# Compass is not available, don't complain about it
end end
# Evaluate a passed block if given # Evaluate a passed block if given

View file

@ -27,68 +27,97 @@ require 'middleman-core/core_extensions/routing'
require 'middleman-core/core_extensions/show_exceptions' require 'middleman-core/core_extensions/show_exceptions'
# Setup default helpers # Setup default helpers
require 'middleman-core/core_extensions/default_helpers' Middleman::Extensions.register :default_helpers do
require 'middleman-core/core_extensions/default_helpers'
require 'middleman-core/core_extensions/i18n' Middleman::CoreExtensions::DefaultHelpers
end
# Compass framework # Compass framework
begin Middleman::Extensions.register :compass do
require 'middleman-core/core_extensions/compass' require 'middleman-core/core_extensions/compass'
rescue LoadError Middleman::CoreExtensions::Compass
end end
### ###
# Setup Optional Extensions # Setup Optional Extensions
### ###
Middleman::Extensions.register :i18n do
require 'middleman-core/core_extensions/i18n'
Middleman::CoreExtensions::Internationalization
end
# CacheBuster adds a query string to assets in dynamic templates to # CacheBuster adds a query string to assets in dynamic templates to
# avoid browser caches failing to update to your new content. # avoid browser caches failing to update to your new content.
require 'middleman-core/extensions/cache_buster' Middleman::Extensions.register :cache_buster do
Middleman::Extensions::CacheBuster.register require 'middleman-core/extensions/cache_buster'
Middleman::Extensions::CacheBuster
end
# RelativeAssets allow any asset path in dynamic templates to be either # RelativeAssets allow any asset path in dynamic templates to be either
# relative to the root of the project or use an absolute URL. # relative to the root of the project or use an absolute URL.
require 'middleman-core/extensions/relative_assets' Middleman::Extensions.register :relative_assets do
Middleman::Extensions::RelativeAssets.register require 'middleman-core/extensions/relative_assets'
Middleman::Extensions::RelativeAssets
end
# AssetHost allows you to setup multiple domains to host your static # AssetHost allows you to setup multiple domains to host your static
# assets. Calls to asset paths in dynamic templates will then rotate # assets. Calls to asset paths in dynamic templates will then rotate
# through each of the asset servers to better spread the load. # through each of the asset servers to better spread the load.
require 'middleman-core/extensions/asset_host' Middleman::Extensions.register :asset_host do
Middleman::Extensions::AssetHost.register require 'middleman-core/extensions/asset_host'
Middleman::Extensions::AssetHost
end
# MinifyCss compresses CSS # MinifyCss compresses CSS
require 'middleman-core/extensions/minify_css' Middleman::Extensions.register :minify_css do
Middleman::Extensions::MinifyCss.register require 'middleman-core/extensions/minify_css'
Middleman::Extensions::MinifyCss
end
# MinifyJavascript compresses JS # MinifyJavascript compresses JS
require 'middleman-core/extensions/minify_javascript' Middleman::Extensions.register :minify_javascript do
Middleman::Extensions::MinifyJavascript.register require 'middleman-core/extensions/minify_javascript'
Middleman::Extensions::MinifyJavascript
end
# GZIP assets and pages during build # GZIP assets and pages during build
require 'middleman-core/extensions/gzip' Middleman::Extensions.register :gzip do
Middleman::Extensions::Gzip.register require 'middleman-core/extensions/gzip'
Middleman::Extensions::Gzip
end
# AssetHash appends a hash of the file contents to the assets filename # AssetHash appends a hash of the file contents to the assets filename
# to avoid browser caches failing to update to your new content. # to avoid browser caches failing to update to your new content.
require 'middleman-core/extensions/asset_hash' Middleman::Extensions.register :asset_hash do
Middleman::Extensions::AssetHash.register require 'middleman-core/extensions/asset_hash'
Middleman::Extensions::AssetHash
end
# Provide Apache-style index.html files for directories # Provide Apache-style index.html files for directories
require 'middleman-core/extensions/directory_indexes' Middleman::Extensions.register :directory_indexes do
Middleman::Extensions::DirectoryIndexes.register require 'middleman-core/extensions/directory_indexes'
Middleman::Extensions::DirectoryIndexes
end
# Lorem provides a handful of helpful prototyping methods to generate # Lorem provides a handful of helpful prototyping methods to generate
# words, paragraphs, fake images, names and email addresses. # words, paragraphs, fake images, names and email addresses.
require 'middleman-core/extensions/lorem' Middleman::Extensions.register :lorem do
require 'middleman-core/extensions/lorem'
Middleman::Extensions::Lorem
end
# AutomaticImageSizes inspects the images used in your dynamic templates # AutomaticImageSizes inspects the images used in your dynamic templates
# and automatically adds width and height attributes to their HTML # and automatically adds width and height attributes to their HTML
# elements. # elements.
require 'middleman-core/extensions/automatic_image_sizes' Middleman::Extensions.register :automatic_image_sizes do
Middleman::Extensions::AutomaticImageSizes.register require 'middleman-core/extensions/automatic_image_sizes'
Middleman::Extensions::AutomaticImageSizes
end
# AutomaticAltTags uses the file name of the `image_tag` to generate # AutomaticAltTags uses the file name of the `image_tag` to generate
# a default `:alt` value. # a default `:alt` value.
require 'middleman-core/extensions/automatic_alt_tags' Middleman::Extensions.register :automatic_alt_tags do
Middleman::Extensions::AutomaticAltTags.register require 'middleman-core/extensions/automatic_alt_tags'
Middleman::Extensions::AutomaticAltTags
end

View file

@ -73,5 +73,3 @@ class Middleman::CoreExtensions::Compass < ::Middleman::Extension
end end
end end
end end
Middleman::CoreExtensions::Compass.register

View file

@ -259,5 +259,3 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
end end
end end
end end
Middleman::CoreExtensions::DefaultHelpers.register

View file

@ -88,8 +88,7 @@ module Middleman
# @param [Symbol, Module] ext Which extension to activate # @param [Symbol, Module] ext Which extension to activate
# @return [void] # @return [void]
def activate(ext, options={}, &block) def activate(ext, options={}, &block)
if extension = ::Middleman::Extensions.load(ext) extension = ::Middleman::Extensions.load(ext)
if extension.ancestors.include?(::Middleman::Extension)
logger.debug "== Activating: #{ext}" logger.debug "== Activating: #{ext}"
if extension.supports_multiple_instances? if extension.supports_multiple_instances?
@ -98,17 +97,11 @@ module Middleman
extensions[ext][key] = extension.new(self.class, options, &block) extensions[ext][key] = extension.new(self.class, options, &block)
else else
if extensions[ext] if extensions[ext]
logger.error "== #{ext} already activated." raise "#{ext} has already been activated and cannot be re-activated."
else else
extensions[ext] = extension.new(self.class, options, &block) extensions[ext] = extension.new(self.class, options, &block)
end end
end end
else
logger.error "!! Tried to activate old-style extension: #{ext}"
end
else
logger.error "!! Unknown Extension: #{ext}"
end
end end
# Access activated extensions # Access activated extensions

View file

@ -201,5 +201,3 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
end end
end end
end end
Middleman::CoreExtensions::Internationalization.register(:i18n)

View file

@ -33,14 +33,6 @@ module Middleman
self.defined_helpers += m self.defined_helpers += m
end end
def extension_name
self.ext_name || self.name.underscore.split('/').last.to_sym
end
def register(n=self.extension_name)
::Middleman::Extensions.register(n, self)
end
def activate def activate
new(::Middleman::Application) new(::Middleman::Application)
end end
@ -56,7 +48,7 @@ module Middleman
end end
def activated_extension(instance) def activated_extension(instance)
name = instance.class.extension_name name = instance.class.ext_name
return unless @_extension_activation_callbacks && @_extension_activation_callbacks[name] return unless @_extension_activation_callbacks && @_extension_activation_callbacks[name]
@_extension_activation_callbacks[name].each do |block| @_extension_activation_callbacks[name].each do |block|
block.arity == 1 ? block.call(instance) : block.call() block.arity == 1 ? block.call(instance) : block.call()
@ -136,7 +128,7 @@ module Middleman
end end
if ext.respond_to?(:manipulate_resource_list) if ext.respond_to?(:manipulate_resource_list)
ext.app.sitemap.register_resource_list_manipulator(ext.class.extension_name, ext) ext.app.sitemap.register_resource_list_manipulator(ext.class.ext_name, ext)
end end
end end
end end

View file

@ -13,7 +13,16 @@ module Middleman
# activate :my_extension # activate :my_extension
# #
# Provide your extension module either as the namespace # Provide your extension module either as the namespace
# parameter, or return it from the block: # parameter:
#
# Middleman::Extensions.register(:my_extension, MyExtension)
#
# Or return it from a block:
#
# Middleman::Extensions.register(:my_extension) do
# require 'my_extension'
# MyExtension
# end
# #
# @param [Symbol] name The name of the extension # @param [Symbol] name The name of the extension
# @param [Module] namespace The extension module # @param [Module] namespace The extension module
@ -22,34 +31,49 @@ module Middleman
# you the ability to require other files only when the # you the ability to require other files only when the
# extension is activated. # extension is activated.
def register(name, namespace=nil, &block) def register(name, namespace=nil, &block)
# If we've already got a matching extension that passed the # If we've already got an extension registered under this name, bail out
# version check, bail out. if registered.has_key?(name.to_sym)
return if registered.has_key?(name.to_sym) && !registered[name.to_sym].is_a?(String) raise "There is already an extension registered with the name '#{name}'"
end
registered[name.to_sym] = if block_given? registered[name.to_sym] = if block_given?
block block
elsif namespace elsif namespace && namespace.ancestors.include?(::Middleman::Extension)
namespace namespace
else
raise "You must provide a Middleman::Extension or a block that returns a Middleman::Extension"
end end
end end
# Load an extension by name, evaluating block definition if necessary. # Load an extension by name, evaluating block definition if necessary.
def load(name) def load(name)
name = name.to_sym name = name.to_sym
return nil unless registered.has_key?(name)
unless registered.has_key?(name)
raise "Unknown Extension: #{name}. Check the name and make sure you have referenced the extension's gem in your Gemfile."
end
extension = registered[name] extension = registered[name]
if extension.is_a?(Proc) if extension.is_a?(Proc)
extension = extension.call() || nil extension = extension.call()
registered[name] = extension registered[name] = extension
end end
if !extension.ancestors.include?(::Middleman::Extension)
raise "Tried to activate old-style extension: #{name}. They are no longer supported."
end
# Set the extension's name to whatever it was registered as.
extension.ext_name = name
extension extension
end end
end end
end end
# Where to look in gems for extensions to auto-register # Where to look in gems for extensions to auto-register. Since most extensions are
# called out in a Gemfile, this is really only useful for template extensions that get
# used by "middleman init".
EXTENSION_FILE = File.join('lib', 'middleman_extension.rb') unless const_defined?(:EXTENSION_FILE) EXTENSION_FILE = File.join('lib', 'middleman_extension.rb') unless const_defined?(:EXTENSION_FILE)
class << self class << self

View file

@ -174,5 +174,3 @@ class Middleman::Extensions::Lorem < ::Middleman::Extension
end end
end end
end end
Middleman::Extensions::Lorem.register