Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #57 from tenderlove/shell
Browse files Browse the repository at this point in the history
Reduce subshells
  • Loading branch information
josh committed Dec 5, 2011
2 parents 1c0af50 + cf60759 commit aaa25c4
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions lib/execjs/external_runtime.rb
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit aaa25c4

Please sign in to comment.