Class: Countless::Cloc
- Inherits:
-
Object
- Object
- Countless::Cloc
- Defined in:
- lib/countless/cloc.rb
Overview
A simple wrapper around the CLOC utility.
Class Method Summary collapse
-
.raw_stats(*paths) ⇒ Hash{String => Hash{String => Mixed}] the raw CLOC YAML output
Fetch the raw statistics via CLOC for the given paths.
-
.stats(*paths) ⇒ Hash{String => Hash{Symbol => Integer}}
Extract code statistics from the given files with CLOC.
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.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/countless/cloc.rb', line 36 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
}
}
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 |