Skip to content

Commit f03285b

Browse files
committed
add CSV support
1 parent 5d9bc86 commit f03285b

7 files changed

Lines changed: 81 additions & 40 deletions

File tree

lib/problematic_variable_finder.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,42 @@ def read_file(path)
1919
@files ||= {}
2020
@files[path] ||= File.read(path)
2121
end
22+
23+
def options
24+
@options ||= parse_options
25+
end
26+
27+
private
28+
29+
def parse_options
30+
options = {}
31+
32+
OptionParser.new do |opts|
33+
opts.banner = "Usage: #{__FILE__} [options]"
34+
35+
opts.on("-fFORMAT", "--format=FORMAT", "output format default stdout, options [csv]") do |f|
36+
options[:format] = (f || 'stdout').to_sym
37+
end
38+
39+
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
40+
options[:verbose] = v
41+
end
42+
43+
opts.on("-d", "--directory", "Directory to find app in") do |d|
44+
options[:directory] = d
45+
end
46+
47+
opts.on("-i", "--ignore rails,activerecord", Array, "Ignore gems") do |i|
48+
options[:ignore] = i
49+
end
50+
51+
opts.on("-g", "--gems rails,activerecord", Array, "List of gems") do |g|
52+
options[:gems] = g
53+
end
54+
end.parse!
55+
56+
options
57+
end
58+
2259
end
2360
end

lib/problematic_variable_finder/formatters/cli_formatter.rb

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

1313
def call
14+
csv = CSV.new(STDOUT)
15+
csv << ['gem', 'location', 'type', 'value']
16+
1417
problems.each do |path, (full_path, file_problems)|
1518
puts
1619

1720
file_problems.each do |problem|
18-
display_problem(full_path, path, problem)
21+
csv << display_problem(full_path, path, problem)
1922
end
2023
end
2124
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'problematic_variable_finder/formatters/display_cli_problem'
2+
3+
module ProblematicVariableFinder
4+
module Formatters
5+
class Csv < CliFormatter
6+
private
7+
8+
def display_problem(full_path, path, problem)
9+
DisplayCsvProblem.new(gem_name, full_path, path, problem).call
10+
end
11+
end
12+
end
13+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module ProblematicVariableFinder
2+
module Formatters
3+
class DisplayCsvProblem < DisplayCliProblem
4+
def call
5+
[gem_name, "#{path}:#{line_number}", problem[:type], problem[:name].to_s]
6+
end
7+
end
8+
end
9+
end

lib/problematic_variable_finder/fs_caching.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ def store
55
end
66

77
def cache(key)
8-
return yield
9-
10-
case @in_transaction
8+
case @in_transaction
119
when true
1210
store[key] ||= yield
1311
else

lib/problematic_variable_finder/gem_problems.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ module ProblematicVariableFinder
22
class GemProblems
33
include FsCaching
44

5-
def initialize(gem_path, gems, options)
5+
def initialize(gem_path, gems)
66
@gem_path, @gems = gem_path, gems
7-
@options = options
87
end
98

10-
attr_reader :gem_path, :gems, :options
9+
attr_reader :gem_path, :gems
1110

1211
def problems
1312
@problems ||= determine_problems
@@ -51,6 +50,8 @@ def ignore_gem?(name)
5150
options[:ignore] && Array(options[:ignore]).include?(name)
5251
end
5352

53+
delegate :options, to: ProblematicVariableFinder
54+
5455
def ignore_list
5556
@ignore_list ||= ProblematicVariableFinder.read_file(File.expand_path('DEFAULT_IGNORED_GEMS', __dir__)).map(&:strip)
5657
end

lib/problematic_variable_finder/runner.rb

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require 'problematic_variable_finder/gem_problems'
99
require 'problematic_variable_finder/problem_finder'
1010
require 'problematic_variable_finder/formatters/cli_formatter'
11+
require 'problematic_variable_finder/formatters/csv'
1112

1213
module ProblematicVariableFinder
1314
class Runner
@@ -39,7 +40,14 @@ def display_gem_problems
3940
end
4041

4142
def display_problems(gem_name, problems)
42-
Formatters::CliFormatter.new(gem_name, problems).call
43+
klass = case options[:format]
44+
when :csv
45+
Formatters::Csv
46+
else
47+
Formatters::CliFormatter
48+
end
49+
50+
klass.new(gem_name, problems).call
4351
end
4452

4553
def each_gem_problem
@@ -52,11 +60,11 @@ def main_problems
5260
app_problems.merge(lib_problems)
5361
end
5462

55-
def app_problems
63+
def app_problems
5664
problem_finder.find_problems_in_directory("app")
5765
end
5866

59-
def lib_problems
67+
def lib_problems
6068
problem_finder.find_problems_in_directory("lib")
6169
end
6270

@@ -73,41 +81,13 @@ def gems
7381
end
7482

7583
def gem_problems
76-
@gem_problems ||= GemProblems.new(gem_path, gems, options)
84+
@gem_problems ||= GemProblems.new(gem_path, gems)
7785
end
7886

7987
def problem_finder
8088
@problem_finder ||= ProblemFinder.new
8189
end
8290

83-
def options
84-
@options ||= parse_options
85-
end
86-
87-
def parse_options
88-
options = {}
89-
90-
OptionParser.new do |opts|
91-
opts.banner = "Usage: #{__FILE__} [options]"
92-
93-
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
94-
options[:verbose] = v
95-
end
96-
97-
opts.on("-d", "--directory", "Directory to find app in") do |d|
98-
options[:directory] = d
99-
end
100-
101-
opts.on("-i", "--ignore rails,activerecord", Array, "Ignore gems") do |i|
102-
options[:ignore] = i
103-
end
104-
105-
opts.on("-g", "--gems rails,activerecord", Array, "List of gems") do |g|
106-
options[:gems] = g
107-
end
108-
end.parse!
109-
110-
options
111-
end
91+
delegate :options, to: ProblematicVariableFinder
11292
end
11393
end

0 commit comments

Comments
 (0)