-
-
Notifications
You must be signed in to change notification settings - Fork 61
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -39,6 +40,7 @@ def self.default_options | |
:liberal => false, | ||
:fuzzy => false, | ||
:only => nil, | ||
:report => false | ||
} | ||
end | ||
|
||
|
@@ -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 "" | ||
|
@@ -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] | ||
io.puts | ||
|
||
self.summary.sort_by { |_,v| -v }.each do |file, score| | ||
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same
|
||
|
||
item.locations.each_with_index do |loc, i| | ||
loc_prefix = "%s: " % (?A.ord + i).chr if option[:diff] | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please follow my coding conventions There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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