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]
= []
+= ['--import-path', @opts[:import_path]] if @opts[:import_path]
+= ['--opaque-types', @opts[:opaque_types]] if @opts[:opaque_types]
args = + args
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)
@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
}
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
|