Permalink
Browse files

Merge pull request #12 from kares/master

JRuby compatibility - replace popen with a tempfile
  • Loading branch information...
2 parents 0243477 + 1f4f79b commit 3e4d763c618e3d1a585f8d32af0d56f1f8a64774 @jashkenas jashkenas committed Jul 28, 2011
Showing with 26 additions and 94 deletions.
  1. +0 −1 lib/closure-compiler.rb
  2. +22 −21 lib/closure/compiler.rb
  3. +0 −66 lib/closure/popen.rb
  4. +4 −6 test/unit/test_closure_compiler.rb
View
@@ -12,5 +12,4 @@ module Closure
end
-require 'stringio'
require 'closure/compiler'
View
@@ -1,4 +1,5 @@
-require 'closure/popen'
+require 'stringio'
+require 'tempfile'
module Closure
@@ -20,28 +21,28 @@ def initialize(options={})
# JavaScript as a string or yields an IO object containing the response to a
# block, for streaming.
def compile(io)
- result, error = nil, nil
- status = Closure::Popen.popen(command) do |stdin, stdout, stderr|
- if io.respond_to? :read
- while buffer = io.read(4096) do
- stdin.write(buffer)
- end
- else
- stdin.write(io.to_s)
- end
- stdin.close
- if Closure::Popen::WINDOWS
- stderr.close
- result = stdout.read
- error = "Stderr cannot be read on Windows."
- else
- out_thread = Thread.new { result = stdout.read }
- err_thread = Thread.new { error = stderr.read }
- out_thread.join and err_thread.join
+ tempfile = Tempfile.new('closure_compiler')
+ if io.respond_to? :read
+ while buffer = io.read(4096) do
+ tempfile.write(buffer)
end
- yield(StringIO.new(result)) if block_given?
+ else
+ tempfile.write(io.to_s)
end
- raise Error, error unless status.success?
+ tempfile.flush
+
+ begin
+ result = `#{command} --js #{tempfile.path}`
+ rescue Exception
+ raise Error, "compression failed"
+ ensure
+ tempfile.close!
+ end
+ unless $?.exitstatus.zero?
+ raise Error, result
+ end
+
+ yield(StringIO.new(result)) if block_given?
result
end
alias_method :compress, :compile
View
@@ -1,66 +0,0 @@
-module Closure
-
- # A slightly modified version of Ruby 1.8's Open3, that doesn't use a
- # grandchild process, and returns the pid of the external process.
- module Popen
-
- WINDOWS = RUBY_PLATFORM.match(/(win|w)32$/)
- ONE_NINE = RUBY_VERSION >= "1.9"
- if WINDOWS
- if ONE_NINE
- require 'open3'
- else
- require 'rubygems'
- require 'win32/open3'
- end
- end
-
- def self.popen(cmd)
- if WINDOWS
- error = nil
- Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thread|
- yield(stdin, stdout, stderr) if block_given?
- stdout.read unless stdout.closed? or stdout.eof?
- unless stderr.closed?
- stderr.rewind
- error = stderr.read
- end
- return wait_thread.value if wait_thread.is_a? Thread
- end
- else
- # pipe[0] for read, pipe[1] for write
- pw, pr, pe = IO.pipe, IO.pipe, IO.pipe
-
- pid = fork {
- pw[1].close
- STDIN.reopen(pw[0])
- pw[0].close
-
- pr[0].close
- STDOUT.reopen(pr[1])
- pr[1].close
-
- pe[0].close
- STDERR.reopen(pe[1])
- pe[1].close
-
- exec(cmd)
- }
-
- pw[0].close
- pr[1].close
- pe[1].close
- pi = [pw[1], pr[0], pe[0]]
- pw[1].sync = true
- begin
- yield(*pi) if block_given?
- ensure
- pi.each{|p| p.close unless p.closed?}
- end
- Process.waitpid pid
- end
- $?
- end
-
- end
-end
@@ -34,12 +34,10 @@ def test_block_syntax
end
def test_jar_and_java_specifiation
- if !Closure::Popen::WINDOWS
- jar = Dir['vendor/closure-compiler-*.jar'].first
- java = `which java`.strip
- compiler = Compiler.new(:java => java, :jar_file => jar)
- assert compiler.compress(ORIGINAL) == COMPILED_SIMPLE
- end
+ jar = Dir['vendor/closure-compiler-*.jar'].first
+ java = `which java`.strip
+ compiler = Compiler.new(:java => java, :jar_file => jar)
+ assert compiler.compress(ORIGINAL) == COMPILED_SIMPLE
end
def test_exceptions

0 comments on commit 3e4d763

Please sign in to comment.