Module: Asciidoctor::Diagram::CliGenerator

Defined in:
lib/asciidoctor-diagram/util/cli_generator.rb

Class Method Summary collapse

Class Method Details

.generate(tool, parent, code) ⇒ 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
# File 'lib/asciidoctor-diagram/util/cli_generator.rb', line 10

def self.generate(tool, parent, code)
  tool_var = '@' + tool

  tool_path = instance_variable_get(tool_var)
  unless tool_path
    tool_path = parent.document.attributes[tool]
    tool_path = ::Asciidoctor::Diagram.which(tool) unless tool_path && File.executable?(tool_path)
    raise "Could not find the '#{tool}' executable in PATH; add it to the PATH or specify its location using the '#{tool}' document attribute" unless tool_path
    instance_variable_set(tool_var, tool_path)
  end

  target_file = Tempfile.new(tool)
  begin
    target_file.close

    args = yield tool_path, target_file.path

    IO.popen(args, "w") do |io|
      io.write code
    end
    result_code = $?

    raise "#{tool} image generation failed" unless result_code == 0

    File.binread(target_file.path)
  ensure
    target_file.unlink
  end
end