-
-
Save benjaminwood/9744892 to your computer and use it in GitHub Desktop.
Revisions
-
benjaminwood revised this gist
Mar 24, 2014 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,6 +32,7 @@ def sort_by_combined_score(totals) flog = Flog.new flog.process(sexp_parsed) flog.calculate_total_scores totals[file] = flog.totals totals -
James Martin revised this gist Jan 18, 2013 . 1 changed file with 9 additions and 31 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,18 +1,12 @@ 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 @@ -24,41 +18,25 @@ def sort_by_combined_score(totals) 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) totals[file] = flog.totals totals end sort_by_combined_score(totals).each do |filename, totals| puts "#{filename} : #{totals}" end -
James Martin created this gist Jan 18, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,66 @@ require 'action_view' require 'ostruct' require 'fileutils' require 'erb' require 'flog' require 'ruby_parser' require 'sexp_processor' ERBHandler = ActionView::Template::Handlers::ERB.new def mkdir_chdir!(path) Dir.mkdir(path) unless File.directory?(path) Dir.chdir(path) end 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__ puts "Contemplative" puts "Finding ERB templates from: #{ARGV[0]}" original_directory = Dir.pwd src_dir = ARGV[0] Dir.chdir src_dir erb_paths = Dir.glob("**/*.erb") files = erb_paths.map do |path| OpenStruct.new( filename: File.basename(path), path: File.dirname(path) ) end Dir.chdir(original_directory) compiled_directory = File.join(original_directory, 'compiled_templates') mkdir_chdir!(compiled_directory) totals = {} files.each do |file| original_filename = File.join(file.path, file.filename) FileUtils.mkdir_p(file.path) raw_erb = File.read(File.join(src_dir, file.path, file.filename)) erubis_source = ERBHandler.call(new_template(raw_erb)) sexp_parsed = RubyParser.new.parse(erubis_source) flog = Flog.new flog.process(sexp_parsed) totals[original_filename] = flog.totals end puts "Totals:" sort_by_combined_score(totals).each do |filename, totals| puts "#{filename} : #{totals}" end end