Class: Gravitext::DevTools::LineCounter

Inherits:
Object
  • Object
show all
Includes:
FileUtils, Gravitext::DevTools
Defined in:
lib/gravitext-devtools/line_counter.rb

Constant Summary

Constants included from Gravitext::DevTools

VERSION

Instance Method Summary collapse

Methods included from Gravitext::DevTools

configure, load_config_from_pwd

Constructor Details

#initializeLineCounter

Returns a new instance of LineCounter.



27
28
29
30
# File 'lib/gravitext-devtools/line_counter.rb', line 27

def initialize
  @verbose = false
  @git_lister = GitFileLister.new
end

Instance Method Details

#count_files(files) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/gravitext-devtools/line_counter.rb', line 87

def count_files( files )
  total_lines = total_code = 0
  files.each do |fn|
    lines, codelines = count_lines( fn )
    show_count_line( fn, lines, codelines ) if @verbose
    total_lines += lines
    total_code  += codelines
  end
  [ total_lines, total_code ]
end

#count_lines(filename) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gravitext-devtools/line_counter.rb', line 68

def count_lines( filename )
  lines = codelines = 0
  is_java = ( filename =~ /\.java$/ )

  open( filename, 'rb' ) { |f|
    f.each do |line|
      lines += 1
      next if line =~ /^\s*$/
      next if line =~ ( is_java ? %r{\s*[/*]} : /^\s*#/ )
      codelines += 1
    end
  }
  [ lines, codelines ]
end

#parse_options(args = ARGV) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/gravitext-devtools/line_counter.rb', line 32

def parse_options( args = ARGV )

  @git_lister.parse_options( args ) do |opts|
    opts.banner = "Usage:  gt-count [dir|file] ..."
    opts.on( "-v", "--verbose",
             "Output per file counts" ) do
      @verbose = true
    end
  end
end

#run(args = ARGV) ⇒ 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
# File 'lib/gravitext-devtools/line_counter.rb', line 43

def run( args = ARGV )

  parse_options( args )

  @git_lister.exclusions = []
  git_files = @git_lister.files

  show_count_line( "LANG/FILE", "LINES", "CODE" )
  total_lines = total_code = 0

  map = [ [ 'JAVA', [ '**/*.java' ] ],
          [ 'RUBY', [ '**/*.rb', '**/bin/*', '**/init/*',
                      '**/Rakefile', '**/Gemfile', '**.gemspec' ] ] ]

  map.each do | lang, fpats |
    files = git_files.select { |f| @git_lister.match?( fpats, f ) }
    lines, codelines = count_files( files )
    show_count_line( lang, lines, codelines )
    total_lines += lines
    total_code  += codelines
  end
  show_count_line( "TOTAL", total_lines, total_code)

end

#show_count_line(msg, lines, code) ⇒ Object



83
84
85
# File 'lib/gravitext-devtools/line_counter.rb', line 83

def show_count_line(msg, lines, code)
  printf( "%6s %6s %s\n", lines.to_s, code.to_s, msg )
end