From e99649b33e44efa2c442968727e5fe2483162efc Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Mon, 11 Apr 2016 16:02:31 -0700 Subject: [PATCH] Make global config access work inside steps --- .gitignore | 2 + .../features/front-matter-neighbor.feature | 8 +++- .../core_extensions/collections.rb | 45 ++++++++++++------- .../collections/step_context.rb | 8 +++- .../middleman-core/core_extensions/data.rb | 2 + .../middleman-core/core_extensions/i18n.rb | 2 + .../lib/middleman-core/extension.rb | 40 +++++++++++++++-- 7 files changed, 82 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 88e2e11e..aabe272b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ +.byebug_history npm-debug.log +manifest.yaml /.bundle .DS_Store coverage diff --git a/middleman-core/features/front-matter-neighbor.feature b/middleman-core/features/front-matter-neighbor.feature index e2e5cc69..9260ff41 100644 --- a/middleman-core/features/front-matter-neighbor.feature +++ b/middleman-core/features/front-matter-neighbor.feature @@ -22,7 +22,7 @@ Feature: Neighboring YAML Front Matter Then I should not see "---" When I go to "/raw-front-matter.php.frontmatter" Then I should see "File Not Found" - + Scenario: YAML not on first line, with encoding Given the Server is running at "frontmatter-neighbor-app" When I go to "/front-matter-encoding.html" @@ -35,7 +35,7 @@ Feature: Neighboring YAML Front Matter Given the Server is running at "frontmatter-neighbor-app" And the file "source/front-matter-change.html.erb" has the contents """ - <%= current_page.data.title %> + FileA <%= current_page.data.title %> """ And the file "source/front-matter-change.html.erb.frontmatter" has the contents """ @@ -43,6 +43,8 @@ Feature: Neighboring YAML Front Matter title: Hello World layout: false --- + + FileB """ When I go to "/front-matter-change.html" Then I should see "Hello World" @@ -52,6 +54,8 @@ Feature: Neighboring YAML Front Matter title: Hola Mundo layout: false --- + + FileC """ When I go to "/front-matter-change.html" Then I should see "Hola Mundo" diff --git a/middleman-core/lib/middleman-core/core_extensions/collections.rb b/middleman-core/lib/middleman-core/core_extensions/collections.rb index 434455f1..22e6d281 100644 --- a/middleman-core/lib/middleman-core/core_extensions/collections.rb +++ b/middleman-core/lib/middleman-core/core_extensions/collections.rb @@ -1,3 +1,4 @@ +require 'monitor' require 'middleman-core/core_extensions/collections/pagination' require 'middleman-core/core_extensions/collections/step_context' require 'middleman-core/core_extensions/collections/lazy_root' @@ -41,6 +42,8 @@ module Middleman @values_by_name = {} @collector_roots = [] + + @lock = Monitor.new end def before_configuration @@ -81,27 +84,35 @@ module Middleman Contract ResourceList => ResourceList def manipulate_resource_list(resources) - @collector_roots.each do |pair| - dataset = pair[:block].call(app, resources) - pair[:root].realize!(dataset) - end + @lock.synchronize do + @collector_roots.each do |pair| + dataset = pair[:block].call(app, resources) + pair[:root].realize!(dataset) + end - ctx = StepContext.new - leaves = @leaves.dup + ctx = StepContext.new + StepContext.current = ctx - @collectors_by_name.each do |k, v| - @values_by_name[k] = v.value(ctx) - leaves.delete v - end + leaves = @leaves.dup - # Execute code paths - leaves.each do |v| - v.value(ctx) - end + @collectors_by_name.each do |k, v| + @values_by_name[k] = v.value(ctx) + leaves.delete v + end - # Inject descriptors - ctx.descriptors.reduce(resources) do |sum, d| - d.execute_descriptor(app, sum) + # Execute code paths + leaves.each do |v| + v.value(ctx) + end + + # Inject descriptors + results = ctx.descriptors.reduce(resources) do |sum, d| + d.execute_descriptor(app, sum) + end + + StepContext.current = nil + + results end end end diff --git a/middleman-core/lib/middleman-core/core_extensions/collections/step_context.rb b/middleman-core/lib/middleman-core/core_extensions/collections/step_context.rb index 2ce90b83..0b17870d 100644 --- a/middleman-core/lib/middleman-core/core_extensions/collections/step_context.rb +++ b/middleman-core/lib/middleman-core/core_extensions/collections/step_context.rb @@ -2,8 +2,12 @@ module Middleman module CoreExtensions module Collections class StepContext - def self.add_to_context(name, &func) - send(:define_method, :"_internal_#{name}", &func) + class << self + attr_accessor :current + + def add_to_context(name, &func) + send(:define_method, :"_internal_#{name}", &func) + end end attr_reader :descriptors diff --git a/middleman-core/lib/middleman-core/core_extensions/data.rb b/middleman-core/lib/middleman-core/core_extensions/data.rb index 18057465..d711ef4f 100644 --- a/middleman-core/lib/middleman-core/core_extensions/data.rb +++ b/middleman-core/lib/middleman-core/core_extensions/data.rb @@ -88,6 +88,8 @@ module Middleman def update_files(updated_files, removed_files) updated_files.each(&method(:touch_file)) removed_files.each(&method(:remove_file)) + + @app.sitemap.rebuild_resource_list!(:touched_data_file) end # Update the internal cache for a given file path diff --git a/middleman-core/lib/middleman-core/core_extensions/i18n.rb b/middleman-core/lib/middleman-core/core_extensions/i18n.rb index ac86d9a0..156f2694 100644 --- a/middleman-core/lib/middleman-core/core_extensions/i18n.rb +++ b/middleman-core/lib/middleman-core/core_extensions/i18n.rb @@ -213,6 +213,8 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension def on_file_changed(_updated_files, _removed_files) ::I18n.load_path |= app.files.by_type(:locales).files.map { |p| p[:full_path].to_s } ::I18n.reload! + + @app.sitemap.rebuild_resource_list!(:touched_locale_file) end def configure_i18n diff --git a/middleman-core/lib/middleman-core/extension.rb b/middleman-core/lib/middleman-core/extension.rb index d9dc01ca..8f342e69 100644 --- a/middleman-core/lib/middleman-core/extension.rb +++ b/middleman-core/lib/middleman-core/extension.rb @@ -490,15 +490,14 @@ module Middleman class ConfigExtension < Extension def initialize(app, config={}, &block) @descriptors = {} - @wrapped = {} + @ready = false self.class.exposed_to_config.each do |k, v| @descriptors[k] = [] define_singleton_method(:"__original_#{v}", &method(v)) define_singleton_method(v) do |*args, &b| - @descriptors[k] << method(:"__original_#{v}").call(*args, &b) - @app.sitemap.rebuild_resource_list!(:"first_run_change_#{v}") + proxy_method_call(k, v, args, &b) end end @@ -506,11 +505,24 @@ module Middleman end def after_configuration + context = self + self.class.exposed_to_config.each do |k, v| - ::Middleman::CoreExtensions::Collections::StepContext.add_to_context(k, &method(:"__original_#{v}")) + ::Middleman::CoreExtensions::Collections::StepContext.add_to_context(k) do |*args, &b| + r = context.method(:"__original_#{v}").call(*args, &b) + self.descriptors << r if r.respond_to?(:execute_descriptor) + end end end + def ready + @ready = true + + # @descriptors.each do |k, v| + # @descriptors[k] = [] + # end + end + # Update the main sitemap resource list # @return Array Contract ResourceList => ResourceList @@ -519,5 +531,25 @@ module Middleman c.execute_descriptor(app, sum) end end + + Contract Symbol, Symbol, ArrayOf[Any], Maybe[Proc] => Any + def proxy_method_call(k, v, args, &b) + if @ready + ctx = ::Middleman::CoreExtensions::Collections::StepContext.current + r = method(:"__original_#{v}").call(*args, &b) + + if r.respond_to?(:execute_descriptor) + if ctx + ctx.descriptors << r + else + @descriptors[k] << r + @app.sitemap.rebuild_resource_list!(:"first_run_change_#{v}") + end + end + else + @descriptors[k] << method(:"__original_#{v}").call(*args, &b) + @app.sitemap.rebuild_resource_list!(:"first_run_change_#{v}") + end + end end end