Permalink
Browse files

Add `which` support for Windows

  • Loading branch information...
sstephenson committed Feb 7, 2011
1 parent 910ba90 commit 3cf772ef19a6429690bcc95e66eba61b6c545a42
Showing with 20 additions and 2 deletions.
  1. +6 −0 lib/execjs.rb
  2. +12 −2 lib/execjs/external_runtime.rb
  3. +2 −0 lib/execjs/support/which.bat
View
@@ -1,3 +1,5 @@
+require "rbconfig"
+
module ExecJS
class Error < ::StandardError; end
class RuntimeError < Error; end
@@ -23,4 +25,8 @@ def self.runtime
def self.root
@root ||= File.expand_path("../execjs", __FILE__)
end
+
+ def self.windows?
+ @windows ||= RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
+ end
end
@@ -29,8 +29,7 @@ def available?
protected
def locate_binary
- @command = @command.join(" ") if @command.is_a?(Array)
- if binary = `which #{@command}`.split("\n").first
+ if binary = which(@command)
if @test_args
output = `#{binary} #{@test_args} 2>&1`
binary if output.match(@test_match)
@@ -40,6 +39,17 @@ def locate_binary
end
end
+ def which(command)
+ Array(command).find do |name|
+ result = if ExecJS.windows?
+ `#{ExecJS.root}/support/which.bat #{name}`
+ else
+ `which #{name} 2>&1`
+ end
+ result.strip.split("\n").first
+ end
+ end
+
def compile(source)
runner_source.sub('#{source}', source)
end
@@ -0,0 +1,2 @@
+@rem Taken from http://blogs.msdn.com/b/oldnewthing/archive/2005/01/20/357225.aspx
+@for %%e in (%PATHEXT%) do @for %%i in (%1%%e) do @if NOT "%%~$PATH:i"=="" @echo %%~$PATH:i

0 comments on commit 3cf772e

Please sign in to comment.