Sha256: 9632b2dcd05e5091cfd6693af5dd4ca662934981f1eae823cddbeb4c816babd0

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# encoding: utf-8

namespace :metrics do
  begin
    require 'flay'

    project = Develry.project
    config  = project.flay

    compatible_scores = %w(mri-1.9.3 mri-2.0.0)

    if ! compatible_scores.include?(Develry.rvm)
      task :flay do
        $stderr.puts "Flay is disabled under #{Develry.rvm}"
      end
    elsif config.enabled?
      # Original code by Marty Andrews:
      # http://blog.martyandrews.net/2009/05/enforcing-ruby-code-quality.html
      desc 'Measure code duplication'
      task :flay do
        threshold   = config.threshold
        total_score = config.total_score
        files       = Flay.expand_dirs_to_files(project.lib_dir).sort

        # Run flay first to ensure the max mass matches the threshold
        flay = Flay.new(fuzzy: false, verbose: false, mass: 0)
        flay.process(*files)
        flay.analyze

        masses = flay.masses.map do |hash, mass|
          Rational(mass, flay.hashes[hash].size)
        end

        max = (masses.max || 0).to_i
        unless max >= threshold
          Develry.notify "Adjust flay threshold down to #{max}"
        end

        total = masses.sum.to_i
        unless total == total_score
          Develry.notify "Flay total is now #{total}, but expected #{total_score}"
        end

        # Run flay a second time with the threshold set
        flay = Flay.new(fuzzy: false, verbose: false, mass: threshold.succ)
        flay.process(*files)
        flay.analyze

        mass_size = flay.masses.size

        if mass_size.nonzero?
          flay.report
          Develry.notify "#{mass_size} chunks have a duplicate mass > #{threshold}"
        end
      end
    else
      task :flay do
        $stderr.puts 'Flay is disabled'
      end
    end
  rescue LoadError
    task :flay do
      $stderr.puts 'In order to run flay, you must: gem install flay'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
develry-0.0.3 tasks/metrics/flay.rake
develry-0.0.2 tasks/metrics/flay.rake