Skip to content

Commit 3e4d763

Browse files
committed
Merge pull request #12 from kares/master
JRuby compatibility - replace popen with a tempfile
2 parents 0243477 + 1f4f79b commit 3e4d763

4 files changed

Lines changed: 26 additions & 94 deletions

File tree

lib/closure-compiler.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ module Closure
1212

1313
end
1414

15-
require 'stringio'
1615
require 'closure/compiler'

lib/closure/compiler.rb

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
require 'closure/popen'
1+
require 'stringio'
2+
require 'tempfile'
23

34
module Closure
45

@@ -20,28 +21,28 @@ def initialize(options={})
2021
# JavaScript as a string or yields an IO object containing the response to a
2122
# block, for streaming.
2223
def compile(io)
23-
result, error = nil, nil
24-
status = Closure::Popen.popen(command) do |stdin, stdout, stderr|
25-
if io.respond_to? :read
26-
while buffer = io.read(4096) do
27-
stdin.write(buffer)
28-
end
29-
else
30-
stdin.write(io.to_s)
31-
end
32-
stdin.close
33-
if Closure::Popen::WINDOWS
34-
stderr.close
35-
result = stdout.read
36-
error = "Stderr cannot be read on Windows."
37-
else
38-
out_thread = Thread.new { result = stdout.read }
39-
err_thread = Thread.new { error = stderr.read }
40-
out_thread.join and err_thread.join
24+
tempfile = Tempfile.new('closure_compiler')
25+
if io.respond_to? :read
26+
while buffer = io.read(4096) do
27+
tempfile.write(buffer)
4128
end
42-
yield(StringIO.new(result)) if block_given?
29+
else
30+
tempfile.write(io.to_s)
4331
end
44-
raise Error, error unless status.success?
32+
tempfile.flush
33+
34+
begin
35+
result = `#{command} --js #{tempfile.path}`
36+
rescue Exception
37+
raise Error, "compression failed"
38+
ensure
39+
tempfile.close!
40+
end
41+
unless $?.exitstatus.zero?
42+
raise Error, result
43+
end
44+
45+
yield(StringIO.new(result)) if block_given?
4546
result
4647
end
4748
alias_method :compress, :compile

lib/closure/popen.rb

Lines changed: 0 additions & 66 deletions
This file was deleted.

test/unit/test_closure_compiler.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ def test_block_syntax
3434
end
3535

3636
def test_jar_and_java_specifiation
37-
if !Closure::Popen::WINDOWS
38-
jar = Dir['vendor/closure-compiler-*.jar'].first
39-
java = `which java`.strip
40-
compiler = Compiler.new(:java => java, :jar_file => jar)
41-
assert compiler.compress(ORIGINAL) == COMPILED_SIMPLE
42-
end
37+
jar = Dir['vendor/closure-compiler-*.jar'].first
38+
java = `which java`.strip
39+
compiler = Compiler.new(:java => java, :jar_file => jar)
40+
assert compiler.compress(ORIGINAL) == COMPILED_SIMPLE
4341
end
4442

4543
def test_exceptions

0 commit comments

Comments
 (0)