Skip to content

Instantly share code, notes, and snippets.

@benjaminwood
Forked from jamesmartin/contemplative.rb
Created March 24, 2014 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminwood/9744892 to your computer and use it in GitHub Desktop.
Save benjaminwood/9744892 to your computer and use it in GitHub Desktop.
require 'action_view'
require 'ostruct'
require 'erb'
require 'flog'
require 'ruby_parser'
require 'sexp_processor'
ERBHandler = ActionView::Template::Handlers::ERB.new
def new_template(body, details={format: :html})
ActionView::Template.new(body, "irrelevant", details.fetch(:handler) {ERBHandler}, details)
end
def sort_by_combined_score(totals)
totals.sort do |a, b|
b[1].values.inject(&:+) <=> a[1].values.inject(&:+)
end
end
if $0 == __FILE__
src_dir = ARGV[0]
raise ArgumentError, "Supply a path containing ERB templates" unless src_dir
puts "Contemplating ERB templates from: #{src_dir}"
erb_files = Dir.glob(File.join(src_dir, "**/*.erb"))
totals = erb_files.inject({}) do |totals, file|
erb_source = File.read(file)
ruby_source = ERBHandler.call(new_template(erb_source))
sexp_parsed = RubyParser.new.parse(ruby_source)
flog = Flog.new
flog.process(sexp_parsed)
flog.calculate_total_scores
totals[file] = flog.totals
totals
end
sort_by_combined_score(totals).each do |filename, totals|
puts "#{filename} : #{totals}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment