Class: Loco::Counter

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/loco/counter.rb

Constant Summary collapse

EXCLUDE =
[
 /\.sqlite3/,
 /\.gem/,
 /^db\/schema.rb/,
 /^db\/backups/,
 /^db\/migrate/,
 /^vendor/,
 /^public\/system/,
 /^public\/images/,
 /^public\/stylesheets/,
 /^config\/newrelic.yml/,
 /^tmp/,
 /^coverage/,
 /^log/,
 /^doc/,
 /^script/,
 /^stories/,
 /^static/,
 /^public\/fonts/,
 /\.#.+/,
 /\.png/,
 /\.pdf/,
 /\.ico/,
 /lib\/tasks\/.+\.txt/,
 /^README/,
 /^Gemfile.lock/,
 /^public\/javascripts\/jquery/
]

Instance Method Summary collapse

Methods included from Util

#align

Instance Method Details

#analyse_loc(opts = { }) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/loco/counter.rb', line 43

def analyse_loc opts={ }
  regexp = opts[:pattern]
  @stats = []
  @exts = Hash.new { |h, k| h[k] = 0 }
  @ext_lines = Hash.new { |h, k| h[k] = 0 }
  @sizes = Hash.new { |h, k| h[k] = [] }
  total_lines = 0

  here = File.expand_path "."
  Dir.glob("#{here}/**/*").each do |full_path|
    unless File.stat(full_path).directory?
      path = full_path.sub("#{here}/", '')
      unless exclude?(path) || !regexp.match(path)
        s = Stats.new(path)
        if s.matches(opts)
          @stats << s
          @exts[File.extname(path)] += 1
          @ext_lines[File.extname(path)] += s.lines
          @sizes[s.lines] << path
          total_lines += s.lines
        end
      end
    end
  end

  puts "\n### extensions ###"
  @exts.each { |ext, count| puts "#{align ext} - #{align count} files, #{align @ext_lines[ext]} lines"}

  puts "\n### distribution ###"
  @sizes = @sizes.to_a.sort { |a, b|
    a[0] <=> b[0]
  }

  @sizes.each { |size, files|
    puts "#{align size} lines #{align files.size} #{"*" * files.size}"
  }

  total = @stats.size
  @stats = @stats.sort { |a, b| b.lines <=> a.lines }
  @stats = @stats[0..50] if @stats.size > 50

  puts "\n### longest files ###"
  @stats.each { |s| puts s }

  puts "\n### Total #{regexp.inspect} : #{total} files, #{total_lines} lines, #{"%.3f" % (1.0 * total_lines / total)} avg loc per file ###"
end

#exclude?(path) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'lib/loco/counter.rb', line 34

def exclude? path
  EXCLUDE.each { |regex|
    if path =~ regex
      return true
    end
  }
  false
end