Module: Asciidoctor::Diagram::Cli

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

Class Method Summary collapse

Class Method Details

.run(*args) ⇒ Object



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
# File 'lib/asciidoctor-diagram/util/cli.rb', line 11

def self.run(*args)
  if args.last.is_a? Hash
    opts = args.pop.dup
  else
    opts = {}
  end

  in_data = opts[:stdin_data]

  if Hash === args.first
    env = args.shift.dup
  else
    env = {}
  end

  pb = java.lang.ProcessBuilder.new(*args)

  pb.directory(java.io.File.new(opts[:chdir] || Dir.pwd))

  env.each_pair do |key, value|
    pb.environment.put(key, value)
  end
  p = pb.start

  stdout = ""
  out_reader = start_stream_reader(p.getInputStream, stdout)
  stderr = ""
  err_reader = start_stream_reader(p.getErrorStream, stderr)

  if in_data
    p.getOutputStream.write(in_data.to_java_bytes)
    p.getOutputStream.close
  end

  p.waitFor

  out_reader.join
  err_reader.join

  status = p.exitValue

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

  {
      :out => stdout,
      :err => stderr,
      :status => status
  }
end