aboutsummaryrefslogtreecommitdiffstats
path: root/tools/line_statistics
blob: bfa921b095016b33b0a7bad0cdeda1006e2d4691 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Class used to calculates LOC for a provided file list.
#
# Example:
#   files = FileList["lib/active_record/**/*.rb"]
#   CodeTools::LineStatistics.new(files).print_loc
module CodeTools
  class LineStatistics

    # @param files [Array, FileList, Enumerable]
    # e.g. FileList["lib/active_record/**/*.rb"]
    def initialize(files)
      @files = Array(files).compact
    end

    # Calculates LOC for each file
    # Outputs each file and a total LOC
    def print_loc
      lines, codelines, total_lines, total_codelines = 0, 0, 0, 0

      @files.each do |file_name|
        next if file_name =~ /vendor/
        File.open(file_name, 'r') do |f|
          while line = f.gets
            lines += 1
            next if line =~ /^\s*$/
            next if line =~ /^\s*#/
            codelines += 1
          end
        end
        puts "L: #{sprintf("%4d", lines)}, LOC #{sprintf("%4d", codelines)} | #{file_name}"

        total_lines     += lines
        total_codelines += codelines

        lines, codelines = 0, 0
      end

      puts "Total: Lines #{total_lines}, LOC #{total_codelines}"
    end

  end
end