Class: Hone::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/hone/analyzer.rb

Overview

Orchestrates the full analysis pipeline: Scanner -> MethodMap -> Profiler -> Correlator -> Reporter

Examples:

Basic usage (static analysis only)

analyzer = Hone::Analyzer.new("app.rb")
result = analyzer.run
puts result.output

With profile correlation

analyzer = Hone::Analyzer.new("app.rb", profile: "profile.json")
result = analyzer.run

Directory analysis

analyzer = Hone::Analyzer.new("lib/", format: :json)
result = analyzer.run

Defined Under Namespace

Classes: Result

Constant Summary collapse

PROFILE_DIR =

Default directory for cached profile files

"tmp/hone"

Instance Method Summary collapse

Constructor Details

#initialize(path, profile: nil, memory_profile: nil, format: :text, color: nil, quiet: false, verbose: false, top: nil, hot_only: false, show_cold: false, fail_on: :hot, diff: nil, baseline: nil, rails: false) ⇒ Analyzer

Returns a new instance of Analyzer.

Parameters:

  • path (String)

    File or directory to analyze

  • profile (String, nil) (defaults to: nil)

    Path to StackProf JSON profile

  • memory_profile (String, nil) (defaults to: nil)

    Path to MemoryProfiler JSON profile

  • format (Symbol) (defaults to: :text)

    Output format: :text, :json, :github

  • color (Boolean, nil) (defaults to: nil)

    Force color output (nil = auto-detect)

  • quiet (Boolean) (defaults to: false)

    Minimal output

  • verbose (Boolean) (defaults to: false)

    Extended output with pattern details

  • top (Integer, nil) (defaults to: nil)

    Show only top N findings

  • hot_only (Boolean) (defaults to: false)

    Only show HOT findings

  • show_cold (Boolean) (defaults to: false)

    Include COLD findings

  • fail_on (Symbol) (defaults to: :hot)

    Exit non-zero for: :hot, :warm, :any, :none

  • diff (String, nil) (defaults to: nil)

    Git ref to compare against for changed files

  • baseline (String, nil) (defaults to: nil)

    Path to baseline JSON file to suppress findings

  • rails (Boolean) (defaults to: false)

    Enable Rails-specific analysis



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hone/analyzer.rb', line 41

def initialize(path, profile: nil, memory_profile: nil, format: :text, color: nil, quiet: false, verbose: false,
  top: nil, hot_only: false, show_cold: false, fail_on: :hot, diff: nil, baseline: nil, rails: false)
  @path = path
  @profile_path = profile || auto_detect_profile(:cpu)
  @memory_profile_path = memory_profile || auto_detect_profile(:memory)
  @format = format.to_sym
  @color = color
  @quiet = quiet
  @verbose = verbose
  @top = top
  @hot_only = hot_only
  @show_cold = show_cold
  @fail_on = fail_on.to_sym
  @diff = diff
  @baseline = baseline
  @rails = rails
end

Instance Method Details

#runResult

Run the analysis pipeline

Returns:

  • (Result)

    Analysis result with findings, output, and exit code



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
92
93
94
95
# File 'lib/hone/analyzer.rb', line 61

def run
  validate_input!

  # Step 1: Scan for patterns
  findings = scan_path

  # Step 2: Build method map for correlation
  method_map = build_method_map

  # Step 3: Load profile data (if available)
  profiles = load_profiles

  # Step 4: Correlate findings with profile
  correlator = Correlator.new(
    method_map: method_map,
    cpu_profile: profiles[:cpu],
    memory_profile: profiles[:memory]
  )
  correlated = correlator.correlate(findings)

  # Step 5: Apply filtering
  filtered = apply_filters(correlated)

  # Step 6: Generate output
  output = generate_output(filtered)

  # Step 7: Calculate exit code
  exit_code = calculate_exit_code(filtered)

  Result.new(findings: filtered, output: output, exit_code: exit_code)
rescue Error => e
  Result.new(findings: [], output: "Error: #{e.message}", exit_code: ExitCodes::ERROR)
rescue => e
  Result.new(findings: [], output: "Error: #{e.message}\n#{e.backtrace.first(5).join("\n")}", exit_code: ExitCodes::ERROR)
end