Class: Facter::Core::Execution::Windows

Inherits:
Base
  • Object
show all
Defined in:
lib/facter/core/execution/windows.rb

Constant Summary collapse

DEFAULT_COMMAND_EXTENSIONS =
%w[.COM .EXE .BAT .CMD]
ABSOLUTE_PATH_REGEX =
%r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i
DOUBLE_QUOTED_COMMAND =
/^"(.+?)"(?:\s+(.*))?/

Instance Method Summary collapse

Methods inherited from Base

#execute, #with_env

Instance Method Details

#absolute_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/facter/core/execution/windows.rb', line 34

def absolute_path?(path)
  !! (path =~ ABSOLUTE_PATH_REGEX)
end

#expand_command(command) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/facter/core/execution/windows.rb', line 40

def expand_command(command)
  exe = nil
  args = nil

  if (match = command.match(DOUBLE_QUOTED_COMMAND))
    exe, args = match.captures
  else
    exe, args = command.split(/ /,2)
  end

  if exe and (expanded = which(exe))
    expanded = "\"#{expanded}\"" if expanded.match(/\s+/)
    expanded << " #{args}" if args

    return expanded
  end
end

#search_pathsObject



3
4
5
# File 'lib/facter/core/execution/windows.rb', line 3

def search_paths
  ENV['PATH'].split(File::PATH_SEPARATOR)
end

#which(bin) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/facter/core/execution/windows.rb', line 9

def which(bin)
  if absolute_path?(bin)
    return bin if File.executable?(bin)
  else
    search_paths.each do |dir|
      dest = File.join(dir, bin)
      dest.gsub!(File::SEPARATOR, File::ALT_SEPARATOR)
      if File.extname(dest).empty?
        exts = ENV['PATHEXT']
        exts = exts ? exts.split(File::PATH_SEPARATOR) : DEFAULT_COMMAND_EXTENSIONS
        exts.each do |ext|
          destext = dest + ext
          return destext if File.executable?(destext)
        end
      end
      return dest if File.executable?(dest)
    end
  end
  nil
end