2016-08-06 12:48:35 -04:00
|
|
|
require "concurrent/map"
|
|
|
|
require "action_view/path_set"
|
2012-12-17 15:21:16 -05:00
|
|
|
|
2012-12-16 17:17:53 -05:00
|
|
|
module ActionView
|
2013-12-15 21:51:35 -05:00
|
|
|
class DependencyTracker # :nodoc:
|
2015-09-19 09:56:26 -04:00
|
|
|
@trackers = Concurrent::Map.new
|
2012-12-16 17:38:46 -05:00
|
|
|
|
2015-07-15 17:32:45 -04:00
|
|
|
def self.find_dependencies(name, template, view_paths = nil)
|
2012-12-17 15:26:55 -05:00
|
|
|
tracker = @trackers[template.handler]
|
2016-02-10 17:42:09 -05:00
|
|
|
return [] unless tracker
|
2012-12-17 15:26:55 -05:00
|
|
|
|
2016-02-10 17:42:09 -05:00
|
|
|
tracker.call(name, template, view_paths)
|
2012-12-16 17:38:46 -05:00
|
|
|
end
|
|
|
|
|
2013-02-20 10:42:30 -05:00
|
|
|
def self.register_tracker(extension, tracker)
|
|
|
|
handler = Template.handler_for_extension(extension)
|
2016-02-10 17:42:09 -05:00
|
|
|
if tracker.respond_to?(:supports_view_paths?)
|
|
|
|
@trackers[handler] = tracker
|
|
|
|
else
|
|
|
|
@trackers[handler] = lambda { |name, template, _|
|
|
|
|
tracker.call(name, template)
|
|
|
|
}
|
|
|
|
end
|
2012-12-16 17:38:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.remove_tracker(handler)
|
|
|
|
@trackers.delete(handler)
|
2012-12-16 17:17:53 -05:00
|
|
|
end
|
|
|
|
|
2013-12-15 21:51:35 -05:00
|
|
|
class ERBTracker # :nodoc:
|
2012-12-16 17:17:53 -05:00
|
|
|
EXPLICIT_DEPENDENCY = /# Template Dependency: (\S+)/
|
|
|
|
|
2013-12-15 21:51:35 -05:00
|
|
|
# A valid ruby identifier - suitable for class, method and specially variable names
|
|
|
|
IDENTIFIER = /
|
|
|
|
[[:alpha:]_] # at least one uppercase letter, lowercase letter or underscore
|
|
|
|
[[:word:]]* # followed by optional letters, numbers or underscores
|
|
|
|
/x
|
|
|
|
|
|
|
|
# Any kind of variable name. e.g. @instance, @@class, $global or local.
|
|
|
|
# Possibly following a method call chain
|
|
|
|
VARIABLE_OR_METHOD_CHAIN = /
|
|
|
|
(?:\$|@{1,2})? # optional global, instance or class variable indicator
|
|
|
|
(?:#{IDENTIFIER}\.)* # followed by an optional chain of zero-argument method calls
|
|
|
|
(?<dynamic>#{IDENTIFIER}) # and a final valid identifier, captured as DYNAMIC
|
|
|
|
/x
|
|
|
|
|
|
|
|
# A simple string literal. e.g. "School's out!"
|
|
|
|
STRING = /
|
|
|
|
(?<quote>['"]) # an opening quote
|
|
|
|
(?<static>.*?) # with anything inside, captured as STATIC
|
|
|
|
\k<quote> # and a matching closing quote
|
|
|
|
/x
|
|
|
|
|
|
|
|
# Part of any hash containing the :partial key
|
|
|
|
PARTIAL_HASH_KEY = /
|
|
|
|
(?:\bpartial:|:partial\s*=>) # partial key in either old or new style hash syntax
|
|
|
|
\s* # followed by optional spaces
|
|
|
|
/x
|
|
|
|
|
2014-07-25 19:35:16 -04:00
|
|
|
# Part of any hash containing the :layout key
|
|
|
|
LAYOUT_HASH_KEY = /
|
|
|
|
(?:\blayout:|:layout\s*=>) # layout key in either old or new style hash syntax
|
|
|
|
\s* # followed by optional spaces
|
|
|
|
/x
|
|
|
|
|
2012-12-16 17:17:53 -05:00
|
|
|
# Matches:
|
2013-12-21 19:14:07 -05:00
|
|
|
# partial: "comments/comment", collection: @all_comments => "comments/comment"
|
|
|
|
# (object: @single_comment, partial: "comments/comment") => "comments/comment"
|
2012-12-16 17:17:53 -05:00
|
|
|
#
|
2013-12-21 19:14:07 -05:00
|
|
|
# "comments/comments"
|
|
|
|
# 'comments/comments'
|
|
|
|
# ('comments/comments')
|
|
|
|
#
|
|
|
|
# (@topic) => "topics/topic"
|
|
|
|
# topics => "topics/topic"
|
|
|
|
# (message.topics) => "topics/topic"
|
|
|
|
RENDER_ARGUMENTS = /\A
|
2014-07-25 19:35:16 -04:00
|
|
|
(?:\s*\(?\s*) # optional opening paren surrounded by spaces
|
|
|
|
(?:.*?#{PARTIAL_HASH_KEY}|#{LAYOUT_HASH_KEY})? # optional hash, up to the partial or layout key declaration
|
|
|
|
(?:#{STRING}|#{VARIABLE_OR_METHOD_CHAIN}) # finally, the dependency name of interest
|
2013-12-15 21:51:35 -05:00
|
|
|
/xm
|
2012-12-16 17:17:53 -05:00
|
|
|
|
2015-02-24 09:08:05 -05:00
|
|
|
LAYOUT_DEPENDENCY = /\A
|
|
|
|
(?:\s*\(?\s*) # optional opening paren surrounded by spaces
|
|
|
|
(?:.*?#{LAYOUT_HASH_KEY}) # check if the line has layout key declaration
|
|
|
|
(?:#{STRING}|#{VARIABLE_OR_METHOD_CHAIN}) # finally, the dependency name of interest
|
|
|
|
/xm
|
|
|
|
|
2015-07-15 17:32:45 -04:00
|
|
|
def self.supports_view_paths? # :nodoc:
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.call(name, template, view_paths = nil)
|
|
|
|
new(name, template, view_paths).dependencies
|
2012-12-16 17:17:53 -05:00
|
|
|
end
|
|
|
|
|
2015-07-15 17:32:45 -04:00
|
|
|
def initialize(name, template, view_paths = nil)
|
|
|
|
@name, @template, @view_paths = name, template, view_paths
|
2012-12-16 17:17:53 -05:00
|
|
|
end
|
|
|
|
|
2013-02-20 10:42:30 -05:00
|
|
|
def dependencies
|
2012-12-16 17:17:53 -05:00
|
|
|
render_dependencies + explicit_dependencies
|
|
|
|
end
|
|
|
|
|
2013-02-26 18:56:56 -05:00
|
|
|
attr_reader :name, :template
|
|
|
|
private :name, :template
|
|
|
|
|
2014-07-25 19:35:16 -04:00
|
|
|
private
|
2013-02-20 10:42:30 -05:00
|
|
|
def source
|
|
|
|
template.source
|
|
|
|
end
|
|
|
|
|
2012-12-16 17:17:53 -05:00
|
|
|
def directory
|
|
|
|
name.split("/")[0..-2].join("/")
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_dependencies
|
2013-12-15 21:51:35 -05:00
|
|
|
render_dependencies = []
|
2013-12-21 19:14:07 -05:00
|
|
|
render_calls = source.split(/\brender\b/).drop(1)
|
2012-12-16 17:17:53 -05:00
|
|
|
|
2013-12-21 19:14:07 -05:00
|
|
|
render_calls.each do |arguments|
|
2015-02-24 09:08:05 -05:00
|
|
|
add_dependencies(render_dependencies, arguments, LAYOUT_DEPENDENCY)
|
|
|
|
add_dependencies(render_dependencies, arguments, RENDER_ARGUMENTS)
|
2013-12-15 21:51:35 -05:00
|
|
|
end
|
2012-12-16 17:17:53 -05:00
|
|
|
|
2013-12-15 21:51:35 -05:00
|
|
|
render_dependencies.uniq
|
|
|
|
end
|
|
|
|
|
2015-02-24 09:08:05 -05:00
|
|
|
def add_dependencies(render_dependencies, arguments, pattern)
|
|
|
|
arguments.scan(pattern) do
|
|
|
|
add_dynamic_dependency(render_dependencies, Regexp.last_match[:dynamic])
|
|
|
|
add_static_dependency(render_dependencies, Regexp.last_match[:static])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-15 21:51:35 -05:00
|
|
|
def add_dynamic_dependency(dependencies, dependency)
|
|
|
|
if dependency
|
|
|
|
dependencies << "#{dependency.pluralize}/#{dependency.singularize}"
|
|
|
|
end
|
|
|
|
end
|
2012-12-16 17:17:53 -05:00
|
|
|
|
2013-12-15 21:51:35 -05:00
|
|
|
def add_static_dependency(dependencies, dependency)
|
|
|
|
if dependency
|
2016-08-06 12:48:35 -04:00
|
|
|
if dependency.include?("/")
|
2013-12-15 21:51:35 -05:00
|
|
|
dependencies << dependency
|
|
|
|
else
|
|
|
|
dependencies << "#{directory}/#{dependency}"
|
|
|
|
end
|
|
|
|
end
|
2012-12-16 17:17:53 -05:00
|
|
|
end
|
|
|
|
|
2015-07-15 17:32:45 -04:00
|
|
|
def resolve_directories(wildcard_dependencies)
|
|
|
|
return [] unless @view_paths
|
|
|
|
|
2016-02-10 18:44:14 -05:00
|
|
|
wildcard_dependencies.flat_map { |query, templates|
|
|
|
|
@view_paths.find_all_with_query(query).map do |template|
|
|
|
|
"#{File.dirname(query)}/#{File.basename(template).split('.').first}"
|
2015-07-15 17:32:45 -04:00
|
|
|
end
|
2016-02-10 18:44:14 -05:00
|
|
|
}.sort
|
2015-07-15 17:32:45 -04:00
|
|
|
end
|
|
|
|
|
2012-12-16 17:17:53 -05:00
|
|
|
def explicit_dependencies
|
2015-07-15 17:32:45 -04:00
|
|
|
dependencies = source.scan(EXPLICIT_DEPENDENCY).flatten.uniq
|
|
|
|
|
2016-08-06 12:48:35 -04:00
|
|
|
wildcards, explicits = dependencies.partition { |dependency| dependency[-1] == "*" }
|
2015-07-15 17:32:45 -04:00
|
|
|
|
|
|
|
(explicits + resolve_directories(wildcards)).uniq
|
2012-12-16 17:17:53 -05:00
|
|
|
end
|
|
|
|
end
|
2012-12-16 17:38:46 -05:00
|
|
|
|
2013-02-20 10:42:30 -05:00
|
|
|
register_tracker :erb, ERBTracker
|
2012-12-16 17:17:53 -05:00
|
|
|
end
|
|
|
|
end
|