Skip to content

Commit a079d97

Browse files
committed
Merge pull request #258 from MGotink/use_reek_directly
Use reek directly
2 parents 9d4f9d9 + 115c5f1 commit a079d97

File tree

4 files changed

+103
-199
lines changed

4 files changed

+103
-199
lines changed

HISTORY.md

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ As such, a _Feature_ would map to either major (breaking change) or minor. A _bu
88

99
* Breaking Changes
1010
* Features
11+
* Add line numbers to reek output. (ggallen, #255)
12+
* Use reek directly. (Martin Gotink, #258)
13+
* Add support for reek 2. (Martin Gotink, #258)
1114
* Fixes
1215
* Use same styling for covered as ignored lines. (Martin Gotink, #254)
1316
* Misc
+23-80
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module MetricFu
22
class ReekGenerator < Generator
3-
REEK_REGEX = /^(\S+) (.*) \((.*)\)$/
4-
53
def self.metric
64
:reek
75
end
@@ -12,34 +10,25 @@ def emit
1210
mf_log "Skipping Reek, no files found to analyze"
1311
@output = ""
1412
else
15-
args = cli_options(files)
16-
@output = run!(args)
17-
@output = massage_for_reek_12 if reek_12?
13+
@output = run!(files, config_files)
1814
end
1915
end
2016

21-
def run!(args)
17+
def run!(files, config_files)
18+
require "reek"
19+
# To load any changing dependencies such as "reek/configuration/app_configuration"
20+
# Added in 1.6.0 https://github.com/troessner/reek/commit/7f4ed2be442ca926e08ccc41945e909e8f710947
21+
# But not always loaded
2222
require "reek/cli/application"
2323

24-
MetricFu::Utility.capture_output do
25-
Reek::Cli::Application.new(args).execute
26-
end
24+
examiner = Reek.const_defined?(:Examiner) ? Reek.const_get(:Examiner) : Reek.const_get(:Core).const_get(:Examiner)
25+
examiner.new(files, config_files)
2726
end
2827

2928
def analyze
30-
@matches = @output.chomp.split("\n\n").map { |m| m.split("\n") }
31-
@matches = @matches.map do |match|
32-
break {} if zero_warnings?(match)
33-
file_path = match.shift.split(" -- ").first
34-
file_path = file_path.gsub('"', " ").strip
35-
code_smells = match.map do |smell|
36-
match_object = smell.match(REEK_REGEX)
37-
next unless match_object
38-
{ method: match_object[1].strip,
39-
message: match_object[2].strip,
40-
type: match_object[3].strip }
41-
end.compact
42-
{ file_path: file_path, code_smells: code_smells }
29+
@matches = @output.smells.group_by(&:source).collect do |file_path, smells|
30+
{ file_path: file_path,
31+
code_smells: analyze_smells(smells) }
4332
end
4433
end
4534

@@ -67,31 +56,6 @@ def per_file_info(out)
6756
end
6857
end
6958

70-
def reek_12?
71-
return false if @output.length == 0
72-
(@output =~ /^"/) != 0
73-
end
74-
75-
def massage_for_reek_12
76-
section_break = ""
77-
@output.split("\n").map do |line|
78-
case line
79-
when /^ /
80-
"#{line.gsub(/^ /, '')}\n"
81-
else
82-
parts = line.split(" -- ")
83-
if parts[1].nil?
84-
"#{line}\n"
85-
else
86-
warnings = parts[1].gsub(/ \(.*\):/, ":")
87-
result = "#{section_break}\"#{parts[0]}\" -- #{warnings}\n"
88-
section_break = "\n"
89-
result
90-
end
91-
end
92-
end.join
93-
end
94-
9559
private
9660

9761
def files_to_analyze
@@ -100,47 +64,26 @@ def files_to_analyze
10064
remove_excluded_files(files_to_reek)
10165
end
10266

103-
def cli_options(files)
104-
[
105-
disable_line_number_option,
106-
turn_off_color,
107-
*config_option,
108-
*files
109-
].reject(&:empty?)
110-
end
111-
11267
# TODO: Check that specified line config file exists
113-
def config_option
114-
config_file_pattern = options[:config_file_pattern]
115-
if config_file_pattern.to_s.empty?
116-
[""]
117-
else
118-
["--config", config_file_pattern]
119-
end
68+
def config_files
69+
Array(options[:config_file_pattern])
12070
end
12171

122-
# Work around "Error: invalid option: --no-color" in reek < 1.3.7
123-
def turn_off_color
124-
if reek_version >= "1.3.7"
125-
"--no-color"
126-
else
127-
""
128-
end
72+
def analyze_smells(smells)
73+
smells.collect(&method(:smell_data))
12974
end
13075

131-
def reek_version
132-
@reek_version ||= `reek --version`.chomp.sub(/\s*reek\s*/, "")
133-
# use the above, as the below may activate a version not available in
134-
# a Bundler context
135-
# MetricFu::GemVersion.activated_version('reek').to_s
76+
def smell_data(smell)
77+
{ method: smell.context,
78+
message: smell.message,
79+
type: smell_type(smell),
80+
lines: smell.lines }
13681
end
13782

138-
def disable_line_number_option
139-
"--no-line-numbers"
140-
end
83+
def smell_type(smell)
84+
return smell.subclass if smell.respond_to?(:subclass)
14185

142-
def zero_warnings?(match)
143-
match.last == "0 total warnings"
86+
smell.smell_type
14487
end
14588
end
14689
end

metric_fu.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Gem::Specification.new do |s|
4545
s.add_runtime_dependency "flay", [">= 2.0.1", "~> 2.1"]
4646
s.add_runtime_dependency "churn", ["~> 0.0.35"]
4747
s.add_runtime_dependency "flog", [">= 4.1.1", "~> 4.1"]
48-
s.add_runtime_dependency "reek", [">= 1.3.4", "~> 1.3"]
48+
s.add_runtime_dependency "reek", [">= 1.3.4", "< 3.0"]
4949
s.add_runtime_dependency "cane", [">= 2.5.2", "~> 2.5"]
5050
s.add_runtime_dependency "rails_best_practices", [">= 1.14.3", "~> 1.14"]
5151
s.add_runtime_dependency "metric_fu-Saikuro", [">= 1.1.3", "~> 1.1"]

0 commit comments

Comments
 (0)