Merge pull request #1876 from middleman/bugfix/global-collection-steps

Make global config access work inside steps
This commit is contained in:
Thomas Reynolds 2016-04-11 16:23:07 -07:00
commit c7669609e6
7 changed files with 82 additions and 25 deletions

2
.gitignore vendored
View file

@ -1,4 +1,6 @@
.byebug_history
npm-debug.log
manifest.yaml
/.bundle
.DS_Store
coverage

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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<Middleman::Sitemap::Resource>
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