Module: Kumi::Dev::Parse

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

Class Method Summary collapse

Class Method Details

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



10
11
12
13
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
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
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/kumi/dev/parse.rb', line 10

def run(schema_path, opts = {})
  # Load schema via text frontend
  begin
    schema, _inputs = Kumi::Frontends::Text.load(path: schema_path)
  rescue LoadError
    puts "Error: kumi-parser gem not available. Install: gem install kumi-parser"
    return false
  rescue StandardError => e
    puts "Parse error: #{e.message}"
    return false
  end

  # Run analyzer
  runner_opts = opts.slice(:trace, :snap, :snap_dir, :resume_from, :resume_at, :stop_after)
  res = Dev::Runner.run(schema, runner_opts)

  unless res.ok?
    puts "Analysis errors:"
    res.errors.each { |err| puts "  #{err}" }
    return false
  end

  unless res.ir
    puts "Error: No IR generated"
    return false
  end

  # Report trace file if enabled
  puts "Trace written to: #{res.trace_file}" if opts[:trace] && res.respond_to?(:trace_file)

  # Determine file extension and renderer
  extension = opts[:json] ? "json" : "txt"

  file_name = File.basename(schema_path)
  golden_path = File.join(File.dirname(schema_path), "expected", "#{file_name}_ir.#{extension}")

  # Render IR
  rendered = if opts[:json]
               Dev::IR.to_json(res.ir, pretty: true)
             else
               Dev::IR.to_text(res.ir)
             end

  # Handle write mode
  if opts[:write]
    FileUtils.mkdir_p(File.dirname(golden_path))
    File.write(golden_path, rendered)
    puts "Wrote: #{golden_path}"
    return true
  end

  # Handle update mode (write only if different)
  if opts[:update]
    if File.exist?(golden_path) && File.read(golden_path) == rendered
      puts "No changes (#{golden_path})"
      return true
    else
      FileUtils.mkdir_p(File.dirname(golden_path))
      File.write(golden_path, rendered)
      puts "Updated: #{golden_path}"
      return true
    end
  end

  # Handle no-diff mode
  if opts[:no_diff]
    puts rendered
    return true
  end

  # Default: diff mode (same as write but show diff instead)
  if File.exist?(golden_path)
    # Use diff directly with the golden file path
    require "tempfile"
    Tempfile.create(["actual", File.extname(golden_path)]) do |actual_file|
      actual_file.write(rendered)
      actual_file.flush

      result = `diff -u --label=expected --label=actual #{golden_path} #{actual_file.path}`
      if result.empty?
        puts "No changes (#{golden_path})"
        return true
      else
        puts result.chomp
        return false
      end
    end
  else
    # No golden file exists, just print the output
    puts rendered
    true
  end
end