Permalink
Browse files

use Ruby to search ENV[PATH] rather than shelling out

  • Loading branch information...
1 parent dc5951b commit cf60759e7715505ca977c239868fe5660ae83398 @tenderlove committed Dec 5, 2011
Showing with 21 additions and 10 deletions.
  1. +21 −10 lib/execjs/external_runtime.rb
@@ -121,6 +121,22 @@ def binary
@binary ||= locate_binary
end
+ def which_windows(name)
+ result = `#{shell_escape("#{ExecJS.root}/support/which.bat", name)}`
+ result.strip.split("\n").first
+ end
+
+ def which_unix(name)
+ if File.executable? cmd
+ cmd
+ else
+ path = ENV['PATH'].split(File::PATH_SEPARATOR).find { |path|
+ File.executable? File.join(path, cmd)
+ }
+ path && File.expand_path(cmd, path)
+ end
+ end
+
protected
def runner_source
@runner_source ||= IO.read(@runner_path)
@@ -147,19 +163,14 @@ def locate_binary
end
def which(command)
- Array(command).each do |name|
+ Array(command).find do |name|
name, args = name.split(/\s+/, 2)
- result = if ExecJS.windows?
- `#{shell_escape("#{ExecJS.root}/support/which.bat", name)}`
- else
- `#{shell_escape('command', '-v', name)} 2>/dev/null`
- end
+ path = ExecJS.windows? ? which_windows(name) : which_unix(name)
- if path = result.strip.split("\n").first
- return args ? "#{path} #{args}" : path
- end
+ next unless path
+
+ args ? "#{path} #{args}" : path
end
- nil
end
if "".respond_to?(:force_encoding)

0 comments on commit cf60759

Please sign in to comment.