From 86bbcb767638a2ef0a9acd6078d38a03a24c8a7f Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sat, 2 May 2015 19:24:18 -0700 Subject: [PATCH] Replace hooks --- .../lib/middleman-core/application.rb | 53 +++++++------------ middleman-core/lib/middleman-core/builder.rb | 4 +- .../lib/middleman-core/callback_manager.rb | 13 +++-- .../lib/middleman-core/file_renderer.rb | 20 ++----- middleman-core/lib/middleman-core/rack.rb | 2 +- middleman-core/middleman-core.gemspec | 1 - 6 files changed, 37 insertions(+), 56 deletions(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index f3775a8e..c115fa28 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -9,9 +9,6 @@ require 'i18n' require 'active_support/json' require 'active_support/core_ext/integer/inflections' -# Simple callback library -require 'hooks' - # Our custom logger require 'middleman-core/logger' @@ -21,6 +18,7 @@ require 'middleman-core/sitemap/store' require 'middleman-core/configuration' +require 'middleman-core/callback_manager' require 'middleman-core/extension_manager' require 'middleman-core/core_extensions' @@ -56,31 +54,6 @@ module Middleman end end - # Uses callbacks - include Hooks - include Hooks::InstanceHooks - - define_hook :initialized - define_hook :after_configuration - define_hook :before_configuration - - # Before request hook - define_hook :before - - # Ready (all loading and parsing of extensions complete) hook - define_hook :ready - - # Runs before the build is started - define_hook :before_build - - # Runs after the build is finished - define_hook :after_build - - define_hook :before_shutdown - - define_hook :before_render - define_hook :after_render - # Which host preview should start on. # @return [Fixnum] config.define_setting :host, '0.0.0.0', 'The preview server host' @@ -209,6 +182,20 @@ module Middleman # Search the root of the project for required files $LOAD_PATH.unshift(root) unless $LOAD_PATH.include?(root) + @callbacks = ::Middleman::CallbackManager.new + @callbacks.install_methods!(self, [ + :initialized, + :after_configuration, + :before_configuration, + :before, + :ready, + :before_build, + :after_build, + :before_shutdown, + :before_render, + :after_render + ]) + @middleware = Set.new @mappings = Set.new @@ -244,9 +231,9 @@ module Middleman @extensions.auto_activate(:before_configuration) - run_hook :initialized + execute_callbacks :initialized - run_hook :before_configuration + execute_callbacks :before_configuration evaluate_configuration @@ -270,10 +257,10 @@ module Middleman @extensions.activate_all - run_hook :after_configuration + execute_callbacks :after_configuration config_context.execute_callbacks(:after_configuration) - run_hook :ready + execute_callbacks :ready @config_context.execute_callbacks(:ready) end @@ -365,7 +352,7 @@ module Middleman end def shutdown! - run_hook :before_shutdown + execute_callbacks :before_shutdown end # Work around this bug: http://bugs.ruby-lang.org/issues/4521 diff --git a/middleman-core/lib/middleman-core/builder.rb b/middleman-core/lib/middleman-core/builder.rb index b4f789df..60fffd40 100644 --- a/middleman-core/lib/middleman-core/builder.rb +++ b/middleman-core/lib/middleman-core/builder.rb @@ -49,7 +49,7 @@ module Middleman @has_error = false @events = {} - @app.run_hook :before_build, self + @app.execute_callbacks :before_build, [self] queue_current_paths if @cleaning prerender_css @@ -61,7 +61,7 @@ module Middleman ::Middleman::Profiling.report('build') # Run hooks - @app.run_hook :after_build, self + @app.execute_callbacks :after_build, [self] @app.config_context.execute_callbacks(:after_build, [self]) !@has_error diff --git a/middleman-core/lib/middleman-core/callback_manager.rb b/middleman-core/lib/middleman-core/callback_manager.rb index 3bedc8b9..5535ded8 100644 --- a/middleman-core/lib/middleman-core/callback_manager.rb +++ b/middleman-core/lib/middleman-core/callback_manager.rb @@ -25,8 +25,10 @@ module Middleman end install_target.define_singleton_method(:execute_callbacks) do |keys, *args| - manager.execute(keys, args, self) + manager.execute(keys, args[0], self) end + + install_target.define_singleton_method(:callbacks_for, &method(:callbacks_for)) end Contract Or[Symbol, ArrayOf[Symbol]], Proc => Any @@ -40,10 +42,13 @@ module Middleman Contract Or[Symbol, ArrayOf[Symbol]], Maybe[ArrayOf[Any]], Maybe[RespondTo[:instance_exec]] => Any def execute(keys, args=[], scope=self) - immutable_keys = keys.is_a?(Symbol) ? keys : ::Hamster::Vector.new(keys) + callbacks_for(keys).each { |b| scope.instance_exec(*args, &b) } + end - callbacks = @callbacks.get(immutable_keys) - callbacks && callbacks.each { |b| scope.instance_exec(*args, &b) } + Contract Or[Symbol, ArrayOf[Symbol]] => ::Hamster::Set + def callbacks_for(keys) + immutable_keys = keys.is_a?(Symbol) ? keys : ::Hamster::Vector.new(keys) + @callbacks.get(immutable_keys) || ::Hamster.set end end end diff --git a/middleman-core/lib/middleman-core/file_renderer.rb b/middleman-core/lib/middleman-core/file_renderer.rb index 91b083e1..02081b86 100644 --- a/middleman-core/lib/middleman-core/file_renderer.rb +++ b/middleman-core/lib/middleman-core/file_renderer.rb @@ -59,14 +59,10 @@ module Middleman options = options.deep_merge(options[:renderer_options]) if options[:renderer_options] template_class = ::Tilt[path] + # Allow hooks to manipulate the template before render - @app.class.callbacks_for_hook(:before_render).each do |callback| - newbody = if callback.respond_to?(:call) - callback.call(body, path, locs, template_class) - elsif callback.respond_to?(:evaluate) - callback.evaluate(self, body, path, locs, template_class) - end - body = newbody if newbody # Allow the callback to return nil to skip it + content = @app.callbacks_for(:before_render).reduce(content) do |sum, callback| + callback.call(content, path, locs, template_class) || sum end # Read compiled template from disk or cache @@ -80,14 +76,8 @@ module Middleman end # Allow hooks to manipulate the result after render - @app.class.callbacks_for_hook(:after_render).each do |callback| - # Uber::Options::Value doesn't respond to call - newcontent = if callback.respond_to?(:call) - callback.call(content, path, locs, template_class) - elsif callback.respond_to?(:evaluate) - callback.evaluate(self, content, path, locs, template_class) - end - content = newcontent if newcontent # Allow the callback to return nil to skip it + content = @app.callbacks_for(:after_render).reduce(content) do |sum, callback| + callback.call(content, path, locs, template_class) || sum end output = ::ActiveSupport::SafeBuffer.new '' diff --git a/middleman-core/lib/middleman-core/rack.rb b/middleman-core/lib/middleman-core/rack.rb index 9cc79212..b8e438e0 100644 --- a/middleman-core/lib/middleman-core/rack.rb +++ b/middleman-core/lib/middleman-core/rack.rb @@ -85,7 +85,7 @@ module Middleman full_request_path = File.join(env['SCRIPT_NAME'], request_path) # Path including rack mount # Run before callbacks - @middleman.run_hook :before + @middleman.execute_callbacks :before # Get the resource object for this path resource = @middleman.sitemap.find_resource_by_destination_path(request_path.gsub(' ', '%20')) diff --git a/middleman-core/middleman-core.gemspec b/middleman-core/middleman-core.gemspec index 7fbc48d7..752108eb 100644 --- a/middleman-core/middleman-core.gemspec +++ b/middleman-core/middleman-core.gemspec @@ -24,7 +24,6 @@ Gem::Specification.new do |s| s.add_dependency('rack', ['>= 1.4.5', '< 2.0']) s.add_dependency('tilt', ['~> 1.4.1']) s.add_dependency('erubis') - s.add_dependency('hooks', ['~> 0.3']) # Helpers s.add_dependency('activesupport', ['~> 4.2.0'])