Skip to content

Commit b57cc9d

Browse files
committed
fix csv generation
1 parent d458d8d commit b57cc9d

File tree

6 files changed

+98
-27
lines changed

6 files changed

+98
-27
lines changed

lib/problematic_variable_finder/formatters/cli_formatter.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@ def initialize(gem_name, problems)
1111
end
1212

1313
def call
14-
csv = CSV.new(STDOUT)
15-
csv << ['gem', 'location', 'type', 'value']
16-
1714
problems.each do |path, (full_path, file_problems)|
1815
puts
1916

2017
file_problems.each do |problem|
21-
csv << display_problem(full_path, path, problem)
18+
display_problem(full_path, path, problem)
2219
end
2320
end
2421
end

lib/problematic_variable_finder/formatters/csv.rb

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
1-
require 'problematic_variable_finder/formatters/display_cli_problem'
1+
require 'problematic_variable_finder/formatters/cli_formatter'
2+
require 'problematic_variable_finder/formatters/display_csv_problem'
3+
require 'csv'
24

35
module ProblematicVariableFinder
46
module Formatters
57
class Csv < CliFormatter
8+
def call
9+
write_mode = File.exist?(csv_filename) ? "a" : "w"
10+
11+
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
27+
end
28+
29+
csv
30+
end
31+
end
32+
33+
634
private
735

8-
def display_problem(full_path, path, problem)
9-
DisplayCsvProblem.new(gem_name, full_path, path, problem).call
36+
def csv_filename
37+
"problematic_variables.csv"
38+
end
39+
40+
def display_problem(problem)
41+
DisplayCsvProblem.new(problem).call
1042
end
1143
end
1244
end

lib/problematic_variable_finder/formatters/display_csv_problem.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
module ProblematicVariableFinder
22
module Formatters
33
class DisplayCsvProblem < DisplayCliProblem
4+
def initialize(problem)
5+
@problem = problem
6+
end
7+
48
def call
5-
[gem_name, "#{path}:#{line_number}", problem[:type], problem[:name].to_s]
9+
[
10+
problem.github_link,
11+
problem.gem_name,
12+
problem.gem_version,
13+
problem.out_of_date,
14+
"#{problem.path}:#{problem.line_number}",
15+
problem.type,
16+
problem.code
17+
]
618
end
719
end
820
end

lib/problematic_variable_finder/gem_problems.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'problematic_variable_finder/problem'
2+
13
module ProblematicVariableFinder
24
class GemProblems
35
include FsCaching
@@ -25,16 +27,32 @@ def determine_problems
2527
find_gem_problems(name, version)
2628
end
2729

28-
gem_is_out_of_date = outdated_gems.include?(name)
30+
gem_problems = objectify(gem_problems)
2931

30-
problems[key] = [gem_problems, gem_is_out_of_date] if gem_problems.any?
32+
problems[key] = gem_problems if gem_problems.any?
3133
end
3234

3335
puts problems
3436

3537
problems
3638
end
3739

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
53+
end
54+
end
55+
3856
def exclude_because_of_only_list?(name)
3957
return false unless options[:gems]
4058

lib/problematic_variable_finder/problem_finder.rb

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'problematic_variable_finder/main_finder'
2+
require 'problematic_variable_finder/problem'
23

34
module ProblematicVariableFinder
45
class ProblemFinder
@@ -7,15 +8,15 @@ class ProblemFinder
78
def find_problems_in_directory(path, remove_paths=[])
89
key = [path, remove_paths].inspect
910

10-
files = Dir.glob("#{path}/**/*.rb")
11+
files = Dir.glob("#{path}/**/*.rb")
1112

12-
files.reject! do |f|
13-
filename = f
14-
filename = remove_paths.each do |path|
15-
filename = filename.gsub(path, '')
16-
end
13+
files.reject! do |f|
14+
filename = f
15+
filename = remove_paths.each do |path|
16+
filename = filename.gsub(path, '')
17+
end
1718

18-
%w(
19+
%w(
1920
/spec/
2021
/.bundle/
2122
/.gems/
@@ -27,20 +28,31 @@ def find_problems_in_directory(path, remove_paths=[])
2728
/vendor/
2829
_spec.rb
2930
_test.rb
30-
).any? do |s|
31-
filename.include?(s)
32-
end
31+
).any? do |s|
32+
filename.include?(s)
3333
end
34+
end
3435

35-
directory_problems = {}
36+
directory_problems = {}
3637

37-
files.each do |f|
38-
puts f
39-
full_path, path, problems = find_file_problems(f, remove_paths)
40-
directory_problems[path] = [full_path, problems] if problems.any?
38+
files.each do |f|
39+
puts f
40+
full_path, path, problems = find_file_problems(f, remove_paths)
41+
problems.map! do |problem|
42+
Problem.new(
43+
gem_name: name,
44+
gem_version: version,
45+
type: problem[:type],
46+
filename: filename,
47+
line_number: problem[:line_number],
48+
code: problem[:name].to_s,
49+
out_of_date: outdated_gems.include?(name)
50+
)
4151
end
52+
directory_problems[path] = [full_path, problems] if problems.any?
53+
end
4254

43-
directory_problems
55+
directory_problems
4456
end
4557

4658
def find_file_problems(f, remove_paths)

lib/problematic_variable_finder/runner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def call
3030

3131
def display_gem_problems
3232
each_gem_problem do |gem_name, problems, out_of_date|
33-
if options[:verbose]
33+
if options[:verbose]
3434
display_problems(gem_name, problems)
3535
else
3636
puts '-----------------'

0 commit comments

Comments
 (0)