Sha256: ba3e61c0cb1bdd098af5894952d0b633c2f0c3c50a37f6423f556c29a23f25a9

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

require "churn/calculator"

require "attractor/value"

module Attractor
  # calculates churn and complexity
  class BaseCalculator
    attr_reader :type

    def initialize(file_prefix: "", ignores: "", file_extension: "rb", minimum_churn_count: 3, start_ago: "5y")
      @file_prefix = file_prefix
      @file_extension = file_extension
      @minimum_churn_count = minimum_churn_count
      @start_date = Date.today - Attractor::DurationParser.new(start_ago).duration
      @ignores = ignores
    end

    def calculate
      churn = ::Churn::ChurnCalculator.new(
        file_extension: @file_extension,
        file_prefix: @file_prefix,
        minimum_churn_count: @minimum_churn_count,
        start_date: @start_date,
        ignores: @ignores
      ).report(false)

      puts "Calculating churn and complexity values for #{churn[:churn][:changes].size} #{type} files"

      values = churn[:churn][:changes].map do |change|
        history = git_history_for_file(file_path: change[:file_path])
        commit = history&.first&.first

        cached_value = Cache.read(file_path: change[:file_path])

        if !cached_value.nil? && !cached_value.current_commit.nil? && cached_value.current_commit == commit
          value = cached_value
        else
          complexity, details = yield(change)

          value = Value.new(file_path: change[:file_path],
            churn: change[:times_changed],
            complexity: complexity,
            details: details,
            history: history)
          Cache.write(file_path: change[:file_path], value: value)
        end

        print "."
        value
      end

      Cache.persist!

      print "\n\n"

      values
    end

    private

    def git_history_for_file(file_path:, limit: 10)
      history = `git log --oneline -n #{limit} -- #{file_path}`
      history.split("\n")
        .map do |log_entry|
        log_entry.partition(/\A(\S+)\s/)
          .map(&:strip)
          .reject(&:empty?)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
attractor-2.6.0 lib/attractor/calculators/base_calculator.rb~
attractor-2.5.0 lib/attractor/calculators/base_calculator.rb~
attractor-2.4.0 lib/attractor/calculators/base_calculator.rb