1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00

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

@ -22,7 +22,7 @@ Feature: Neighboring YAML Front Matter
Then I should not see "---" Then I should not see "---"
When I go to "/raw-front-matter.php.frontmatter" When I go to "/raw-front-matter.php.frontmatter"
Then I should see "File Not Found" Then I should see "File Not Found"
Scenario: YAML not on first line, with encoding Scenario: YAML not on first line, with encoding
Given the Server is running at "frontmatter-neighbor-app" Given the Server is running at "frontmatter-neighbor-app"
When I go to "/front-matter-encoding.html" 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" 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,27 +84,35 @@ module Middleman
Contract ResourceList => ResourceList Contract ResourceList => ResourceList
def manipulate_resource_list(resources) def manipulate_resource_list(resources)
@collector_roots.each do |pair| @lock.synchronize do
dataset = pair[:block].call(app, resources) @collector_roots.each do |pair|
pair[:root].realize!(dataset) dataset = pair[:block].call(app, resources)
end pair[:root].realize!(dataset)
end
ctx = StepContext.new ctx = StepContext.new
leaves = @leaves.dup StepContext.current = ctx
@collectors_by_name.each do |k, v| leaves = @leaves.dup
@values_by_name[k] = v.value(ctx)
leaves.delete v
end
# Execute code paths @collectors_by_name.each do |k, v|
leaves.each do |v| @values_by_name[k] = v.value(ctx)
v.value(ctx) leaves.delete v
end end
# Inject descriptors # Execute code paths
ctx.descriptors.reduce(resources) do |sum, d| leaves.each do |v|
d.execute_descriptor(app, sum) 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 end
end end

View file

@ -2,8 +2,12 @@ module Middleman
module CoreExtensions module CoreExtensions
module Collections module Collections
class StepContext class StepContext
def self.add_to_context(name, &func) class << self
send(:define_method, :"_internal_#{name}", &func) attr_accessor :current
def add_to_context(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,11 +505,24 @@ 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>
Contract ResourceList => ResourceList Contract ResourceList => ResourceList
@ -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