lib/actionview_precompiler.rb in actionview_precompiler-0.2.3 vs lib/actionview_precompiler.rb in actionview_precompiler-0.3.0
- old
+ new
@@ -1,33 +1,47 @@
+require "action_controller"
+require "action_view"
+
require "actionview_precompiler/version"
require "actionview_precompiler/ast_parser"
require "actionview_precompiler/template_parser"
require "actionview_precompiler/render_parser"
+require "actionview_precompiler/controller_parser"
+require "actionview_precompiler/helper_parser"
require "actionview_precompiler/precompiler"
require "actionview_precompiler/parsed_filename"
module ActionviewPrecompiler
class Error < StandardError; end
+ # Allow overriding from ActionView default handlers if necessary
+ HANDLERS_FOR_EXTENSION = Hash.new do |h, ext|
+ h[ext] = ActionView::Template.handler_for_extension(ext)
+ end
+
def self.precompile(verbose: false)
- target = ActionController::Base # fixme
- view_paths = target.view_paths
- lookup_context = ActionView::LookupContext.new(view_paths)
- paths = view_paths.map(&:path)
- precompiler = Precompiler.new(paths)
+ precompiler = Precompiler.new(verbose: verbose)
- mod = target.view_context_class
- count = 0
- precompiler.each_lookup_args do |args|
- templates = lookup_context.find_all(*args)
- templates.each do |template|
- puts "precompiling: #{template.inspect}" if verbose
- count += 1
- template.send(:compile!, mod)
+ if block_given?
+ # Custom configuration
+ yield precompiler
+ else
+ # Scan view dirs
+ ActionController::Base.view_paths.each do |view_path|
+ precompiler.scan_view_dir view_path.path
end
- end
- if verbose
- puts "Precompiled #{count} Templates"
+ # If we have an application, scan controllers
+ if Rails.respond_to?(:application)
+ Rails.application.paths["app/controllers"].each do |path|
+ precompiler.scan_controller_dir path.to_s
+ end
+
+ Rails.application.paths["app/helpers"].each do |path|
+ precompiler.scan_helper_dir path.to_s
+ end
+ end
end
+
+ precompiler.run
end
end