Merge pull request #1285 from bhollis/extensions

Auto activating extensions
This commit is contained in:
Thomas Reynolds 2014-05-26 14:26:09 -07:00
commit 18884cac95
10 changed files with 152 additions and 144 deletions

View file

@ -25,6 +25,15 @@ require 'middleman-core/config_context'
require 'middleman-core/file_renderer'
require 'middleman-core/template_renderer'
# Rack Request
require 'middleman-core/core_extensions/request'
# Custom Extension API and config.rb handling
require 'middleman-core/core_extensions/extensions'
# Catch and show exceptions at the Rack level
require 'middleman-core/core_extensions/show_exceptions'
# Core Middleman Class
module Middleman
class Application
@ -133,21 +142,12 @@ module Middleman
# Handle exceptions
include Middleman::CoreExtensions::ShowExceptions
# Add Watcher Callbacks
include Middleman::CoreExtensions::FileWatcher
# Activate Data package
include Middleman::CoreExtensions::Data
# Setup custom rendering
include Middleman::CoreExtensions::Rendering
# Sitemap Config options and public api
include Middleman::Sitemap
# Setup external helpers
include Middleman::CoreExtensions::ExternalHelpers
# Reference to Logger singleton
def logger
::Middleman::Logger.singleton
@ -168,7 +168,7 @@ module Middleman
delegate :link_to, :image_tag, :asset_path, to: :generic_template_context
# Initialize the Middleman project
def initialize(&block)
def initialize
@template_context_class = Class.new(Middleman::TemplateContext)
@generic_template_context = @template_context_class.new(self)
@config_context = ConfigContext.new(self, @template_context_class)
@ -179,9 +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
::Middleman::Extensions.auto_activate[:before_sitemap].each do |ext_name|
activate ext_name
end
# Initialize the Sitemap
@sitemap = ::Middleman::Sitemap::Store.new(self)
@ -193,19 +193,6 @@ module Middleman
config[:source] = ENV['MM_SOURCE'] if ENV['MM_SOURCE']
# Built-in extensions
activate :default_helpers
activate :lorem
begin
activate :compass
rescue LoadError
# Compass is not available, don't complain about it
end
# Evaluate a passed block if given
@config_context.instance_exec(&block) if block_given?
super
end

View file

@ -1,3 +1,5 @@
require 'middleman-core/core_extensions/routing'
module Middleman
class ConfigContext
# page routing

View file

@ -1,44 +1,50 @@
# Rack Request
require 'middleman-core/core_extensions/request'
# File Change Notifier
require 'middleman-core/core_extensions/file_watcher'
# Custom Feature API
require 'middleman-core/core_extensions/extensions'
# Data looks at the data/ folder for YAML files and makes them available
# to dynamic requests.
require 'middleman-core/core_extensions/data'
# 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
require 'middleman-core/core_extensions/external_helpers'
Middleman::Extensions.register :external_helpers, auto_activate: :before_configuration do
require 'middleman-core/core_extensions/external_helpers'
Middleman::CoreExtensions::ExternalHelpers
end
# Extended version of Padrino's rendering
require 'middleman-core/core_extensions/rendering'
# Pass custom options to views
require 'middleman-core/core_extensions/routing'
# Catch and show exceptions at the Rack level
require 'middleman-core/core_extensions/show_exceptions'
# Setup default helpers
Middleman::Extensions.register :default_helpers do
Middleman::Extensions.register :default_helpers, auto_activate: :before_configuration do
require 'middleman-core/core_extensions/default_helpers'
Middleman::CoreExtensions::DefaultHelpers
end
# Compass framework
Middleman::Extensions.register :compass do
begin
require 'middleman-core/core_extensions/compass'
Middleman::CoreExtensions::Compass
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
###
@ -103,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 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.

View file

@ -1,37 +1,35 @@
require 'yaml'
require 'active_support/json'
module Middleman
module CoreExtensions
# The data extension parses YAML and JSON files in the data/ directory
# and makes them available to config.rb, templates and extensions
module Data
# Extension registered
class << self
# @private
def included(app)
# Data formats
require 'yaml'
require 'active_support/json'
# The data extension parses YAML and JSON files in the `data/` directory
# and makes them available to `config.rb`, templates and extensions
class Data < Extension
# The regex which tells Middleman which files are for data
MATCHER = /[\w-]+\.(yml|yaml|json)$/
app.config.define_setting :data_dir, 'data', 'The directory data files are stored in'
app.send :include, InstanceMethods
def initialize(app, options_hash={}, &block)
super
app.config.define_setting :data_dir, 'data', 'The directory data files are stored in'
# Directly include the #data method instead of using helpers so that this is available immediately
app.send :include, InstanceMethods
end
def before_configuration
# Setup data files before anything else so they are available when
# parsing config.rb
app.files.changed MATCHER do |file|
data.touch_file(file) if file.start_with?("#{config[:data_dir]}/")
end
app.files.deleted MATCHER do |file|
data.remove_file(file) if file.start_with?("#{config[:data_dir]}/")
end
end
# Instance methods
module InstanceMethods
# Setup data files before anything else so they are available when
# parsing config.rb
def initialize
files.changed DataStore.matcher do |file|
data.touch_file(file) if file.start_with?("#{config[:data_dir]}/")
end
files.deleted DataStore.matcher do |file|
data.remove_file(file) if file.start_with?("#{config[:data_dir]}/")
end
super
end
# The data object
#
# @return [DataStore]
@ -42,25 +40,14 @@ module Middleman
# The core logic behind the data extension.
class DataStore
# Static methods
class << self
# The regex which tells Middleman which files are for data
#
# @return [Regexp]
def matcher
%r{[\w-]+\.(yml|yaml|json)$}
end
end
# Store static data hash
#
# @param [Symbol] name Name of the data, used for namespacing
# @param [Hash] content The content for this data
# @return [Hash]
def store(name=nil, content=nil)
@_local_sources ||= {}
@_local_sources[name.to_s] = content unless name.nil? || content.nil?
@_local_sources
@local_sources[name.to_s] = content unless name.nil? || content.nil?
@local_sources
end
# Store callback-based data
@ -69,9 +56,8 @@ module Middleman
# @param [Proc] proc The callback which will return data
# @return [Hash]
def callbacks(name=nil, proc=nil)
@_callback_sources ||= {}
@_callback_sources[name.to_s] = proc unless name.nil? || proc.nil?
@_callback_sources
@callback_sources[name.to_s] = proc unless name.nil? || proc.nil?
@callback_sources
end
# Setup data store
@ -80,6 +66,8 @@ module Middleman
def initialize(app)
@app = app
@local_data = {}
@local_sources = {}
@callback_sources = {}
end
# Update the internal cache for a given file path

View file

@ -76,7 +76,7 @@ module Middleman
end
# Override application initialization to load `config.rb` and to call lifecycle hooks.
def initialize
def initialize(&block)
super
self.class.inst = self
@ -86,6 +86,10 @@ module Middleman
::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'
begin
require 'middleman-sprockets'
@ -95,6 +99,9 @@ module Middleman
end
end
# Evaluate a passed block if given
config_context.instance_exec(&block) if block_given?
run_hook :initialized
run_hook :before_configuration

View file

@ -1,9 +1,10 @@
# Load helpers in helpers/
module Middleman
module CoreExtensions
module ExternalHelpers
# once registered
def self.included(app)
# Load helpers in `helpers/`
class ExternalHelpers < Extension
def initialize(app, options_hash={}, &block)
super
# Setup a default helpers paths
app.config.define_setting :helpers_dir, 'helpers', 'Directory to autoload helper modules from'
app.config.define_setting :helpers_filename_glob, '**.rb', 'Glob pattern for matching helper ruby files'
@ -11,20 +12,20 @@ module Middleman
basename = File.basename(filename, File.extname(filename))
basename.camelcase
}, 'Proc implementing the conversion from helper filename to module name'
end
# After config
app.after_configuration do
helpers_path = File.join(root, config[:helpers_dir])
next unless File.exist?(helpers_path)
def after_configuration
helpers_path = File.join(app.root, app.config[:helpers_dir])
Dir[File.join(helpers_path, config[:helpers_filename_glob])].each do |filename|
module_name = config[:helpers_filename_to_module_name_proc].call(filename)
if File.exist?(helpers_path)
Dir[File.join(helpers_path, app.config[:helpers_filename_glob])].each do |filename|
module_name = app.config[:helpers_filename_to_module_name_proc].call(filename)
next unless module_name
require filename
next unless Object.const_defined?(module_name.to_sym)
@template_context_class.send :include, Object.const_get(module_name.to_sym)
app.template_context_class.send :include, Object.const_get(module_name.to_sym)
end
end
end

View file

@ -1,7 +1,11 @@
# API for watching file change events
require 'pathname'
require 'set'
module Middleman
module CoreExtensions
module FileWatcher
# API for watching file change events
class FileWatcher < Extension
# Regexes in this list will filter out filenames of files that shouldn't cause file change notifications.
IGNORE_LIST = [
/^bin(\/|$)/,
/^\.bundle(\/|$)/,
@ -20,39 +24,30 @@ module Middleman
/^tmp\//
]
# Setup extension
class << self
# Once registered
def included(app)
require 'pathname'
require 'set'
def initialize(app, options_hash={}, &block)
super
app.send :include, InstanceMethods
app.config.define_setting :file_watcher_ignore, IGNORE_LIST, 'Regexes for paths that should be ignored when they change.'
app.config.define_setting :file_watcher_ignore, IGNORE_LIST, 'Regexes for paths that should be ignored when they change.'
# Before parsing config, load the data/ directory
app.before_configuration do
files.reload_path(config[:data_dir])
end
app.after_configuration do
config[:file_watcher_ignore] << %r{^#{config[:build_dir]}(\/|$)}
end
# After config, load everything else
app.ready do
files.reload_path('.')
end
end
# Directly include the #files method instead of using helpers so that this is available immediately
app.send :include, InstanceMethods
end
# Before parsing config, load the data/ directory
def before_configuration
app.files.reload_path(app.config[:data_dir])
end
def after_configuration
app.config[:file_watcher_ignore] << %r{^#{app.config[:build_dir]}(\/|$)}
app.files.reload_path('.')
end
# Instance methods
module InstanceMethods
# Access the file api
# @return [Middleman::CoreExtensions::FileWatcher::API]
def files
@_files_api ||= API.new(self)
@files_api ||= API.new(self)
end
end

View file

@ -1,7 +1,6 @@
require 'middleman-core/template_context'
# Rendering extension
# rubocop:disable UnderscorePrefixedVariableName
module Middleman
module CoreExtensions
module Rendering

View file

@ -7,13 +7,26 @@ module Middleman
# `middleman-core/core_extensions/extensions.rb`.
module Extensions
@registered = {}
@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
# A hash of all registered extensions. Registered extensions are not necessarily active - this
# 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.
attr_reader :registered
# @api private
# 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:
#
@ -32,16 +45,24 @@ module Middleman
#
# @param [Symbol] name The name of the extension
# @param [Class<Middleman::Extension>] extension_class The extension class (Must inherit from {Middleman::Extension})
# @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
# you the ability to require other files only when the
# extension is first activated.
# @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)
# 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)
# 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?
block
elsif extension_class && extension_class.ancestors.include?(::Middleman::Extension)
@ -49,6 +70,8 @@ module Middleman
else
raise 'You must provide a Middleman::Extension or a block that returns a Middleman::Extension'
end
@auto_activate[options[:auto_activate]] << name if options[:auto_activate]
end
# @api private
@ -78,6 +101,13 @@ module Middleman
extension_class
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

View file

@ -63,6 +63,8 @@ module Middleman
extension_config = {}
@middleman.inst.extensions.each do |ext_name, extension|
next if ::Middleman::Extension.auto_activated.include? ext_name
if extension.is_a?(Hash)
# Multiple instance extension
if extension.size == 1
@ -72,10 +74,8 @@ module Middleman
extension_config["#{ext_name} (#{inst})"] = extension_options(ext)
end
end
elsif extension.is_a?(::Middleman::Extension)
extension_config[ext_name] = extension_options(extension)
else
extension_config[ext_name] = nil
extension_config[ext_name] = extension_options(extension)
end
end