Make global config access work inside steps

This commit is contained in:
Thomas Reynolds 2016-04-11 16:02:31 -07:00
parent ec213efb95
commit e99649b33e
7 changed files with 82 additions and 25 deletions

2
.gitignore vendored
View file

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

View file

@ -35,7 +35,7 @@ Feature: Neighboring YAML Front Matter
Given the Server is running at "frontmatter-neighbor-app" Given the Server is running at "frontmatter-neighbor-app"
And the file "source/front-matter-change.html.erb" has the contents 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 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 title: Hello World
layout: false layout: false
--- ---
FileB
""" """
When I go to "/front-matter-change.html" When I go to "/front-matter-change.html"
Then I should see "Hello World" Then I should see "Hello World"
@ -52,6 +54,8 @@ Feature: Neighboring YAML Front Matter
title: Hola Mundo title: Hola Mundo
layout: false layout: false
--- ---
FileC
""" """
When I go to "/front-matter-change.html" When I go to "/front-matter-change.html"
Then I should see "Hola Mundo" 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/pagination'
require 'middleman-core/core_extensions/collections/step_context' require 'middleman-core/core_extensions/collections/step_context'
require 'middleman-core/core_extensions/collections/lazy_root' require 'middleman-core/core_extensions/collections/lazy_root'
@ -41,6 +42,8 @@ module Middleman
@values_by_name = {} @values_by_name = {}
@collector_roots = [] @collector_roots = []
@lock = Monitor.new
end end
def before_configuration def before_configuration
@ -81,12 +84,15 @@ module Middleman
Contract ResourceList => ResourceList Contract ResourceList => ResourceList
def manipulate_resource_list(resources) def manipulate_resource_list(resources)
@lock.synchronize do
@collector_roots.each do |pair| @collector_roots.each do |pair|
dataset = pair[:block].call(app, resources) dataset = pair[:block].call(app, resources)
pair[:root].realize!(dataset) pair[:root].realize!(dataset)
end end
ctx = StepContext.new ctx = StepContext.new
StepContext.current = ctx
leaves = @leaves.dup leaves = @leaves.dup
@collectors_by_name.each do |k, v| @collectors_by_name.each do |k, v|
@ -100,9 +106,14 @@ module Middleman
end end
# Inject descriptors # Inject descriptors
ctx.descriptors.reduce(resources) do |sum, d| results = ctx.descriptors.reduce(resources) do |sum, d|
d.execute_descriptor(app, sum) d.execute_descriptor(app, sum)
end end
StepContext.current = nil
results
end
end end
end end
end end

View file

@ -2,9 +2,13 @@ module Middleman
module CoreExtensions module CoreExtensions
module Collections module Collections
class StepContext class StepContext
def self.add_to_context(name, &func) class << self
attr_accessor :current
def add_to_context(name, &func)
send(:define_method, :"_internal_#{name}", &func) send(:define_method, :"_internal_#{name}", &func)
end end
end
attr_reader :descriptors attr_reader :descriptors

View file

@ -88,6 +88,8 @@ module Middleman
def update_files(updated_files, removed_files) def update_files(updated_files, removed_files)
updated_files.each(&method(:touch_file)) updated_files.each(&method(:touch_file))
removed_files.each(&method(:remove_file)) removed_files.each(&method(:remove_file))
@app.sitemap.rebuild_resource_list!(:touched_data_file)
end end
# Update the internal cache for a given file path # 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) 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.load_path |= app.files.by_type(:locales).files.map { |p| p[:full_path].to_s }
::I18n.reload! ::I18n.reload!
@app.sitemap.rebuild_resource_list!(:touched_locale_file)
end end
def configure_i18n def configure_i18n

View file

@ -490,15 +490,14 @@ module Middleman
class ConfigExtension < Extension class ConfigExtension < Extension
def initialize(app, config={}, &block) def initialize(app, config={}, &block)
@descriptors = {} @descriptors = {}
@wrapped = {} @ready = false
self.class.exposed_to_config.each do |k, v| self.class.exposed_to_config.each do |k, v|
@descriptors[k] = [] @descriptors[k] = []
define_singleton_method(:"__original_#{v}", &method(v)) define_singleton_method(:"__original_#{v}", &method(v))
define_singleton_method(v) do |*args, &b| define_singleton_method(v) do |*args, &b|
@descriptors[k] << method(:"__original_#{v}").call(*args, &b) proxy_method_call(k, v, args, &b)
@app.sitemap.rebuild_resource_list!(:"first_run_change_#{v}")
end end
end end
@ -506,10 +505,23 @@ module Middleman
end end
def after_configuration def after_configuration
context = self
self.class.exposed_to_config.each do |k, v| 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 end
end
def ready
@ready = true
# @descriptors.each do |k, v|
# @descriptors[k] = []
# end
end
# Update the main sitemap resource list # Update the main sitemap resource list
# @return Array<Middleman::Sitemap::Resource> # @return Array<Middleman::Sitemap::Resource>
@ -519,5 +531,25 @@ module Middleman
c.execute_descriptor(app, sum) c.execute_descriptor(app, sum)
end end
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
end end