Module: Kumi::Dev::Runner

Defined in:
lib/kumi/dev/runner.rb

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.run(schema, opts = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
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
# File 'lib/kumi/dev/runner.rb', line 14

def run(schema, opts = {})
  # Set ENV vars for debug/checkpoint based on opts
  setup_env_vars(opts)

  state = Core::Analyzer::AnalysisState.new
  errors = []

  begin
    final_state = Dev::Profiler.phase("text.analyzer") do
      Kumi::Analyzer.run_analysis_passes(schema, Kumi::Analyzer::DEFAULT_PASSES, state, errors)
    end
    ir = final_state[:ir_module]

    result = Result.new(
      state: final_state,
      ir: ir,
      errors: errors
    )

    # Report trace file if enabled
    if opts[:trace] && defined?(@trace_file) && @trace_file
      trace_file_path = @trace_file
      result.define_singleton_method(:trace_file) { trace_file_path }
    end

    result
  rescue StandardError => e
    # Convert exception to error if not already captured
    errors << e.message unless errors.include?(e.message)
    Result.new(
      state: state,
      ir: nil,
      errors: errors
    )
  end
end

.setup_env_vars(opts) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kumi/dev/runner.rb', line 51

def self.setup_env_vars(opts)
  if opts[:trace]
    ENV["KUMI_DEBUG_STATE"] = "1"
    trace_file = ENV["KUMI_DEBUG_FILE"] || "tmp/state_trace.jsonl"
    ENV["KUMI_DEBUG_FILE"] = trace_file

    # Store for later reporting
    @trace_file = trace_file
  end

  ENV["KUMI_CHECKPOINT_PHASES"] = opts[:snap] if opts[:snap]

  ENV["KUMI_CHECKPOINT_DIR"] = opts[:snap_dir] if opts[:snap_dir]

  ENV["KUMI_CHECKPOINT_RESUME_FROM"] = opts[:resume_from] if opts[:resume_from]

  ENV["KUMI_CHECKPOINT_RESUME_AT"] = opts[:resume_at] if opts[:resume_at]

  return unless opts[:stop_after]

  ENV["KUMI_CHECKPOINT_STOP_AFTER"] = opts[:stop_after]
end