Module: Asciidoctor::Diagram::CliGenerator

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

Class Method Summary collapse

Class Method Details

.generate(opts, target_file, open3_opts = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/asciidoctor-diagram/util/cli_generator.rb', line 45

def self.generate(opts, target_file, open3_opts = {})
  case opts
    when Array
      args = opts
      out_file = nil
    when Hash
      args = opts[:args]
      out_file = opts[:out_file]
    else
      raise "Block passed to generate_file should return an Array or a Hash"
  end

  run_cli(*args, open3_opts)

  if out_file
    File.rename(out_file, target_file.path)
  end

  File.binread(target_file.path)
end

.generate_file(tool, format, code) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/asciidoctor-diagram/util/cli_generator.rb', line 23

def self.generate_file(tool, format, code)
  tool_name = File.basename(tool)

  source_file = Tempfile.new([tool_name, ".#{format}"])
  begin
    File.write(source_file.path, code)

    target_file = Tempfile.new([tool_name, ".#{format}"])
    begin
      target_file.close

      opts = yield tool, source_file.path, target_file.path

      generate(opts, target_file)
    ensure
      target_file.unlink
    end
  ensure
    source_file.unlink
  end
end

.generate_stdin(tool, format, code) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/asciidoctor-diagram/util/cli_generator.rb', line 8

def self.generate_stdin(tool, format, code)
  tool_name = File.basename(tool)

  target_file = Tempfile.new([tool_name, ".#{format}"])
  begin
    target_file.close

    opts = yield tool, target_file.path

    generate(opts, target_file, :stdin_data => code)
  ensure
    target_file.unlink
  end
end

.run_cli(*args) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/asciidoctor-diagram/util/cli_generator.rb', line 66

def self.run_cli(*args)
  stdout, stderr, status = Open3.capture3(*args)

  if status != 0
    raise "#{File.basename(args[0])} failed: #{stdout.empty? ? stderr : stdout}"
  end

  stdout
end