diff options
author | Ho-Sheng Hsiao <hosheng.hsiao@gmail.com> | 2012-03-30 22:13:19 -0400 |
---|---|---|
committer | Ho-Sheng Hsiao <hosh@opscode.com> | 2012-05-10 14:51:43 -0400 |
commit | cbc6c7261db77d945791de8e9bdef5f36ccfa0c4 (patch) | |
tree | 79f2a8334d74adc48672c14079f0ecf4842bec7e /lib | |
parent | 219f51cf15783077ce483b5bdbc56601e37827fd (diff) | |
download | mixlib-shellout-cbc6c7261db77d945791de8e9bdef5f36ccfa0c4.tar.gz |
[CHEF-2994][WINDOWS] Factored out utility methods to Mixlib::ShellOut::Windows::Utils
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mixlib/shellout/windows.rb | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/lib/mixlib/shellout/windows.rb b/lib/mixlib/shellout/windows.rb index 94095cf..fce16a8 100644 --- a/lib/mixlib/shellout/windows.rb +++ b/lib/mixlib/shellout/windows.rb @@ -166,8 +166,8 @@ module Mixlib return [ nil, command ] if candidate.length == 0 # Check if the exe exists directly. Otherwise, search PATH. - exe = find_exe_at_location(candidate) - exe = which(unquoted_executable_path(command)) if exe.nil? && exe !~ /[\\\/]/ + exe = Utils.find_executable(candidate) + exe = Utils.which(unquoted_executable_path(command)) if exe.nil? && exe !~ /[\\\/]/ if exe.nil? || exe =~ IS_BATCH_FILE # Batch files MUST use cmd; and if we couldn't find the command we're looking for, we assume it must be a cmd builtin. @@ -195,7 +195,6 @@ module Mixlib end end - def inherit_environment result = {} ENV.each_pair do |k,v| @@ -212,25 +211,32 @@ module Mixlib result end - def pathext - @pathext ||= ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') + [''] : [''] - end + module Utils + def self.pathext + @pathext ||= ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') + [''] : [''] + end - def which(cmd) - ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| - exe = find_exe_at_location("#{path}/${cmd}") - return exe if exe + # which() mimicks the Unix which command + def self.which(cmd) + ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| + exe = find_executable("#{path}/${cmd}") + return exe if exe + end + return nil end - return nil - end - def find_exe_at_location(path) - return path if File.executable? path - pathext.each do |ext| - exe = "#{path}#{ext}" - return exe if File.executable? exe + # Windows has a different notion of what "executable" means + # The OS will search through valid the extensions and look + # for a binary there. + def self.find_executable(path) + return path if File.executable? path + + pathext.each do |ext| + exe = "#{path}#{ext}" + return exe if File.executable? exe + end + return nil end - return nil end end # class end |