Skip to content

Commit

Permalink
use Temfile instead of POpen piping (for JRuby compatibility)
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Apr 25, 2011
1 parent 70b2ab0 commit f9736a4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 94 deletions.
1 change: 0 additions & 1 deletion lib/closure-compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ module Closure

end

require 'stringio'
require 'closure/compiler'
41 changes: 20 additions & 21 deletions lib/closure/compiler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'closure/popen'
require 'stringio'
require 'tempfile'

module Closure

Expand All @@ -20,28 +21,26 @@ 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)
tempfile = Tempfile.new('closure_compiler')
if io.respond_to? :read
while buffer = io.read(4096) do
tempfile.write(buffer)
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
end
yield(StringIO.new(result)) if block_given?
else
tempfile.write(io.to_s)
end
tempfile.close

begin
result = `#{command} --js #{tempfile.path}`
rescue Exception
raise Error, "compression failed"
end
raise Error, error unless status.success?
unless $?.exitstatus.zero?
raise Error, result
end

yield(StringIO.new(result)) if block_given?
result
end
alias_method :compress, :compile
Expand Down
66 changes: 0 additions & 66 deletions lib/closure/popen.rb

This file was deleted.

10 changes: 4 additions & 6 deletions test/unit/test_closure_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f9736a4

Please sign in to comment.