lib/actionview_precompiler/precompiler.rb in actionview_precompiler-0.2.3 vs lib/actionview_precompiler/precompiler.rb in actionview_precompiler-0.3.0

- old
+ new

@@ -1,95 +1,69 @@ +require "actionview_precompiler/template_scanner" +require "actionview_precompiler/controller_scanner" +require "actionview_precompiler/helper_scanner" +require "actionview_precompiler/template_loader" + module ActionviewPrecompiler class Precompiler - class Template - attr_reader :fullpath, :relative_path, :virtual_path - attr_reader :action, :prefix, :details + def initialize(verbose: false) + @scanners = [] + @loader = TemplateLoader.new + @verbose = verbose + @static_templates = [] + @template_renders = nil + end - def initialize(fullpath, relative_path) - @fullpath = fullpath - @relative_path = relative_path - @virtual_path = relative_path.slice(0, relative_path.index(".")) + def scan_view_dir(view_dir) + @scanners << TemplateScanner.new(view_dir) + end - parsed = ParsedFilename.new(relative_path) - @prefix = parsed.prefix - @action = parsed.action - @partial = parsed.partial? - @details = parsed.details - end + def scan_controller_dir(controller_dir) + @scanners << ControllerScanner.new(controller_dir) + end - def partial? - @partial - end + def scan_helper_dir(controller_dir) + @scanners << HelperScanner.new(controller_dir) end - attr_reader :templates + def add_template(virtual_path, locals = []) + locals = locals.map(&:to_s).sort + @static_templates << [virtual_path, locals] + end - def initialize(view_dirs) - @templates = - view_dirs.flat_map do |view_dir| - Dir["**/*", base: view_dir].map do |file| - fullpath = File.expand_path(file, view_dir) - next if File.directory?(fullpath) + def run + count = 0 + template_renders.each do |virtual_path, locals| + debug "precompiling: #{virtual_path}" - Template.new(fullpath, file) - end.compact - end + templates = @loader.load_template(virtual_path, locals) + count += templates.count - determine_locals + debug " No templates found at #{virtual_path}" if templates.empty? + end + + debug "Precompiled #{count} Templates" end - def each_lookup_args - return enum_for(__method__) unless block_given? + def template_renders + return @template_renders if @template_renders - each_template_render do |template, locals| - details = { - locale: Array(template.details[:locale]), - variants: Array(template.details[:variant]), - formats: Array(template.details[:format]), - handlers: Array(template.details[:handler]) - } + template_renders = [] - yield [template.action, template.prefix, template.partial?, locals, details] + @scanners.each do |scanner| + template_renders.concat scanner.template_renders end - nil - end + template_renders.concat @static_templates - def each_template_render - return enum_for(__method__) unless block_given? + template_renders.uniq! - @templates.each do |template| - locals_set = @locals_sets[template.virtual_path] - if locals_set - locals_set.each do |locals| - yield template, locals - end - elsif !template.partial? - # For now, guess that non-partials we haven't seen take no locals - yield template, [] - else - # Locals unknown - end - end + @template_renders = template_renders end - def determine_locals - @locals_sets = {} + private - @templates.each do |template| - parser = TemplateParser.new(template.fullpath) - parser.render_calls.each do |render_call| - virtual_path = render_call.virtual_path - unless virtual_path.include?("/") - # Not necessarily true, since the perfix is based on the current - # controller, but is a safe bet most of the time. - virtual_path = "#{template.prefix}/#{virtual_path}" - end - @locals_sets[virtual_path] ||= [] - @locals_sets[virtual_path] << render_call.locals_keys.map(&:to_s).sort - end - end - - @locals_sets.each_value(&:uniq!) + def debug(msg) + puts msg if @verbose end end end