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:
parent
ec213efb95
commit
e99649b33e
7 changed files with 82 additions and 25 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,4 +1,6 @@
|
|||
.byebug_history
|
||||
npm-debug.log
|
||||
manifest.yaml
|
||||
/.bundle
|
||||
.DS_Store
|
||||
coverage
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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,12 +84,15 @@ module Middleman
|
|||
|
||||
Contract ResourceList => ResourceList
|
||||
def manipulate_resource_list(resources)
|
||||
@lock.synchronize do
|
||||
@collector_roots.each do |pair|
|
||||
dataset = pair[:block].call(app, resources)
|
||||
pair[:root].realize!(dataset)
|
||||
end
|
||||
|
||||
ctx = StepContext.new
|
||||
StepContext.current = ctx
|
||||
|
||||
leaves = @leaves.dup
|
||||
|
||||
@collectors_by_name.each do |k, v|
|
||||
|
@ -100,9 +106,14 @@ module Middleman
|
|||
end
|
||||
|
||||
# Inject descriptors
|
||||
ctx.descriptors.reduce(resources) do |sum, d|
|
||||
results = ctx.descriptors.reduce(resources) do |sum, d|
|
||||
d.execute_descriptor(app, sum)
|
||||
end
|
||||
|
||||
StepContext.current = nil
|
||||
|
||||
results
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,9 +2,13 @@ module Middleman
|
|||
module CoreExtensions
|
||||
module Collections
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :descriptors
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,10 +505,23 @@ 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>
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue