Skip to content

Commit 18c4464

Browse files
committed
fix cli and csv output
1 parent b57cc9d commit 18c4464

File tree

8 files changed

+81
-107
lines changed

8 files changed

+81
-107
lines changed

lib/problematic_variable_finder/formatters/cli_formatter.rb

+4-15
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,17 @@
33
module ProblematicVariableFinder
44
module Formatters
55
class CliFormatter
6-
attr_reader :gem_name, :problems
6+
attr_reader :problems
77

8-
def initialize(gem_name, problems)
9-
@gem_name = gem_name
8+
def initialize(problems)
109
@problems = problems
1110
end
1211

1312
def call
14-
problems.each do |path, (full_path, file_problems)|
15-
puts
16-
17-
file_problems.each do |problem|
18-
display_problem(full_path, path, problem)
19-
end
13+
problems.each do |problem|
14+
DisplayCliProblem.new(problem).call
2015
end
2116
end
22-
23-
private
24-
25-
def display_problem(full_path, path, problem)
26-
DisplayCliProblem.new(gem_name, full_path, path, problem).call
27-
end
2817
end
2918
end
3019
end

lib/problematic_variable_finder/formatters/csv.rb

+24-18
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,39 @@ module ProblematicVariableFinder
66
module Formatters
77
class Csv < CliFormatter
88
def call
9-
write_mode = File.exist?(csv_filename) ? "a" : "w"
10-
119
CSV.open(csv_filename, "#{write_mode}b") do |csv|
12-
csv << [
13-
'github_link',
14-
'gem_name',
15-
'gem_version',
16-
'location',
17-
'type',
18-
'code'
19-
]
20-
21-
problems.each do |gem_with_version, gem_problems|
22-
puts
23-
24-
gem_problems.each do |problem|
25-
csv << display_problem(problem)
26-
end
10+
unless exists?
11+
csv << [
12+
'github_link',
13+
'gem_name',
14+
'gem_version',
15+
'out_of_date',
16+
'location',
17+
'type',
18+
'code'
19+
]
20+
end
21+
22+
problems.compact.each do |problem|
23+
csv << display_problem(problem)
2724
end
2825

2926
csv
3027
end
3128
end
3229

33-
3430
private
3531

32+
def write_mode
33+
exists? ? "a" : "w"
34+
end
35+
36+
def exists?
37+
return @exists if defined?(@exists)
38+
39+
@exists = File.exist?(csv_filename)
40+
end
41+
3642
def csv_filename
3743
"problematic_variables.csv"
3844
end

lib/problematic_variable_finder/formatters/display_cli_problem.rb

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
module ProblematicVariableFinder
22
module Formatters
33
class DisplayCliProblem
4-
attr_reader :gem_name, :full_path, :path, :problem
4+
attr_reader :problem
55

6-
def initialize(gem_name, full_path, path, problem)
7-
@gem_name = gem_name
8-
@full_path = full_path
9-
@path = path
6+
def initialize(problem)
107
@problem = problem
118
end
129

@@ -56,7 +53,7 @@ def snippet
5653
name.to_s
5754
when Parser::AST::Node, ProblematicVariableFinder::Parsing::SexpWrapper
5855
if name.respond_to?(:loc)
59-
file_contents[name.loc.expression.begin_pos..name.loc.expression.end_pos]
56+
file_contents[name.loc.expression.begin_pos..name.loc.expression.end_pos]
6057
else
6158
name.to_s
6259
end
@@ -65,9 +62,9 @@ def snippet
6562

6663
def print_problem_header
6764
puts '-----------------'
68-
puts "#{gem_name}"
69-
puts "#{path}:#{line_number}"
70-
puts "#{problem[:type]}: #{problem[:name]}"
65+
puts "#{problem.gem_name}"
66+
puts "#{problem.filename}:#{problem.line_number}"
67+
puts "#{problem.type}: #{problem.code}"
7168
puts
7269
end
7370
end

lib/problematic_variable_finder/formatters/display_csv_problem.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def call
1111
problem.gem_name,
1212
problem.gem_version,
1313
problem.out_of_date,
14-
"#{problem.path}:#{problem.line_number}",
14+
"#{problem.filename}:#{problem.line_number}",
1515
problem.type,
1616
problem.code
1717
]

lib/problematic_variable_finder/gem_problems.rb

+18-29
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,26 @@ def problems
1515
end
1616

1717
def determine_problems
18-
problems = {}
19-
20-
gems.each do |name, version|
18+
gems.flat_map do |name, version|
2119
next if ignore_gem?(name)
2220
next if exclude_because_of_only_list?(name)
2321

24-
key = "#{name}-#{version}"
22+
key = "#{name}-#{version}-cache-bust-1"
2523

2624
gem_problems = cache(key) do
2725
find_gem_problems(name, version)
2826
end
2927

30-
gem_problems = objectify(gem_problems)
31-
32-
problems[key] = gem_problems if gem_problems.any?
28+
objectify(name, version, gem_problems)
3329
end
34-
35-
puts problems
36-
37-
problems
3830
end
3931

40-
def objectify(gem_problems)
41-
gem_problems.flat_map do |filename, file_problems|
42-
file_problems.map do |problem|
43-
Problem.new(
44-
gem_name: name,
45-
gem_version: version,
46-
type: problem[:type],
47-
filename: filename,
48-
line_number: problem[:line_number],
49-
code: problem[:name].to_s,
50-
out_of_date: outdated_gems.include?(name)
51-
)
52-
end
32+
def objectify(name, version, gem_problems)
33+
gem_problems.map do |problem|
34+
problem.gem_name = name
35+
problem.gem_version = version
36+
problem.out_of_date = outdated_gems.include?(name)
37+
problem
5338
end
5439
end
5540

@@ -74,19 +59,23 @@ def ignore_list
7459
@ignore_list ||= ProblematicVariableFinder.read_file(File.expand_path('DEFAULT_IGNORED_GEMS', __dir__)).split("\n").map(&:strip)
7560
end
7661

77-
def outdated_gems
62+
def outdated_gems
63+
byebug
7864
@outdated_gems ||= outdated.map{|o| o.gsub(/\s+\*\s+/, '').split(" ").first }
7965
end
8066

8167
def outdated
82-
@outdated ||= cache('BUNDLE_OUT_OF_DATE_INFO') do
83-
`bundle outdated`.split("\n").grep(/ \*/).reject do |s|
84-
s['development'] ||
85-
s['test']
68+
@outdated ||= cache("BUNDLE_OUT_OF_DATE_INFO_#{gemfile_lock_sha}") do
69+
`bundle outdated --group default --group production`.split("\n").map do |s|
70+
s.split(" ").map(&:strip).first
8671
end
8772
end
8873
end
8974

75+
def gemfile_lock_sha
76+
Digest::SHA1.hexdigest(ProblematicVariableFinder.read_file('Gemfile.lock'))
77+
end
78+
9079
def find_gem_problems(name, version)
9180
directory = "#{gem_path}/#{name}-#{version}/"
9281
folder = gem_path + '/' + [name, version].join('-') + '/'

lib/problematic_variable_finder/problem.rb

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Problem
33
attr_accessor \
44
:gem_name,
55
:gem_version,
6-
:file_name,
6+
:filename,
77
:line_number,
88
:out_of_date,
99
:type,
@@ -19,16 +19,15 @@ def github_link
1919
@github_link ||=
2020
begin
2121
if source_code_uri
22-
"#{source_code_uri}/#{file_name}:#{line_number}"
22+
"#{source_code_uri}/#{filename}##{line_number}"
2323
else
24-
"#{gem_name} #{file_name}:#{line_number}"
24+
"#{gem_name} #{filename}##{line_number}"
2525
end
2626
end
2727
end
2828

2929
def source_code_uri
30-
gem_spec&.metadata['source_code_uri']
31-
30+
gem_spec&.metadata&.fetch('source_code_uri', nil)
3231
end
3332

3433
def gem_spec

lib/problematic_variable_finder/problem_finder.rb

+6-15
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,24 @@ def find_problems_in_directory(path, remove_paths=[])
3333
end
3434
end
3535

36-
directory_problems = {}
37-
38-
files.each do |f|
39-
puts f
40-
full_path, path, problems = find_file_problems(f, remove_paths)
41-
problems.map! do |problem|
36+
files.flat_map do |f|
37+
_, path, problems = find_file_problems(f, remove_paths)
38+
problems.map do |problem|
4239
Problem.new(
43-
gem_name: name,
44-
gem_version: version,
4540
type: problem[:type],
46-
filename: filename,
41+
filename: path,
4742
line_number: problem[:line_number],
48-
code: problem[:name].to_s,
49-
out_of_date: outdated_gems.include?(name)
43+
code: problem[:name].to_s
5044
)
5145
end
52-
directory_problems[path] = [full_path, problems] if problems.any?
5346
end
54-
55-
directory_problems
5647
end
5748

5849
def find_file_problems(f, remove_paths)
5950
full_path = File.expand_path f
6051
friendly_path = full_path
6152
remove_paths.each do |p|
62-
friendly_path = f.gsub(p, '')
53+
friendly_path = friendly_path.gsub(p, '')
6354
end
6455

6556
problems = begin

lib/problematic_variable_finder/runner.rb

+18-15
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
require 'problematic_variable_finder/problem_finder'
1010
require 'problematic_variable_finder/formatters/cli_formatter'
1111
require 'problematic_variable_finder/formatters/csv'
12+
require 'problematic_variable_finder/fs_caching'
1213

1314
module ProblematicVariableFinder
1415
class Runner
16+
include FsCaching
1517
def self.call
1618
new.call
1719
end
1820

1921
def call
20-
display_problems('main app problems', main_problems)
22+
display_problems(main_problems)
2123
display_gem_problems
2224

2325
if gem_problems.outdated_gems.any?
@@ -29,35 +31,36 @@ def call
2931
end
3032

3133
def display_gem_problems
32-
each_gem_problem do |gem_name, problems, out_of_date|
33-
if options[:verbose]
34-
display_problems(gem_name, problems)
35-
else
36-
puts '-----------------'
37-
puts "#{gem_name} #{out_of_date ? '(out of date)' : ''} #{problems.flatten.length} possible issues"
38-
end
34+
if options[:verbose]
35+
display_problems(gem_problems.problems)
36+
else
37+
puts '-----------------'
38+
puts "#{gem_name} #{out_of_date ? '(out of date)' : ''} #{problems.flatten.length} possible issues"
3939
end
4040
end
4141

42-
def display_problems(gem_name, problems)
42+
def display_problems(problems)
4343
klass = case options[:format]
4444
when :csv
4545
Formatters::Csv
4646
else
4747
Formatters::CliFormatter
4848
end
4949

50-
klass.new(gem_name, problems).call
50+
klass.new(problems).call
5151
end
5252

53-
def each_gem_problem
54-
gem_problems.problems.each do |gem_name, (problems, out_of_date)|
55-
yield gem_name, problems, out_of_date
53+
def main_problems
54+
cache("main_problems_#{sha_of_all_app_files}") do
55+
app_problems + lib_problems
5656
end
5757
end
5858

59-
def main_problems
60-
app_problems.merge(lib_problems)
59+
def sha_of_all_app_files
60+
app = Dir["app/**/*.rb"]
61+
lib = Dir["lib/**/*.rb"]
62+
contents = (app + lib).map { |f| (ProblematicVariableFinder.read_file(f)) }.join
63+
Digest::SHA1.hexdigest(contents)
6164
end
6265

6366
def app_problems

0 commit comments

Comments
 (0)