Class: Kaitai::Struct::Visualizer::KSYCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/kaitai/struct/visualizer/ksy_compiler.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts, out = $stderr) ⇒ KSYCompiler

Returns a new instance of KSYCompiler.



11
12
13
14
# File 'lib/kaitai/struct/visualizer/ksy_compiler.rb', line 11

def initialize(opts, out = $stderr)
  @opts = opts
  @out = out
end

Instance Method Details

#compile_formats(fns) ⇒ Object



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
103
104
105
106
107
108
109
# File 'lib/kaitai/struct/visualizer/ksy_compiler.rb', line 16

def compile_formats(fns)
  errs = false
  main_class_name = nil
  Dir.mktmpdir { |code_dir|
    args = ['--ksc-json-output', '--debug', '-t', 'ruby', *fns, '-d', code_dir]

    # Extra arguments
    extra = []
    extra += ['--import-path', @opts[:import_path]] if @opts[:import_path]
    extra += ['--opaque-types', @opts[:opaque_types]] if @opts[:opaque_types]

    args = extra + args

    # UNIX-based systems run ksc via a shell wrapper that requires
    # extra '--' in invocation to disambiguate our '-d' from java runner
    # '-d' (which allows to pass defines to JVM). Windows-based systems
    # do not need and do not support this extra '--', so we don't add it
    # on Windows.
    args.unshift('--') unless Kaitai::TUI::is_windows?

    status = nil
    log_str = nil
    err_str = nil
    Open3.popen3('kaitai-struct-compiler', *args) { |stdin, stdout, stderr, wait_thr|
      status = wait_thr.value
      log_str = stdout.read
      err_str = stderr.read
    }

    if not status.success?
      if status.exitstatus == 127
        @out.puts "ksv: unable to find and execute kaitai-struct-compiler in your PATH"
      elsif err_str =~ /Error: Unknown option --ksc-json-output/
        @out.puts "ksv: your kaitai-struct-compiler is too old:"
        system('kaitai-struct-compiler', '--version')
        @out.puts "\nPlease use at least v0.7."
      else
        @out.puts "ksc crashed (exit status = #{status}):\n"
        @out.puts "== STDOUT\n"
        @out.puts log_str
        @out.puts
        @out.puts "== STDERR\n"
        @out.puts err_str
        @out.puts
      end
      exit status.exitstatus
    end

    log = JSON.load(log_str)

    # FIXME: add log results check
    @out.puts "Compilation OK"

    fns.each_with_index { |fn, idx|
      @out.puts "... processing #{fn} #{idx}"

      log_fn = log[fn]
      if log_fn['errors']
        report_err(log_fn['errors'])
        errs = true
      else
        log_classes = log_fn['output']['ruby']
        log_classes.each_pair { |k, v|
          if v['errors']
            report_err(v['errors'])
            errs = true
          else
            compiled_name = v['files'][0]['fileName']
            compiled_path = "#{code_dir}/#{compiled_name}"

            @out.puts "...... loading #{compiled_name}"
            require compiled_path
          end
        }

        # Is it main ClassSpecs?
        if idx == 0
          main = log_classes[log_fn['firstSpecName']]
          main_class_name = main['topLevelName']
        end
      end
    }

  }

  if errs
    @out.puts "Fatal errors encountered, cannot continue"
    exit 1
  else
    @out.puts "Classes loaded OK, main class = #{main_class_name}"
  end

  return main_class_name
end

#report_err(err) ⇒ Object



111
112
113
# File 'lib/kaitai/struct/visualizer/ksy_compiler.rb', line 111

def report_err(err)
  @out.puts "Error: #{err.inspect}"
end