Module: CountLOC

Defined in:
lib/countloc.rb

Overview

CountLOC module that encapsulates the methods and classes used to gather code metrics.

Defined Under Namespace

Classes: ConsoleWriter, CsvFileWriter, HtmlFileWriter, LineCounter, Results

Constant Summary collapse

VERSION =
'0.4.0'
DEFAULT_FILE_TYPES =
{
    :ruby   => [".rb"],
    :python => [".py"],
    :c      => [".h", ".c"],
    :cpp    => [".h", ".hpp", ".cpp", ".c", ".cc", ".inl"],
    :csharp => [".cs"],
    :java   => [".java"],
    :vb     => [".vb"]
}
DEFAULT_STYLE =
{
  ".rb"   => :ruby,
  ".py"   => :python,
  ".h"    => :cpp,
  ".c"    => :cpp,
  ".hpp"  => :cpp,
  ".cpp"  => :cpp,
  ".cc"   => :cpp,
  ".inl"  => :cpp,
  ".cs"   => :csharp,
  ".java" => :java,
  ".vb"   => :vb
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.countloc(files, options = {}) ⇒ Object

Generates LOC metrics for the specified files and sends the results to the console. Supported options:

  • :recurse - recurse into sub-directories. Default = false.

  • :csv - write the results to a csv file. Default = false.

  • :csvFilename - name of csv file to generate. Used with :csv option. Default = countloc.csv

  • :html - write the results to a html file. Default = false.

  • :htmlFilename - name of html file to generate. Used with :html option. Default = countloc.html

  • :quiet - do not output results to stdout

  • :mode - language mode. Determines parsing rules and default file types. Supported modes: ruby, python, c, cpp, csharp, java, vb. If no mode is selected, the default mode is to select the parsing rules based on file extension. A default set of file extensions covering all supported languages will be used.

  • :fileTypes - Types of file to include in the LOC analysis. By default the file types used are based on the language mode selected. This option can be used to override the default file types.



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/countloc.rb', line 349

def countloc(files, options = {})
  
  # Setup defaults for filenames
  if options[:defaultMode]
    options[:fileTypes] = DEFAULT_FILE_TYPES.values.flatten if not options[:useUserFileTypes]      
  else
    options[:fileTypes] = DEFAULT_FILE_TYPES[options[:mode]] if not options[:useUserFileTypes]
  end
  
  # Setup the output writers based on the options
  writers = []
  writers << ConsoleWriter.new if not options[:quiet]
  writers << CsvFileWriter.new(options[:csvFilename]) if options[:csv]
  writers << HtmlFileWriter.new(options[:htmlFilename]) if options[:html]

  # Expand directories into the appropriate file lists
  dirs = files.select { |filename| File.directory?(filename) }
  if dirs.size > 0
    recursePattern = ("**" if options[:recurse]) || ""
    files -= dirs
    options[:fileTypes].each do |fileType|
      files += dirs.collect { |dirname| Dir.glob(File.join(dirname, recursePattern, '*'+fileType))}.flatten      
    end
    files.uniq!
  end

  # Sum will keep the running total
  sum = LineCounter.new("TOTAL")

  # Container to hold the results
  results = Results.new

  # Generate metrics for each file
  files.each do |filename|
    begin
        File.open(filename) do |file|
        counter = LineCounter.new(filename)
        mode = options[:mode]
        if options[:defaultMode]
          mode = DEFAULT_STYLE[File.extname(filename)] || options[:mode]
        end
        counter.read(file, mode)
        sum += counter
        results << counter
      end
    rescue
      puts "Error: " + $!.to_s
    end
  end

  # Add the totals to the results
  results << sum

  # Write the metrics to the required files in the appropriate formats
  writers.each { |writer| writer.write(results) }

  return results
end

Instance Method Details

#getStyle(filename) ⇒ Object

Get the supported style for a given filename



56
57
58
# File 'lib/countloc.rb', line 56

def getStyle(filename)
  DEFAULT_FILE_TYPES.invert[File.extname(filename)]
end