Module: Linecounter::Report

Defined in:
lib/linecounter/report.rb

Class Method Summary collapse

Class Method Details

.json(result, options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/linecounter/report.rb', line 18

def json(result, options)
  rows = result.rows
  payload = {
    generated_at: Time.now.utc.iso8601,
    files_scanned: rows.size,
    top: rows.first(options[:top])
  }
  unless options[:show_branch_count]
    payload[:top] = payload[:top].map { |r| r.reject { |k, _| k == :branch_breakdown } }
  end
  if options[:show_structure_overview]
    payload[:structure_overview] = result.structure_overview
    payload[:structure_loc_overview] = type_loc_overview(result)
  end
  if options[:show_detailed_structure]
    payload[:structure_item_counts_overview] = result.structure_item_counts_overview
    payload[:structure_item_loc_overview] = result.structure_item_loc_overview
  end
  unless options[:show_detailed_structure]
    payload[:top] = payload[:top].map { |r| r.reject { |k, _| k == :structure_item_loc || k == :structure_item_counts } }
  end
  puts JSON.pretty_generate(payload)
end

.render(result, options) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/linecounter/report.rb', line 10

def render(result, options)
  if options[:json]
    json(result, options)
  else
    text(result, options)
  end
end

.text(result, options) ⇒ Object



42
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
89
90
91
# File 'lib/linecounter/report.rb', line 42

def text(result, options)
  rows = result.rows
  puts "Ruby Quality Signals"
  puts "Files scanned: #{rows.size}"
  puts
  puts "Column descriptions:"
  puts "  Churn    = total git commits touching the file (optionally since --since)."
  puts "  Branches = count of control-flow tokens (sum of per-keyword counts)."
  puts "  LOC      = non-empty lines of code in the file."
  puts "  File     = repository-relative path."
  puts
  puts "%-6s %-8s %-6s %s" % ["Churn", "Branches", "LOC", "File"]

  rows.first(options[:top]).each do |r|
    puts "%-6d %-8d %-6d %s" % [r[:churn], r[:branches], r[:loc], r[:file]]
    if options[:show_branch_count]
      breakdown = r[:branch_breakdown]
      detail = BranchAnalyzer::BRANCH_TOKENS.map { |name, label, _| "#{label}=#{breakdown[name]}" }.join(" | ")
      puts "  #{detail}"
    end
  end

  if options[:show_structure_overview]
    puts
    puts "Structure overview (all scanned files):"
    StructureAnalyzer::STRUCTURE_ORDER.each do |key|
      count = result.structure_overview[key]
      avg_loc_per_item = count.zero? ? 0.0 : (type_loc_sum(result, key).to_f / count)
      puts "%-26s count=%-4d avg_loc_per_item=%0.2f" % [key, count, avg_loc_per_item]
    end
  end

  if options[:show_detailed_structure]
    puts
    puts "Detailed structure (all scanned files):"
    StructureAnalyzer::STRUCTURE_ORDER.each do |type|
      items = StructureAnalyzer::STRUCTURE_ITEMS.select { |item| item[:type] == type }
      next if items.empty?
      lines = items.map do |item|
        count = result.structure_item_counts_overview[item[:key]]
        next if count.zero?
        avg = result.structure_item_loc_overview[item[:key]].to_f / count
        "  %-26s count=%-4d avg_loc_per_item=%0.2f" % [item[:label], count, avg]
      end.compact
      next if lines.empty?
      puts type
      lines.each { |line| puts line }
    end
  end
end

.type_loc_overview(result) ⇒ Object



102
103
104
105
106
# File 'lib/linecounter/report.rb', line 102

def type_loc_overview(result)
  result.structure_overview.keys.each_with_object({}) do |type, acc|
    acc[type] = type_loc_sum(result, type)
  end
end

.type_loc_sum(result, type) ⇒ Object

Total statement LOC for a structure type, summed from its items. The per-item LOC is item-keyed, so it must be rolled up by type here rather than indexed directly by the type symbol.



96
97
98
99
100
# File 'lib/linecounter/report.rb', line 96

def type_loc_sum(result, type)
  StructureAnalyzer::STRUCTURE_ITEMS
    .select { |item| item[:type] == type }
    .sum { |item| result.structure_item_loc_overview[item[:key]] }
end