Commit 6dcb57b8 authored by Coraline Ehmke's avatar Coraline Ehmke

Text output for CLI

parent 0f7d3849
......@@ -4,18 +4,23 @@ require 'poro_plus'
require 'fileutils'
require 'haml'
require "snuffle/version"
require "snuffle/analysis"
require "snuffle/cohort"
require "snuffle/cohort_detector"
require "snuffle/cli"
require "snuffle/node"
require "snuffle/source_file"
require "snuffle/source_parser"
require "snuffle/elements/hash"
require "snuffle/elements/string"
require "snuffle/util/correlation"
require "snuffle/util/histogram"
require_relative "snuffle/version"
require_relative "snuffle/analysis"
require_relative "snuffle/cohort"
require_relative "snuffle/cohort_detector"
require_relative "snuffle/cli"
require_relative "snuffle/formatters/base"
require_relative "snuffle/formatters/csv"
require_relative "snuffle/formatters/html"
require_relative "snuffle/formatters/html_index"
require_relative "snuffle/formatters/text"
require_relative "snuffle/node"
require_relative "snuffle/source_file"
require_relative "snuffle/source_parser"
require_relative "snuffle/elements/hash"
require_relative "snuffle/elements/string"
require_relative "snuffle/util/correlation"
require_relative "snuffle/util/histogram"
module Snuffle
end
......@@ -10,12 +10,10 @@ module Snuffle
desc "check PATH_TO_FILE [-f FORMAT] [-t MAX_COMPLEXITY_ALLOWED]", desc_text
method_option :format, :type => :string, :default => 'text', :aliases => "-f"
method_option :threshold, :type => :numeric, :default => 0, :aliases => "-t"
def check(path="./")
file_list(path).each{ |path_to_file| parse(path_to_file) }
file_list(path).each{ |path_to_file| report(path_to_file) }
handle_index(summaries)
report
end
default_task :check
......@@ -35,34 +33,33 @@ module Snuffle
def formatter
case options['format']
when 'html'
Formatters::Html
::Formatters::Html
when 'csv'
Formatters::Csv
::Formatters::Csv
else
Formatters::Text
::Formatters::Text
end
end
def handle_index(file_summary)
return unless options['format'] == 'html'
index = Formatters::HtmlIndex.new(file_summary)
index = ::Formatters::HtmlIndex.new(file_summary)
self.last_file = "#{index.output_path}/#{index.filename}"
index.export
end
def parse(path_to_file, options={})
file = ParsedFile.new(path_to_file: path_to_file)
def report(path_to_file)
file = SourceFile.new(path_to_file: path_to_file)
parser = formatter.new(file)
self.summaries ||= []
self.summaries << file.summary.merge(results_file: parser.path_to_results)
last_file = file
end
def report
unless options['format'] == "text"
puts "Results written to:"
if options['format'] == "text"
puts parser.export
else
puts "results written to:"
puts last_file.present? && "#{last_file}" || results_files.join("\r\n")
end
report_complexity
end
def results_files
......
require 'text-table'
module Formatters
class Text
......@@ -5,16 +7,19 @@ module Formatters
include Formatters::Base
def header
"#{file.class_name}\t\t#{file.complexity}"
["Class", "Object Candidates"]
end
def export
puts content
table = ::Text::Table.new
table.head = header
table.rows = rows
table.to_s
end
def rows
file.methods.map do |method|
"#{file.class_name}\t#{method.prefix}#{method.name}\t#{method.complexity}"
file.object_candidates.map do |candidate|
[file.class_name, candidate.join(", ")]
end
end
......@@ -24,4 +29,3 @@ module Formatters
end
end
......@@ -27,8 +27,27 @@ module Snuffle
}
end
def class_name
return @class_name if @class_name
@class_name = find_class(ast) || "?"
end
private
def text_at(start_pos, end_pos)
source[start_pos..end_pos - 1]
end
def find_class(node)
return unless node.respond_to?(:type)
concat = []
if node.type == :module || node.type == :class
concat << text_at(node.loc.name.begin_pos, node.loc.name.end_pos)
end
concat << node.children.map{|child| find_class(child)}.compact
concat.flatten.select(&:present?).join('::')
end
def cohorts
CohortDetector.new(self.nodes).cohorts
end
......
......@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "thor"
spec.add_dependency "ephemeral", "~> 2.3.2"
spec.add_dependency "poro_plus"
spec.add_dependency "text-table"
spec.add_development_dependency "bundler", "~> 1.6"
spec.add_development_dependency "rake"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment