Remove template lookup cache in build mode. Fixes #1301

This commit is contained in:
Thomas Reynolds 2014-07-20 13:37:16 -07:00
parent 22dace72df
commit debf3c704b

View file

@ -161,60 +161,71 @@ module Middleman
# @param [String] request_path # @param [String] request_path
# @option options [Boolean] :preferred_engine If set, try this engine first, then fall back to any engine. # @option options [Boolean] :preferred_engine If set, try this engine first, then fall back to any engine.
# @return [String, Boolean] Either the path to the template, or false # @return [String, Boolean] Either the path to the template, or false
Contract IsA['Middleman::Application'], Or[Symbol, String], Hash => Maybe[String] Contract IsA['Middleman::Application'], Or[Symbol, String], Maybe[Hash] => Maybe[String]
def self.resolve_template(app, request_path, options={}) def self.resolve_template(app, request_path, options={})
# Find the path by searching or using the cache # Find the path by searching or using the cache
request_path = request_path.to_s request_path = request_path.to_s
cache.fetch(:resolve_template, request_path, options) do
relative_path = Util.strip_leading_slash(request_path)
on_disk_path = File.expand_path(relative_path, app.source_dir)
# By default, any engine will do # Cache lookups in build mode only
preferred_engines = ['*'] if app.build?
preferred_engines << nil if options[:try_static] cache.fetch(:resolve_template, request_path, options) do
uncached_resolve_template(app, request_path, options)
end
else
uncached_resolve_template(app, request_path, options)
end
end
# If we're specifically looking for a preferred engine Contract IsA['Middleman::Application'], String, Hash => Maybe[String]
if options.key?(:preferred_engine) def self.uncached_resolve_template(app, request_path, options)
extension_class = ::Tilt[options[:preferred_engine]] relative_path = Util.strip_leading_slash(request_path)
on_disk_path = File.expand_path(relative_path, app.source_dir)
# Get a list of extensions for a preferred engine # By default, any engine will do
matched_exts = ::Tilt.mappings.select do |_, engines| preferred_engines = ['*']
engines.include? extension_class preferred_engines << nil if options[:try_static]
end.keys
# Prefer to look for the matched extensions # If we're specifically looking for a preferred engine
unless matched_exts.empty? if options.key?(:preferred_engine)
preferred_engines.unshift('{' + matched_exts.join(',') + '}') extension_class = ::Tilt[options[:preferred_engine]]
end
# Get a list of extensions for a preferred engine
matched_exts = ::Tilt.mappings.select do |_, engines|
engines.include? extension_class
end.keys
# Prefer to look for the matched extensions
unless matched_exts.empty?
preferred_engines.unshift('{' + matched_exts.join(',') + '}')
end
end
search_paths = preferred_engines.map do |preferred_engine|
path_with_ext = on_disk_path.dup
path_with_ext << ('.' + preferred_engine) unless preferred_engine.nil?
path_with_ext
end
found_path = nil
search_paths.each do |path_with_ext|
found_path = Dir[path_with_ext].find do |path|
::Tilt[path]
end end
search_paths = preferred_engines.map do |preferred_engine| unless found_path
path_with_ext = on_disk_path.dup found_path = path_with_ext if File.exist?(path_with_ext)
path_with_ext << ('.' + preferred_engine) unless preferred_engine.nil?
path_with_ext
end end
found_path = nil break if found_path
search_paths.each do |path_with_ext| end
found_path = Dir[path_with_ext].find do |path|
::Tilt[path]
end
unless found_path # If we found one, return it
found_path = path_with_ext if File.exist?(path_with_ext) if found_path
end found_path
elsif File.exist?(on_disk_path)
break if found_path on_disk_path
end else
nil
# If we found one, return it
if found_path
found_path
elsif File.exist?(on_disk_path)
on_disk_path
else
nil
end
end end
end end
end end