Change the auto activated extension feature to allow specifying different lifecycle events to activate at, allowing more extensions to auto activate.

This commit is contained in:
Ben Hollis 2014-05-25 21:06:25 -07:00
parent 70b3b87905
commit 6561fea296
5 changed files with 48 additions and 38 deletions

View file

@ -179,11 +179,9 @@ module Middleman
# Setup the default values from calls to set before initialization # Setup the default values from calls to set before initialization
self.class.config.load_settings(self.class.superclass.config.all_settings) self.class.config.load_settings(self.class.superclass.config.all_settings)
# Parse YAML from templates. Must be before sitemap so sitemap ::Middleman::Extensions.auto_activate[:before_sitemap].each do |ext_name|
# extensions see updated frontmatter! activate ext_name
activate :front_matter end
activate :data
activate :file_watcher
# Initialize the Sitemap # Initialize the Sitemap
@sitemap = ::Middleman::Sitemap::Store.new(self) @sitemap = ::Middleman::Sitemap::Store.new(self)

View file

@ -1,24 +1,24 @@
# File Change Notifier
Middleman::Extensions.register :file_watcher do
require 'middleman-core/core_extensions/file_watcher'
Middleman::CoreExtensions::FileWatcher
end
# Data looks at the data/ folder for YAML files and makes them available
# to dynamic requests.
Middleman::Extensions.register :data do
require 'middleman-core/core_extensions/data'
Middleman::CoreExtensions::Data
end
# Parse YAML from templates # Parse YAML from templates
Middleman::Extensions.register :front_matter do Middleman::Extensions.register :front_matter, auto_activate: :before_sitemap do
require 'middleman-core/core_extensions/front_matter' require 'middleman-core/core_extensions/front_matter'
Middleman::CoreExtensions::FrontMatter Middleman::CoreExtensions::FrontMatter
end end
# Data looks at the data/ folder for YAML files and makes them available
# to dynamic requests.
Middleman::Extensions.register :data, auto_activate: :before_sitemap do
require 'middleman-core/core_extensions/data'
Middleman::CoreExtensions::Data
end
# File Change Notifier
Middleman::Extensions.register :file_watcher, auto_activate: :before_sitemap do
require 'middleman-core/core_extensions/file_watcher'
Middleman::CoreExtensions::FileWatcher
end
# External helpers looks in the helpers/ folder for helper modules # External helpers looks in the helpers/ folder for helper modules
Middleman::Extensions.register :external_helpers, auto_activate_before_configuration: true do Middleman::Extensions.register :external_helpers, auto_activate: :before_configuration do
require 'middleman-core/core_extensions/external_helpers' require 'middleman-core/core_extensions/external_helpers'
Middleman::CoreExtensions::ExternalHelpers Middleman::CoreExtensions::ExternalHelpers
end end
@ -27,7 +27,7 @@ end
require 'middleman-core/core_extensions/rendering' require 'middleman-core/core_extensions/rendering'
# Setup default helpers # Setup default helpers
Middleman::Extensions.register :default_helpers, auto_activate_before_configuration: true do Middleman::Extensions.register :default_helpers, auto_activate: :before_configuration do
require 'middleman-core/core_extensions/default_helpers' require 'middleman-core/core_extensions/default_helpers'
Middleman::CoreExtensions::DefaultHelpers Middleman::CoreExtensions::DefaultHelpers
end end
@ -35,11 +35,18 @@ end
# Compass framework # Compass framework
begin begin
require 'middleman-core/core_extensions/compass' require 'middleman-core/core_extensions/compass'
Middleman::Extensions.register :compass, Middleman::CoreExtensions::Compass, auto_activate_before_configuration: true Middleman::Extensions.register :compass, Middleman::CoreExtensions::Compass, auto_activate: :before_configuration
rescue LoadError rescue LoadError
# Compass is not available, don't complain about it # Compass is not available, don't complain about it
end end
# Lorem provides a handful of helpful prototyping methods to generate
# words, paragraphs, fake images, names and email addresses.
Middleman::Extensions.register :lorem, auto_activate: :before_configuration do
require 'middleman-core/extensions/lorem'
Middleman::Extensions::Lorem
end
### ###
# Setup Optional Extensions # Setup Optional Extensions
### ###
@ -102,13 +109,6 @@ Middleman::Extensions.register :directory_indexes do
Middleman::Extensions::DirectoryIndexes Middleman::Extensions::DirectoryIndexes
end end
# Lorem provides a handful of helpful prototyping methods to generate
# words, paragraphs, fake images, names and email addresses.
Middleman::Extensions.register :lorem, auto_activate_before_configuration: true 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.

View file

@ -86,7 +86,7 @@ module Middleman
::Middleman::Extension.clear_after_extension_callbacks ::Middleman::Extension.clear_after_extension_callbacks
::Middleman::Extensions.auto_activate_before_configuration.each do |ext_name| ::Middleman::Extensions.auto_activate[:before_configuration].each do |ext_name|
activate ext_name activate ext_name
end end

View file

@ -1,5 +1,4 @@
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.
@ -8,7 +7,12 @@ 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 @auto_activate = {
# Activate before the Sitemap is instantiated
before_sitemap: [],
# Activate the extension before `config.rb` and the `:before_configuration` hook.
before_configuration: []
}
class << self class << self
# @api private # @api private
@ -18,8 +22,10 @@ module Middleman
attr_reader :registered attr_reader :registered
# @api private # @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. # A list of extensions that should be automatically loaded at different points in the application startup lifecycle.
attr_reader :auto_activate_before_configuration # Only internal, built-in Middleman extensions should be listed here.
# @return [Hash{Symbol => Symbol}] A hash from event name to extension name.
attr_reader :auto_activate
# 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:
@ -39,8 +45,7 @@ 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 # @option options [Boolean] :auto_activate If this is set to a lifecycle event (:before_configuration or :before_sitemap), this extension will be automatically activated at that point.
# 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. # 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
@ -66,7 +71,7 @@ module Middleman
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] @auto_activate[options[:auto_activate]] << name if options[:auto_activate]
end end
# @api private # @api private
@ -96,6 +101,13 @@ module Middleman
extension_class extension_class
end end
# @api private
# A flattened list of all extensions which are automatically activated
# @return [Array<Symbol>] A list of extension names which are automatically activated.
def auto_activated
@auto_activate.values.flatten
end
end end
end end
end end

View file

@ -63,7 +63,7 @@ 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 next if ::Middleman::Extension.auto_activated.include? ext_name
if extension.is_a?(Hash) if extension.is_a?(Hash)
# Multiple instance extension # Multiple instance extension