lib/quality/rake/task.rb in quality-1.3.0 vs lib/quality/rake/task.rb in quality-1.3.1
- old
+ new
@@ -2,10 +2,15 @@
require 'rake'
require 'rake/tasklib'
require 'rbconfig'
require_relative '../quality_checker'
+require_relative '../tools/cane'
+require_relative '../tools/flay'
+require_relative '../tools/flog'
+require_relative '../tools/reek'
+require_relative '../tools/rubocop'
module Quality
#
# Defines a task library for running quality's various tools
#
@@ -22,10 +27,16 @@
#
# This will create a task that can be run with:
#
# rake quality
class Task < ::Rake::TaskLib
+ include Tools::Cane
+ include Tools::Flay
+ include Tools::Flog
+ include Tools::Reek
+ include Tools::Rubocop
+
# Name of quality task.
# Defaults to :quality.
attr_accessor :quality_name
# Name of ratchet task.
@@ -49,14 +60,33 @@
# Defaults to .
attr_writer :output_dir
# Defines a new task, using the name +name+.
def initialize(args = {})
+ parse_args(args)
+
+ @skip_tools = []
+
+ @output_dir = '.'
+
+ yield self if block_given?
+
+ define
+ end
+
+ def parse_task_name_args(args)
@quality_name = args[:quality_name] || 'quality'
@ratchet_name = args[:ratchet_name] || 'ratchet'
+ end
+ def parse_args(args)
+ parse_task_name_args(args)
+ parse_unit_test_overrides(args)
+ end
+
+ def parse_unit_test_overrides(args)
# allow unit tests to override the class that Rake DSL
# messages are sent to.
@dsl = args[:dsl] || ::Rake::Task
# likewise, but for system()
@@ -79,18 +109,10 @@
@configuration_writer = args[:configuration_writer] || File
# Class which actually runs the quality check commands
@quality_checker_class =
args[:quality_checker_class] || Quality::QualityChecker
-
- @skip_tools = []
-
- @output_dir = '.'
-
- yield self if block_given?
-
- define
end
private
def define # :nodoc:
@@ -102,11 +124,15 @@
@dsl.define_task(tool) { run_quality_with_tool(tool) }
end
end
def tools
- %w(cane flog flay reek rubocop)
+ self.class.ancestors.map do |ancestor|
+ ancestor_name = ancestor.to_s
+ next unless ancestor_name.start_with?('Quality::Tools::')
+ ancestor_name.split('::').last.downcase
+ end.compact
end
def run_quality
tools.each do |tool|
run_quality_with_tool(tool)
@@ -164,103 +190,21 @@
command_options,
@output_dir)
quality_checker.execute(&count_violations_on_line)
end
- def quality_cane
- unless @configuration_writer.exist?('.cane')
- @configuration_writer.open('.cane', 'w') do |file|
- file.write('-f **/*.rb')
- end
- end
- ratchet_quality_cmd('cane',
- gives_error_code_on_violations: true,
- emacs_format: true) do |line|
- if line =~ /\(([0-9]*)\):$/
- $1.to_i
- else
- 0
- end
- end
- end
-
def ruby_dirs
@ruby_dirs ||= %w(lib test spec feature)
end
- def ruby_files
- @globber.glob('*.rb')
- .concat(@globber.glob(File.join("{#{ruby_dirs.join(',')}}",
- '**', '*.rb'))).join(' ')
+ def ruby_files_glob
+ File.join("{#{ruby_dirs.join(',')}}",
+ '**', '*.rb')
end
- def quality_reek
- args = "--single-line #{ruby_files}"
- ratchet_quality_cmd('reek',
- args: args,
- emacs_format: true,
- gives_error_code_on_violations: true) do |line|
- self.class.count_reek_violations(line)
- end
- end
-
- def self.count_reek_violations(line)
- if line =~ /^ .* (.*)$/
- 1
- else
- 0
- end
- end
-
- def quality_flog
- ratchet_quality_cmd('flog',
- args: "--all --continue --methods-only #{ruby_files}",
- emacs_format: true) do |line|
- self.class.count_violations_in_flog_output(line)
- end
- end
-
- def self.count_violations_in_flog_output(line, threshold = 50)
- if line =~ /^ *([0-9.]*): flog total$/
- 0
- elsif line =~ /^ *([0-9.]*): (.*) .*.rb:[0-9]*$/
- score = $1.to_i
- if score > threshold
- 1
- else
- 0
- end
- else
- 0
- end
- end
-
- def quality_flay
- ratchet_quality_cmd('flay',
- args: "-m 75 -t 99999 #{ruby_files}",
- emacs_format: true) do |line|
- if line =~ /^[0-9]*\).* \(mass = ([0-9]*)\)$/
- $1.to_i
- else
- 0
- end
- end
- end
-
- def quality_rubocop
- ratchet_quality_cmd('rubocop',
- gives_error_code_on_violations: true,
- args: "--format emacs #{ruby_files}") do |line|
- self.class.count_rubocop_violations(line)
- end
- end
-
- def self.count_rubocop_violations(line)
- if line =~ /^.* file[s|] inspected, (.*) offence[s|] detected$/
- 0
- else
- 1
- end
+ def ruby_files
+ @globber.glob('*.rb')
+ .concat(@globber.glob(ruby_files_glob)).join(' ')
end
end
end
end