Module: Asciidoctor::Diagram::Which

Included in:
BlockDiag, Erd, Graphviz, Meme, Mermaid, PlantUml, Shaape, Umlet, Wavedrom
Defined in:
lib/asciidoctor-diagram/util/which.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.which(cmd, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/asciidoctor-diagram/util/which.rb', line 5

def self.which(cmd, options = {})
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']

  paths = (options[:path] || []) + (ENV['PATH'] ? ENV['PATH'].split(File::PATH_SEPARATOR) : [])
  paths.each do |path|
    exts.each { |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable? exe
    }
  end

  nil
end

Instance Method Details

#which(parent_block, cmd, options = {}) ⇒ Object



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

def which(parent_block, cmd, options = {})
  attr_names = options[:attrs] || options.fetch(:alt_attrs, []) + [cmd]
  cmd_names = [cmd] + options.fetch(:alt_cmds, [])

  cmd_var = 'cmd-' + attr_names[0]

  if config.key? cmd_var
    cmd_path = config[cmd_var]
  else
    cmd_path = attr_names.map { |attr_name| parent_block.attr(attr_name, nil, true) }.find { |attr| !attr.nil? }

    unless cmd_path && File.executable?(cmd_path)
      cmd_paths = cmd_names.map do |c|
        ::Asciidoctor::Diagram::Which.which(c, :path => options[:path])
      end

      cmd_path = cmd_paths.reject { |c| c.nil? }.first
    end

    config[cmd_var] = cmd_path

    if cmd_path.nil? && options.fetch(:raise_on_error, true)
      raise "Could not find the #{cmd_names.map { |c| "'#{c}'" }.join(', ')} executable in PATH; add it to the PATH or specify its location using the '#{attr_names[0]}' document attribute"
    end
  end

  cmd_path
end