Skip to content

Commit

Permalink
Merge 5d479ae into a58ba94
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Glusman committed Mar 26, 2015
2 parents a58ba94 + 5d479ae commit 592aae2
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 138 deletions.
2 changes: 1 addition & 1 deletion bin/debt_ceiling
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env ruby
require_relative '../lib/debt_ceiling'
DebtCeiling.calculate(ARGV[0] ? ARGV[0] : '.')
DebtCeiling.audit(ARGV[0] ? ARGV[0] : '.')
1 change: 0 additions & 1 deletion debt_ceiling.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rake', '~> 10.3'
s.add_development_dependency 'rspec', '~> 3.1'
s.add_development_dependency 'coveralls', '~> 0.7'
s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
end
81 changes: 5 additions & 76 deletions lib/debt_ceiling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'rubycritic/cli/application'
require 'ostruct'
require 'forwardable'
require_relative 'debt_ceiling/common_methods'
require_relative 'debt_ceiling/audit'
require_relative 'debt_ceiling/accounting'
require_relative 'debt_ceiling/custom_debt'
require_relative 'debt_ceiling/static_analysis_debt'
Expand All @@ -15,9 +17,6 @@ module DebtCeiling
include Configurations
extend Forwardable
extend self

attr_reader :total_debt, :accounting

def_delegators :configuration,
:extension_path, :blacklist, :whitelist, :max_debt_per_module, :reduction_date,
:reduction_target, :debt_ceiling
Expand All @@ -39,77 +38,7 @@ module DebtCeiling
end


def calculate(dir = '.', opts={preconfigured: false})
@total_debt = accounting(dir, opts).total_debt
accounting.print_results unless opts[:skip_report]
# require 'pry'; binding.pry
fail_test if failed_condition?
total_debt
end

def accounting(dir = '.', opts={preconfigured: false})
@accounting ||= begin
load_configuration unless @loaded || opts[:preconfigured]
Accounting.new(dir, opts)
end
end

def load_configuration(config_file_name=".debt_ceiling.rb")
pwd = Dir.pwd
home = Dir.home
if File.exist?("#{pwd}/#{config_file_name}")
load("#{pwd}/#{config_file_name}")
elsif File.exist?("#{home}/#{config_file_name}")
load("#{home}/#{config_file_name}")
else
puts "No #{config_file_name} configuration file detected in #{pwd} or ~/, using defaults"
end

load extension_path if extension_path && File.exist?(extension_path)
@loaded = true
end

def clear
@loaded = nil
@accounting = nil
end

private


def blacklist_matching(matchers)
@blacklist = matchers.map { |matcher| Regexp.new(matcher) }
end

def whitelist_matching(matchers)
@whitelist = matchers.map { |matcher| Regexp.new(matcher) }
end


def debt_per_reference_to(string, value)
deprecated_reference_pairs[string] = value
end

def failed_condition?
exceeded_total_limit? || missed_target? || max_debt_per_module_exceeded?
end

def exceeded_total_limit?
debt_ceiling && debt_ceiling <= total_debt
end

def missed_target?
reduction_target && reduction_target <= total_debt &&
Time.now > Chronic.parse(reduction_date)
end

def max_debt_per_module_exceeded?
max_debt_per_module && max_debt_per_module <= accounting.max_debt.to_i
end

def fail_test
at_exit do
Kernel.exit 1
end
def audit(dir, opts= {})
Audit.new(dir, opts)
end
end
end
16 changes: 7 additions & 9 deletions lib/debt_ceiling/accounting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class Accounting
extend Forwardable
attr_reader :result, :path, :debts, :total_debt, :max_debt

def initialize(path, opts = {})
def_delegator :max_debt, :analysed_module
alias_method :max_module, :analysed_module
def initialize(path)
@path = path
calc_debt_for_modules(construct_rubycritic_modules(path))
end
Expand All @@ -21,8 +23,8 @@ def print_results
Current total tech debt: #{total_debt}
Largest source of debt is: #{max_debt.name} at #{max_debt.to_i}
The rubycritic grade for that debt is: #{max_debt.letter_grade}
The flog complexity for that debt is: #{max_debt_module.complexity}
Flay suspects #{max_debt_module.duplication.to_i} areas of code duplication
The flog complexity for that debt is: #{max_module.complexity}
Flay suspects #{max_module.duplication.to_i} areas of code duplication
There are #{method_count} methods and #{smell_count} smell(s) from reek.
The file is #{max_debt.linecount} lines long.
RESULTS
Expand All @@ -36,16 +38,12 @@ def get_total_debt
debts.map(&:to_i).reduce(:+)
end

def max_debt_module
max_debt.analysed_module
end

def method_count
max_debt.analysed_module.methods_count
max_module.methods_count
end

def smell_count
max_debt.analysed_module.smells.count
max_module.smells.count
end

def construct_rubycritic_modules(path)
Expand Down
80 changes: 80 additions & 0 deletions lib/debt_ceiling/audit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module DebtCeiling
class Audit
extend Forwardable
include CommonMethods
undef :+

CONFIG_FILE_NAME = ".debt_ceiling.rb"
CONFIG_LOCATIONS = ["#{Dir.pwd}/#{CONFIG_FILE_NAME}", "#{Dir.home}/#{CONFIG_FILE_NAME}"]
NO_CONFIG_FOUND = "No #{CONFIG_FILE_NAME} configuration file detected in #{Dir.pwd} or ~/, using defaults"

attr_reader :accounting, :dir, :loaded

def_delegators :configuration,
:extension_path, :debt_ceiling, :reduction_target, :reduction_date, :max_debt_per_module

def_delegator :accounting, :total_debt

def initialize(dir = '.', opts = {})
@loaded = opts[:preconfigured]
@dir = dir
@accounting = perform_accounting
accounting.print_results unless opts[:skip_report]
fail_test if failed_condition?
end

private

def load_configuration
config_file_location ? load(config_file_location) : puts(NO_CONFIG_FOUND)

load extension_path if extension_path && File.exist?(extension_path)
@loaded = true
end

def config_file_location
CONFIG_LOCATIONS.find {|loc| File.exist?(loc) }
end

def perform_accounting
load_configuration unless loaded
Accounting.new(dir)
end

def blacklist_matching(matchers)
@blacklist = matchers.map { |matcher| Regexp.new(matcher) }
end

def whitelist_matching(matchers)
@whitelist = matchers.map { |matcher| Regexp.new(matcher) }
end


def debt_per_reference_to(string, value)
deprecated_reference_pairs[string] = value
end

def failed_condition?
exceeded_total_limit? || missed_target? || max_debt_per_module_exceeded?
end

def exceeded_total_limit?
debt_ceiling && debt_ceiling <= total_debt
end

def missed_target?
reduction_target && reduction_target <= total_debt &&
Time.now > Chronic.parse(reduction_date)
end

def max_debt_per_module_exceeded?
max_debt_per_module && max_debt_per_module <= accounting.max_debt.to_i
end

def fail_test
at_exit do
Kernel.exit 1
end
end
end
end
13 changes: 13 additions & 0 deletions lib/debt_ceiling/common_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module DebtCeiling
module CommonMethods

def configuration
DebtCeiling.configuration
end

def +(other)
self.to_i + other.to_i
end

end
end
9 changes: 1 addition & 8 deletions lib/debt_ceiling/custom_debt.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module DebtCeiling
class CustomDebt
extend Forwardable
include CommonMethods

def_delegators :configuration,
:deprecated_reference_pairs, :manual_callouts, :cost_per_todo
Expand All @@ -14,18 +15,10 @@ def initialize(file_attributes)
@debt_amount = default_measure_debt
end

def +(other)
self.to_i + other.to_i
end

private

attr_reader :file_attributes, :debt_amount

def configuration
DebtCeiling.configuration
end

def external_measure_debt
public_send(:measure_debt) if self.respond_to?(:measure_debt)
end
Expand Down
27 changes: 11 additions & 16 deletions lib/debt_ceiling/debt.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module DebtCeiling
class Debt
extend Forwardable
include CommonMethods
DoNotWhitelistAndBlacklistSimulateneously = Class.new(StandardError)

def_delegators :file_attributes,
:path, :analysed_module, :module_name, :linecount, :source_code
def_delegators :configuration,
:whitelist, :blacklist

def_delegator :analysed_module, :rating
def_delegator :debt_amount, :to_i
Expand All @@ -21,10 +24,6 @@ def name
analysed_module.name || path.to_s.split('/').last
end

def +(other)
to_i + other.to_i
end

def letter_grade
rating.to_s.downcase.to_sym
end
Expand All @@ -33,29 +32,25 @@ def letter_grade

attr_reader :file_attributes, :debt_amount

def configuration
DebtCeiling.configuration
end

def internal_measure_debt
debt_types.reduce(&:+)
end

def self.whitelist_includes?(debt)
DebtCeiling.whitelist.find { |filename| debt.path.match filename }
def whitelist_includes?(debt)
whitelist.find { |filename| debt.path.match filename }
end

def self.blacklist_includes?(debt)
DebtCeiling.blacklist.find { |filename| debt.path.match filename }
def blacklist_includes?(debt)
blacklist.find { |filename| debt.path.match filename }
end

def valid_debt?
black_empty = DebtCeiling.blacklist.empty?
white_empty = DebtCeiling.whitelist.empty?
black_empty = blacklist.empty?
white_empty = whitelist.empty?
fail DoNotWhitelistAndBlacklistSimulateneously unless black_empty || white_empty
(black_empty && white_empty) ||
(black_empty && self.class.whitelist_includes?(self)) ||
(white_empty && !self.class.blacklist_includes?(self))
(black_empty && whitelist_includes?(self)) ||
(white_empty && !blacklist_includes?(self))
end
end
end
10 changes: 1 addition & 9 deletions lib/debt_ceiling/static_analysis_debt.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module DebtCeiling
class StaticAnalysisDebt
extend Forwardable

include CommonMethods
def_delegators :configuration,
:complexity_multiplier, :duplication_multiplier, :smells_multiplier,
:grade_points, :method_count_multiplier, :ideal_max_line_count,
Expand All @@ -20,10 +20,6 @@ def initialize(file_attributes)
@debt_amount = cost_from_static_analysis_points
end

def +(other)
self.to_i + other.to_i
end

private

attr_reader :file_attributes, :debt_amount
Expand All @@ -36,10 +32,6 @@ def cost_from_non_grade_scoring
flog_flay_debt + method_count_debt + smells_debt + line_count_debt
end

def configuration
DebtCeiling.configuration
end

def letter_grade
rating.to_s.downcase.to_sym
end
Expand Down

0 comments on commit 592aae2

Please sign in to comment.