code_quality 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/code_quality.gemspec +4 -1
- data/config/rubocop-github.yml +5 -0
- data/config/rubocop-rails.yml +3 -0
- data/lib/code_quality/version.rb +1 -1
- data/lib/tasks/code_quality.rake +118 -1
- metadata +51 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0468a907d1a5093eb7a55189993c91b7db4dc35c'
|
4
|
+
data.tar.gz: d976a2903996a791e5fc83596194f93813ed819f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0987639e46edab9cb64d36c1231fa8b0c34f0db415442a1db5b6da4b899d7c71402bef53e102bf19361345756dc954da9c2015ec31b989313a5157935862d23
|
7
|
+
data.tar.gz: 62f47cffc1f28847bb182edf16b150470373337d2e13a2b35c41387d3259dca766426ee16ebc8a4fa117effcd965f071a2c1a4e4468ce5fc17fcc1e0a18f837c
|
data/code_quality.gemspec
CHANGED
@@ -23,7 +23,10 @@ Gem::Specification.new do |spec|
|
|
23
23
|
|
24
24
|
spec.add_dependency "bundler-audit"
|
25
25
|
spec.add_dependency "brakeman"
|
26
|
-
spec.add_dependency "rubycritic"
|
26
|
+
spec.add_dependency "rubycritic", "~> 3.3.0"
|
27
|
+
spec.add_dependency "rubocop", "~> 0.52.0"
|
28
|
+
spec.add_dependency "rubocop-github", "~> 0.8.1"
|
29
|
+
spec.add_dependency "code_metric_fu", "~> 4.14.3"
|
27
30
|
|
28
31
|
spec.add_development_dependency "bundler", "~> 1.16"
|
29
32
|
spec.add_development_dependency "rake", "~> 10.0"
|
data/lib/code_quality/version.rb
CHANGED
data/lib/tasks/code_quality.rake
CHANGED
@@ -58,7 +58,7 @@ namespace :code_quality do
|
|
58
58
|
task :quality_audit => [:"quality_audit:default"] do; end
|
59
59
|
namespace :quality_audit do
|
60
60
|
# default tasks
|
61
|
-
task :default => [:rubycritic] do; end
|
61
|
+
task :default => [:rubycritic, :rubocop, :metric_fu] do; end
|
62
62
|
|
63
63
|
# desc "prepare dir"
|
64
64
|
task :prepare => :helpers do
|
@@ -88,6 +88,110 @@ namespace :code_quality do
|
|
88
88
|
end
|
89
89
|
end
|
90
90
|
end
|
91
|
+
|
92
|
+
desc "rubocop - audit coding style"
|
93
|
+
# e.g.: rake code_quality:quality_audit:rubocop max_offenses=100
|
94
|
+
# options:
|
95
|
+
# config_formula: use which formula for config, supports "github, "rails" or path_to_your_local_config.yml, default is "github"
|
96
|
+
# cli_options: pass extract options, e.g.: cli_options="--show-cops"
|
97
|
+
# max_offenses: if config max_offenses then audit it with detected offenses number in report, e.g.: max_offenses=100
|
98
|
+
task :rubocop => :prepare do
|
99
|
+
run_audit "rubocop - RuboCop is a Ruby static code analyzer. Out of the box it will enforce many of the guidelines outlined in the community Ruby Style Guide." do
|
100
|
+
options = options_from_env(:config_formula, :cli_options, :max_offenses)
|
101
|
+
|
102
|
+
config_formulas = {
|
103
|
+
'github' => 'https://github.com/github/rubocop-github',
|
104
|
+
'rails' => 'https://github.com/rails/rails/blob/master/.rubocop.yml'
|
105
|
+
}
|
106
|
+
|
107
|
+
# prepare cli options
|
108
|
+
config_formula = options.fetch(:config_formula, 'github')
|
109
|
+
if config_formula && File.exists?(config_formula)
|
110
|
+
config_file = config_formula
|
111
|
+
puts "Using config file: #{config_file}"
|
112
|
+
else
|
113
|
+
gem_config_dir = File.expand_path("../../../config", __FILE__)
|
114
|
+
config_file = "#{gem_config_dir}/rubocop-#{config_formula}.yml"
|
115
|
+
puts "Using config formula: [#{config_formula}](#{config_formulas[config_formula]})"
|
116
|
+
end
|
117
|
+
report_path = "#{report_dir}/rubocop-report.html"
|
118
|
+
|
119
|
+
# generate report
|
120
|
+
report = `rubocop -c #{config_file} -S -R -P #{options[:cli_options]} --format offenses --format html -o #{report_path}`
|
121
|
+
puts report
|
122
|
+
puts "Report generated to #{report_path}"
|
123
|
+
show_in_browser File.realpath(report_path)
|
124
|
+
|
125
|
+
# if config max_offenses then audit it with detected offenses number in report
|
126
|
+
if options[:max_offenses]
|
127
|
+
if report.last(20) =~ /(\d+) *Total/
|
128
|
+
detected_offenses = $1.to_i
|
129
|
+
max_offenses = options[:max_offenses].to_i
|
130
|
+
audit_faild "Detected offenses #{colorize(detected_offenses, :yellow)} is more then #{colorize(max_offenses, :yellow)}, must improve your code quality or set a lower #{colorize("max_offenses", :black, :white)}" if detected_offenses > max_offenses
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
desc "metric_fu - many kinds of metrics"
|
137
|
+
# e.g.: rake code_quality:quality_audit:metric_fu metrics=stats,rails_best_practices,roodi rails_best_practices_max_offenses=9 roodi_max_offenses=10
|
138
|
+
# options:
|
139
|
+
# metrics: default to run all metrics, can be config as: cane,churn,flay,flog,hotspots,rails_best_practices,rcov,reek,roodi,saikuro,stats
|
140
|
+
# flay_max_offenses: offenses number for audit
|
141
|
+
# cane_max_offenses: offenses number for audit
|
142
|
+
# rails_best_practices_max_offenses: offenses number for audit
|
143
|
+
# reek_max_offenses: offenses number for audit
|
144
|
+
# roodi_max_offenses: offenses number for audit
|
145
|
+
task :metric_fu => :prepare do
|
146
|
+
metrics_offenses_patterns = {
|
147
|
+
"flay" => /Total Score (\d+)/,
|
148
|
+
"cane" => /Total Violations (\d+)/,
|
149
|
+
"rails_best_practices" => /Found (\d+) errors/,
|
150
|
+
"reek" => /Found (\d+) code smells/,
|
151
|
+
"roodi" => /Found (\d+) errors/,
|
152
|
+
}
|
153
|
+
metrics_have_offenses = metrics_offenses_patterns.keys.map { |metric| "#{metric}_max_offenses".to_sym }
|
154
|
+
options = options_from_env(:metrics, *metrics_have_offenses)
|
155
|
+
run_audit "metric_fu - Code metrics from Flog, Flay, Saikuro, Churn, Reek, Roodi, Code Statistics, and Rails Best Practices. (and optionally RCov)" do
|
156
|
+
report_path = "#{report_dir}/metric_fu"
|
157
|
+
available_metrics = %w{cane churn flay flog hotspots rails_best_practices rcov reek roodi saikuro stats}
|
158
|
+
metric_fu_opts = ""
|
159
|
+
selected_metrics = available_metrics
|
160
|
+
if options[:metrics]
|
161
|
+
selected_metrics = options[:metrics].split(",")
|
162
|
+
disable_metrics = available_metrics - selected_metrics
|
163
|
+
selected_metrics_opt = selected_metrics.map { |m| "--#{m}" }.join(" ")
|
164
|
+
disable_metrics_opt = disable_metrics.map { |m| "--no-#{m}" }.join(" ")
|
165
|
+
metric_fu_opts = "#{selected_metrics_opt} #{disable_metrics_opt}"
|
166
|
+
puts "for metrics: #{selected_metrics.join(",")}"
|
167
|
+
end
|
168
|
+
# geneate report
|
169
|
+
report = `bundle exec metric_fu --no-open #{metric_fu_opts}`
|
170
|
+
FileUtils.remove_dir(report_path) if Dir.exists? report_path
|
171
|
+
FileUtils.mv("tmp/metric_fu/output", report_path, force: true)
|
172
|
+
puts report
|
173
|
+
puts "Report generated to #{report_path}"
|
174
|
+
show_in_browser File.realpath(report_path)
|
175
|
+
|
176
|
+
# audit report result
|
177
|
+
report_result_path = "tmp/metric_fu/report.yml"
|
178
|
+
if File.exists? report_result_path
|
179
|
+
report_result = YAML.load_file(report_result_path)
|
180
|
+
# if config #{metric}_max_offenses then audit it with report result
|
181
|
+
audit_failures = []
|
182
|
+
metrics_offenses_patterns.each do |metric, pattern|
|
183
|
+
option_key = "#{metric}_max_offenses".to_sym
|
184
|
+
if options[option_key]
|
185
|
+
detected_offenses = report_result[metric.to_sym][:total].to_s.match(pattern)[1].to_i rescue 0
|
186
|
+
max_offenses = options[option_key].to_i
|
187
|
+
puts "Metric #{colorize(metric, :green)} detected offenses #{colorize(detected_offenses, :yellow)} is more then #{colorize(max_offenses, :yellow)}, must improve your code quality or set a lower #{colorize(option_key, :black, :white)}" if detected_offenses > max_offenses
|
188
|
+
audit_failures << {metric: metric, detected_offenses: detected_offenses, max_offenses: max_offenses}
|
189
|
+
end
|
190
|
+
end
|
191
|
+
audit_faild "#{audit_failures.size} of #{selected_metrics.size} metrics audit failed" if audit_failures.any?
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
91
195
|
end
|
92
196
|
|
93
197
|
# desc "helper methods"
|
@@ -147,6 +251,19 @@ namespace :code_quality do
|
|
147
251
|
ansi = cov.call(ansi, bg, bgcode) if bg.to_s != "default"
|
148
252
|
ansi
|
149
253
|
end
|
254
|
+
|
255
|
+
def show_in_browser(dir)
|
256
|
+
require "launchy"
|
257
|
+
uri = URI.escape("file://#{dir}/")
|
258
|
+
if File.directory?(dir)
|
259
|
+
uri = URI.join(uri, "index.html")
|
260
|
+
end
|
261
|
+
Launchy.open(uri) if open_in_browser?
|
262
|
+
end
|
263
|
+
|
264
|
+
def open_in_browser?
|
265
|
+
ENV["CI"].nil?
|
266
|
+
end
|
150
267
|
end
|
151
268
|
|
152
269
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code_quality
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- RainChen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler-audit
|
@@ -42,16 +42,58 @@ dependencies:
|
|
42
42
|
name: rubycritic
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 3.3.0
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 3.3.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.52.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.52.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-github
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.8.1
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.8.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: code_metric_fu
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 4.14.3
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 4.14.3
|
55
97
|
- !ruby/object:Gem::Dependency
|
56
98
|
name: bundler
|
57
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -111,6 +153,8 @@ files:
|
|
111
153
|
- bin/console
|
112
154
|
- bin/setup
|
113
155
|
- code_quality.gemspec
|
156
|
+
- config/rubocop-github.yml
|
157
|
+
- config/rubocop-rails.yml
|
114
158
|
- lib/code_quality.rb
|
115
159
|
- lib/code_quality/railtie.rb
|
116
160
|
- lib/code_quality/version.rb
|
@@ -140,3 +184,4 @@ signing_key:
|
|
140
184
|
specification_version: 4
|
141
185
|
summary: run code quality and security audit report with one rake task
|
142
186
|
test_files: []
|
187
|
+
has_rdoc:
|