From 6561fea2960041e7fdd71ac9ceec339fab2b2d69 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sun, 25 May 2014 21:06:25 -0700 Subject: [PATCH] Change the auto activated extension feature to allow specifying different lifecycle events to activate at, allowing more extensions to auto activate. --- .../lib/middleman-core/application.rb | 8 ++-- .../lib/middleman-core/core_extensions.rb | 48 +++++++++---------- .../core_extensions/extensions.rb | 2 +- .../lib/middleman-core/extensions.rb | 26 +++++++--- .../lib/middleman-core/meta_pages.rb | 2 +- 5 files changed, 48 insertions(+), 38 deletions(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index d30e1625..64ce952e 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -179,11 +179,9 @@ module Middleman # Setup the default values from calls to set before initialization self.class.config.load_settings(self.class.superclass.config.all_settings) - # Parse YAML from templates. Must be before sitemap so sitemap - # extensions see updated frontmatter! - activate :front_matter - activate :data - activate :file_watcher + ::Middleman::Extensions.auto_activate[:before_sitemap].each do |ext_name| + activate ext_name + end # Initialize the Sitemap @sitemap = ::Middleman::Sitemap::Store.new(self) diff --git a/middleman-core/lib/middleman-core/core_extensions.rb b/middleman-core/lib/middleman-core/core_extensions.rb index 1e3e738a..729ebdc3 100644 --- a/middleman-core/lib/middleman-core/core_extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions.rb @@ -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 -Middleman::Extensions.register :front_matter do +Middleman::Extensions.register :front_matter, auto_activate: :before_sitemap do require 'middleman-core/core_extensions/front_matter' Middleman::CoreExtensions::FrontMatter 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 -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' Middleman::CoreExtensions::ExternalHelpers end @@ -27,7 +27,7 @@ end require 'middleman-core/core_extensions/rendering' # 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' Middleman::CoreExtensions::DefaultHelpers end @@ -35,11 +35,18 @@ end # Compass framework begin 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 # Compass is not available, don't complain about it 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 ### @@ -102,13 +109,6 @@ Middleman::Extensions.register :directory_indexes do Middleman::Extensions::DirectoryIndexes 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 # and automatically adds width and height attributes to their HTML # elements. diff --git a/middleman-core/lib/middleman-core/core_extensions/extensions.rb b/middleman-core/lib/middleman-core/core_extensions/extensions.rb index ea91c8cf..b6efa1d7 100644 --- a/middleman-core/lib/middleman-core/core_extensions/extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions/extensions.rb @@ -86,7 +86,7 @@ module Middleman ::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 end diff --git a/middleman-core/lib/middleman-core/extensions.rb b/middleman-core/lib/middleman-core/extensions.rb index 34be436b..519493be 100644 --- a/middleman-core/lib/middleman-core/extensions.rb +++ b/middleman-core/lib/middleman-core/extensions.rb @@ -1,5 +1,4 @@ require 'middleman-core/extension' -require 'set' module Middleman # 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`. module Extensions @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 # @api private @@ -18,8 +22,10 @@ module Middleman 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 + # A list of extensions that should be automatically loaded at different points in the application startup lifecycle. + # 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 # 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 [Class] 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. + # @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. # 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 @@ -66,7 +71,7 @@ module Middleman 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] + @auto_activate[options[:auto_activate]] << name if options[:auto_activate] end # @api private @@ -96,6 +101,13 @@ module Middleman extension_class end + + # @api private + # A flattened list of all extensions which are automatically activated + # @return [Array] A list of extension names which are automatically activated. + def auto_activated + @auto_activate.values.flatten + end end end end diff --git a/middleman-core/lib/middleman-core/meta_pages.rb b/middleman-core/lib/middleman-core/meta_pages.rb index 5783670b..c21f25d7 100644 --- a/middleman-core/lib/middleman-core/meta_pages.rb +++ b/middleman-core/lib/middleman-core/meta_pages.rb @@ -63,7 +63,7 @@ module Middleman extension_config = {} @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) # Multiple instance extension