Class: Countless::Cloc

Inherits:
Object
  • Object
show all
Defined in:
lib/countless/cloc.rb

Overview

A simple wrapper around the CLOC utility.

Class Method Summary collapse

Class Method Details

.raw_stats(*paths) ⇒ Hash{String => Hash{String => Mixed}] the raw CLOC YAML output

Fetch the raw statistics via CLOC for the given paths.

rubocop:disable Metrics/MethodLength because of the system

command preparation

Parameters:

  • paths (Array<String, Pathname>)

    the paths (files or directories) to fetch the statistics for

Returns:

  • (Hash{String => Hash{String => Mixed}] the raw CLOC YAML output)

    Hash=> Hash{String => Mixed] the raw CLOC YAML output



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/countless/cloc.rb', line 39

def raw_stats(*paths)
  cmd = [
    Countless.cloc_path,
    '--quiet',
    '--by-file',
    '--yaml',
    '--list-file -',
    '2>/dev/null'
  ].join(' ')

  # We pipe in the file list via stdin to cloc, this allows us to
  # pass large file lists down (ARGV is size limited)
  stdout = IO.popen(cmd, File::RDWR) do |io|
    paths.each { |path| io.puts(path) }
    io.close_write
    io.read
  end

  # When the system command was not successful,
  # we return an fallback result
  return {} unless $CHILD_STATUS.success?

  # Otherwise we use the CLOC produced YAML and parse it
  YAML.safe_load(stdout) || {}
end

.stats(*paths) ⇒ Hash{String => Hash{Symbol => Integer}}

Extract code statistics from the given files with CLOC. Each key of the resulting hash is the file path which was inspected. Each value of the resulting hash contains the raw statistic numbers (blank, comment, code, total).

Example:

{
  "/app/lib/countless/configuration.rb" => {
    :blank=>24, :comment=>43, :code=>141, :total=>208
  }
}

Returns:

  • (Hash{String => Hash{Symbol => Integer}})

    the re-structured CLOC statistics



22
23
24
25
26
27
28
# File 'lib/countless/cloc.rb', line 22

def stats(*paths)
  raw_stats(*paths).except('SUM', 'header').transform_values do |obj|
    obj.symbolize_keys.except(:language).tap do |stats|
      stats[:total] = stats.values.sum
    end
  end
end