Sha256: 3f27a914ab563c875c0f0edecfaf47fc50f5c938da12e11086ffe8accd1ba814

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

require_relative 'analyzer'
require_relative 'calculator'

module SandiMeter
  class FileScanner
    def initialize(log_errors = false)
      @log_errors = log_errors
      @calculator = SandiMeter::Calculator.new
    end

    def scan(path, store_details = false)
      read_ignore_file(path) unless @exclude_patterns

      if File.directory?(path)
        scan_dir(path)
      else
        scan_file(path)
      end

      @calculator.calculate!(store_details)
    end

    private
    def scan_dir(path)
      Dir["#{path}/**/*.rb"].reject { |f| @exclude_patterns && f =~ /#{@exclude_patterns}/ }.each do |file|
        scan_file(file)
      end
    end

    def read_ignore_file(path)
      ignore_file_path = File.join(path, 'sandi_meter', '.sandi_meter')
      if File.exists?(ignore_file_path)
        @exclude_patterns ||= File.read(ignore_file_path).split("\n").join("|")
      end
    end

    def scan_file(path)
      begin
        analyzer = SandiMeter::Analyzer.new
        data = analyzer.analyze(path)
        @calculator.push(data)
      rescue Exception => e
        if @log_errors
          # TODO
          # add backtrace
          puts "Checkout #{path} for:"
          puts "\t#{e.message}"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sandi_meter-1.0.3 lib/sandi_meter/file_scanner.rb