Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #57 from tenderlove/shell

Reduce subshells
  • Loading branch information...
commit aaa25c49259648b28de2c32fab8c48ae9fb61513 2 parents 1c0af50 + cf60759
@josh josh authored
Showing with 29 additions and 13 deletions.
  1. +29 −13 lib/execjs/external_runtime.rb
View
42 lib/execjs/external_runtime.rb
@@ -94,7 +94,7 @@ def initialize(options)
@test_args = options[:test_args]
@test_match = options[:test_match]
@encoding = options[:encoding]
- @binary = locate_binary
+ @binary = nil
end
def exec(source)
@@ -113,16 +113,37 @@ def compile(source)
def available?
require "multi_json"
- @binary ? true : false
+ binary ? true : false
end
+ private
+ 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)
end
def exec_runtime(filename)
- output = sh("#{shell_escape(*(@binary.split(' ') << filename))} 2>&1")
+ output = sh("#{shell_escape(*(binary.split(' ') << filename))} 2>&1")
if $?.success?
output
else
@@ -142,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)
Please sign in to comment.
Something went wrong with that request. Please try again.