Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add json output option #57

Closed
wants to merge 4 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 85 additions & 7 deletions lib/flay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "sexp_processor"
require "ruby_parser"
require "timeout"
require "json"

class File
RUBY19 = "<3".respond_to? :encoding unless defined? RUBY19 # :nodoc:
Expand Down Expand Up @@ -39,6 +40,7 @@ def self.default_options
:liberal => false,
:fuzzy => false,
:only => nil,
:report => false
}
end

Expand Down Expand Up @@ -100,6 +102,10 @@ def self.parse_options args = ARGV
options[:timeout] = t.to_i
end

opts.on("-r", "--report", "Format report as json") do
options[:report] = true
end

extensions = ["rb"] + Flay.load_plugins

opts.separator ""
Expand Down Expand Up @@ -498,17 +504,74 @@ def summary
score
end

##
# Output the report. Duh.
def report_json io, data
json = {}
json[:total] = self.total
clones = []

def report io = $stdout
only = option[:only]
if option[:summary]
summary = []
self.summary.sort_by { |_,v| -v }.each do |file, score|
file_json = {}
file_json[:score] = "%8.2f" % [score]
file_json[:filename] = "%s" % [file]
summary.push(file_json)
end
json[:summary] = summary
else
data.each_with_index do |item, count|
clone = {}
prefix = "%d" % (count + 1) if option[:number]
clone[:prefix] = prefix

data = analyze only
match = item.identical? ? "IDENTICAL" : "Similar"
clone[:match] = match

clone[:mass] = item.mass
clone[:bonus] = item.bonus unless item.bonus.nil?
clone[:name] = item.name
files = []

item.locations.each_with_index do |loc, i|
file = {}

extra = "FUZZY" if loc.fuzzy?

file[:filename] = loc.file
file[:line] = loc.line
file[:extra] = extra unless extra.nil?

if option[:diff] then
nodes = hashes[item.structural_hash]
node = nodes[i]

source = begin
msg = "sexp_to_#{File.extname(node.file).sub(/./, "")}"
self.respond_to?(msg) ? self.send(msg, node) : sexp_to_rb(node)
end

file[:contents] = source.split("\n")
end

files.push(file)
end

clone[:files] = files

clones.push(clone)

json[:clones] = clones

end
end

io.puts json.to_json
end

def report_io io, data
io.puts "Total score (lower is better) = #{self.total}"

if option[:summary] then
if option[:summary]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't reformat my code please.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are totally right, didn't notice. Going to fix

io.puts

self.summary.sort_by { |_,v| -v }.each do |file, score|
Expand All @@ -525,7 +588,7 @@ def report io = $stdout

io.puts
io.puts "%s%s code found in %p (mass%s = %d)" %
[prefix, match, item.name, item.bonus, item.mass]
[prefix, match, item.name, item.bonus, item.mass]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

you are totally right, didn't notice. Going to fix


item.locations.each_with_index do |loc, i|
loc_prefix = "%s: " % (?A.ord + i).chr if option[:diff]
Expand All @@ -548,6 +611,21 @@ def report io = $stdout
end
end

##
# Output the report. Duh.

def report io = $stdout
only = option[:only]

data = analyze only

if option[:report]
report_json(io,data)
else
report_io(io,data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please follow my coding conventions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what you mean. Didn't see anything about conventions, and I'm not used to ruby and to its best code style practices. If you can specify the problem I will fix it in no time

end
end

def sexp_to_rb sexp
begin
require "ruby2ruby"
Expand Down