Class: LayerChecker::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/layer_checker/reporter.rb

Constant Summary collapse

COLORS =
{
  red: "\e[31m",
  green: "\e[32m",
  yellow: "\e[33m",
  blue: "\e[34m",
  reset: "\e[0m"
}.freeze

Class Method Summary collapse

Class Method Details

.colorize(text, color) ⇒ Object



90
91
92
93
# File 'lib/layer_checker/reporter.rb', line 90

def self.colorize(text, color)
  return text unless $stdout.tty?
  "#{COLORS[color]}#{text}#{COLORS[:reset]}"
end

.console_report(result) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
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
61
# File 'lib/layer_checker/reporter.rb', line 24

def self.console_report(result)
  puts "\n" + "=" * 80
  puts colorize("Layer Checker Results", :blue)
  puts "=" * 80 + "\n"

  if result[:success]
    puts colorize("✓ #{result[:message]}", :green)
  else
    puts colorize("✗ #{result[:message]}", :red)
    puts

    if result[:violations].any?
      puts colorize("Violations found:", :yellow)
      puts

      # Group violations by source module
      grouped = result[:violations].group_by { |v| v.to_h[:source_module] }
      
      grouped.each do |source_module, violations|
        module_name = source_module || "Unassigned files"
        puts colorize("  #{module_name}:", :yellow)
        
        violations.each do |violation|
          puts "    #{format_violation(violation)}"
        end
        puts
      end

      # Summary
      puts "=" * 80
      puts colorize("Summary:", :blue)
      puts "  Total violations: #{colorize(result[:violations].size.to_s, :red)}"
      puts "  Modules affected: #{colorize(grouped.keys.size.to_s, :yellow)}"
    end
  end

  puts "=" * 80 + "\n"
end

.format_violation(violation) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/layer_checker/reporter.rb', line 81

def self.format_violation(violation)
  v = violation.to_h
  location = v[:line_number] ? "#{v[:source_file]}:#{v[:line_number]}" : v[:source_file]
  
  "#{location}\n" \
  "      → #{v[:source_module]} cannot depend on #{v[:target_module]}\n" \
  "      → Referenced: #{colorize(v[:target_constant], :red)}"
end

.json_report(result) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/layer_checker/reporter.rb', line 63

def self.json_report(result)
  require 'json'
  
  output = {
    success: result[:success],
    message: result[:message],
    violations: result[:violations].map(&:to_h),
    summary: {
      total_violations: result[:violations].size,
      modules_affected: result[:violations].map { |v| v.to_h[:source_module] }.uniq.size
    }
  }

  puts JSON.pretty_generate(output)
end

.report(result, format: :console) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/layer_checker/reporter.rb', line 13

def self.report(result, format: :console)
  case format
  when :console
    console_report(result)
  when :json
    json_report(result)
  else
    console_report(result)
  end
end