From 928eb82d65bc7426d9579e19958cb263a0bb22c0 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Tue, 8 Jul 2014 17:02:02 -0700 Subject: [PATCH] Convert Sitemap::Extensions into actual Middleman::Extension --- .../lib/middleman-core/application.rb | 3 +- .../lib/middleman-core/configuration.rb | 1 - .../sitemap/extensions/ignores.rb | 9 +-- .../sitemap/extensions/on_disk.rb | 40 ++++++-------- .../sitemap/extensions/proxies.rb | 9 ++- .../sitemap/extensions/redirects.rb | 7 ++- .../sitemap/extensions/request_endpoints.rb | 9 ++- .../lib/middleman-core/sitemap/store.rb | 55 ++++++++++--------- 8 files changed, 67 insertions(+), 66 deletions(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index c6848bb9..f5cfc1c3 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -176,7 +176,7 @@ module Middleman def_delegator :"::Middleman::Util", :instrument def_delegators :"self.class", :root, :root_path def_delegators :@generic_template_context, :link_to, :image_tag, :asset_path - + # Initialize the Middleman project def initialize(&block) # Search the root of the project for required files @@ -332,6 +332,5 @@ module Middleman "#" end alias_method :inspect, :to_s # Ruby 2.0 calls inspect for NoMethodError instead of to_s - end end diff --git a/middleman-core/lib/middleman-core/configuration.rb b/middleman-core/lib/middleman-core/configuration.rb index a17382ce..58141d11 100644 --- a/middleman-core/lib/middleman-core/configuration.rb +++ b/middleman-core/lib/middleman-core/configuration.rb @@ -1,6 +1,5 @@ module Middleman module Configuration - # A class that manages a collection of documented settings. # Can be used by extensions as well as the main Middleman # application. Extensions should probably finalize their instance diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb b/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb index b77f0e19..feee4c1d 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb @@ -2,16 +2,17 @@ module Middleman module Sitemap module Extensions # Class to handle managing ignores - class Ignores - def initialize(app, sitemap) - @app = app + class Ignores < Extension + def initialize(app, config={}, &block) + super + @app.add_to_config_context :ignore, &method(:create_ignore) @app.define_singleton_method :ignore, &method(:create_ignore) # Array of callbacks which can ass ignored @ignored_callbacks = [] - sitemap.define_singleton_method :ignored?, &method(:ignored?) + @app.sitemap.define_singleton_method :ignored?, &method(:ignored?) end # Ignore a path or add an ignore callback diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb index a12ecf44..e39ca08f 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb @@ -3,30 +3,17 @@ require 'set' module Middleman module Sitemap module Extensions - class OnDisk - attr_accessor :sitemap + class OnDisk < Extension attr_accessor :waiting_for_ready - def initialize(app, sitemap) - @sitemap = sitemap - @app = app + def initialize(app, config={}, &block) + super + @file_paths_on_disk = Set.new scoped_self = self @waiting_for_ready = true - @app.before_configuration do - # Register file change callback - extensions[:file_watcher].api.changed do |file| - scoped_self.touch_file(file) - end - - # Register file delete callback - extensions[:file_watcher].api.deleted do |file| - scoped_self.remove_file(file) - end - end - @app.ready do scoped_self.waiting_for_ready = false # Make sure the sitemap is ready for the first request @@ -34,13 +21,18 @@ module Middleman end end + def before_configuration + file_watcher.changed(&method(:touch_file)) + file_watcher.deleted(&method(:remove_file)) + end + # Update or add an on-disk file path # @param [String] file # @return [Boolean] def touch_file(file) return false if File.directory?(file) - path = @sitemap.file_to_path(file) + path = @app.sitemap.file_to_path(file) return false unless path ignored = @app.config[:ignored_sitemap_matchers].any? do |_, callback| @@ -57,11 +49,11 @@ module Middleman # in case one of the other manipulators # (like asset_hash) cares about the contents of this file, # whether or not it belongs in the sitemap (like a partial) - @sitemap.rebuild_resource_list!(:touched_file) + @app.sitemap.rebuild_resource_list!(:touched_file) # Force sitemap rebuild so the next request is ready to go. # Skip this during build because the builder will control sitemap refresh. - @sitemap.ensure_resource_list_updated! unless waiting_for_ready || @app.build? + @app.sitemap.ensure_resource_list_updated! unless waiting_for_ready || @app.build? end # Remove a file from the store @@ -70,11 +62,11 @@ module Middleman def remove_file(file) return unless @file_paths_on_disk.delete?(file) - @sitemap.rebuild_resource_list!(:removed_file) + @app.sitemap.rebuild_resource_list!(:removed_file) # Force sitemap rebuild so the next request is ready to go. # Skip this during build because the builder will control sitemap refresh. - @sitemap.ensure_resource_list_updated! unless waiting_for_ready || @app.build? + @app.sitemap.ensure_resource_list_updated! unless waiting_for_ready || @app.build? end # Update the main sitemap resource list @@ -82,8 +74,8 @@ module Middleman def manipulate_resource_list(resources) resources + @file_paths_on_disk.map do |file| ::Middleman::Sitemap::Resource.new( - @sitemap, - @sitemap.file_to_path(file), + @app.sitemap, + @app.sitemap.file_to_path(file), File.join(@app.root, file) ) end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb b/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb index 9acce571..03541302 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb @@ -1,11 +1,14 @@ +require 'middleman-core/sitemap/resource' + module Middleman module Sitemap module Extensions # Manages the list of proxy configurations and manipulates the sitemap # to include new resources based on those configurations - class Proxies - def initialize(app) - @app = app + class Proxies < Extension + def initialize(app, config={}, &block) + super + @app.add_to_config_context :proxy, &method(:create_proxy) @app.define_singleton_method(:proxy, &method(:create_proxy)) diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb index d6b79045..babe6509 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb @@ -5,9 +5,10 @@ module Middleman module Extensions # Manages the list of proxy configurations and manipulates the sitemap # to include new resources based on those configurations - class Redirects - def initialize(app) - @app = app + class Redirects < Extension + def initialize(app, config={}, &block) + super + @app.add_to_config_context :redirect, &method(:create_redirect) @redirects = {} diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb index 8d555fa6..c6046f40 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb @@ -1,11 +1,14 @@ +require 'middleman-core/sitemap/resource' + module Middleman module Sitemap module Extensions - class RequestEndpoints + class RequestEndpoints < Extension # Manages the list of proxy configurations and manipulates the sitemap # to include new resources based on those configurations - def initialize(app) - @app = app + def initialize(app, config={}, &block) + super + @app.add_to_config_context :endpoint, &method(:create_endpoint) @endpoints = {} diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index a4330696..bf7d2a57 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -2,12 +2,35 @@ require 'active_support/core_ext/hash/deep_merge' require 'monitor' -# Extensions -require 'middleman-core/sitemap/extensions/on_disk' -require 'middleman-core/sitemap/extensions/redirects' -require 'middleman-core/sitemap/extensions/request_endpoints' -require 'middleman-core/sitemap/extensions/proxies' -require 'middleman-core/sitemap/extensions/ignores' +# Ignores +Middleman::Extensions.register :sitemap_ignore, auto_activate: :before_configuration do + require 'middleman-core/sitemap/extensions/ignores' + Middleman::Sitemap::Extensions::Ignores +end + +# Files on Disk +Middleman::Extensions.register :sitemap_ondisk, auto_activate: :before_configuration do + require 'middleman-core/sitemap/extensions/on_disk' + Middleman::Sitemap::Extensions::OnDisk +end + +# Endpoints +Middleman::Extensions.register :sitemap_endpoint, auto_activate: :before_configuration do + require 'middleman-core/sitemap/extensions/request_endpoints' + Middleman::Sitemap::Extensions::RequestEndpoints +end + +# Proxies +Middleman::Extensions.register :sitemap_proxies, auto_activate: :before_configuration do + require 'middleman-core/sitemap/extensions/proxies' + Middleman::Sitemap::Extensions::Proxies +end + +# Redirects +Middleman::Extensions.register :sitemap_redirects, auto_activate: :before_configuration do + require 'middleman-core/sitemap/extensions/redirects' + Middleman::Sitemap::Extensions::Redirects +end module Middleman # Sitemap namespace @@ -34,26 +57,6 @@ module Middleman @lock = Monitor.new reset_lookup_cache! - # Handle ignore commands - Middleman::Sitemap::Extensions::Ignores.new(@app, self) - - # Extensions - { - # Register classes which can manipulate the main site map list - on_disk: Middleman::Sitemap::Extensions::OnDisk.new(@app, self), - - # Request Endpoints - request_endpoints: Middleman::Sitemap::Extensions::RequestEndpoints.new(@app), - - # Proxies - proxies: Middleman::Sitemap::Extensions::Proxies.new(@app), - - # Redirects - redirects: Middleman::Sitemap::Extensions::Redirects.new(@app) - }.each do |k, m| - register_resource_list_manipulator(k, m) - end - @app.config_context.class.send :def_delegator, :app, :sitemap end