Skip to content

Instantly share code, notes, and snippets.

@benjaminwood
Forked from jamesmartin/contemplative.rb
Created March 24, 2014 17:22
Show Gist options
  • Save benjaminwood/9744892 to your computer and use it in GitHub Desktop.
Save benjaminwood/9744892 to your computer and use it in GitHub Desktop.

Revisions

  1. benjaminwood revised this gist Mar 24, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions contemplative.rb
    Original 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
  2. James Martin revised this gist Jan 18, 2013. 1 changed file with 9 additions and 31 deletions.
    40 changes: 9 additions & 31 deletions contemplative.rb
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,12 @@
    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
    @@ -24,41 +18,25 @@ def sort_by_combined_score(totals)
    end

    if $0 == __FILE__
    puts "Contemplative"
    puts "Finding ERB templates from: #{ARGV[0]}"
    original_directory = Dir.pwd
    src_dir = ARGV[0]
    raise ArgumentError, "Supply a path containing ERB templates" unless src_dir

    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)
    puts "Contemplating ERB templates from: #{src_dir}"

    totals = {}
    files.each do |file|
    original_filename = File.join(file.path, file.filename)
    FileUtils.mkdir_p(file.path)
    erb_files = Dir.glob(File.join(src_dir, "**/*.erb"))

    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)
    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[original_filename] = flog.totals
    totals[file] = flog.totals
    totals
    end

    puts "Totals:"
    sort_by_combined_score(totals).each do |filename, totals|
    puts "#{filename} : #{totals}"
    end
  3. James Martin created this gist Jan 18, 2013.
    66 changes: 66 additions & 0 deletions contemplative.rb
    Original 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