Sha256: f10ec8d4e8b40c8c3252d52563bbc40dabb39b8a7c42cead695814629673095d

Contents?: true

Size: 1.24 KB

Versions: 7

Compression:

Stored size: 1.24 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.to_s.empty? && 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

7 entries across 7 versions & 1 rubygems

Version Path
sandi_meter-1.2.0 lib/sandi_meter/file_scanner.rb
sandi_meter-1.1.8 lib/sandi_meter/file_scanner.rb
sandi_meter-1.1.7 lib/sandi_meter/file_scanner.rb
sandi_meter-1.1.6 lib/sandi_meter/file_scanner.rb
sandi_meter-1.1.5 lib/sandi_meter/file_scanner.rb
sandi_meter-1.0.5 lib/sandi_meter/file_scanner.rb
sandi_meter-1.0.4 lib/sandi_meter/file_scanner.rb