Module: Shr::Which

Defined in:
lib/shr/which.rb

Constant Summary collapse

@@path =
ENV['PATH']
@@builtins =
%w{
  assoc
  call
  cd chdir
  cls
  color
  copy
  date
  del erace
  dir
  echo
  exit
  ftype
  md mkdir
  mklink
  move
  path
  pause
  popd
  prompt
  pushd
  rd rmdir
  ren rename
  set
  start
  time
  title
  type
  var
  verify
  vol
}

Class Method Summary collapse

Class Method Details

.add_extensions(program) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/shr/which.rb', line 69

def add_extensions(program)
  if OS.windows?
    # add .bat, .exe, etc.
    extensions = ENV['PATHEXT'].split(';')
    [program].product(extensions).map(&:join)
  else
    [program]
  end
end

.builtins?(program) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/shr/which.rb', line 65

def builtins?(program)
  @@builtins.include? program.to_s if OS.windows?
end

.exist?(program) ⇒ Boolean Also known as: exists?

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/shr/which.rb', line 51

def exist?(program)
  programs = add_extensions(program)

  entries = paths.product(programs).map { |list| list.join '/' }
  result = entries.find do |exe|
    File.executable?(exe) && !File.directory?(exe)
  end

  result = program.to_s if builtins?(program.to_s)
  result
end

.pathsObject



79
80
81
82
83
# File 'lib/shr/which.rb', line 79

def paths
  @@path.split(File::PATH_SEPARATOR).map! do |path|
    OS.windows? ? path.gsub('\\', '/') : path
  end
end