Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JRuby compatibility - replace popen with a tempfile #12

Merged
merged 2 commits into from Jul 28, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/closure-compiler.rb
Expand Up @@ -12,5 +12,4 @@ module Closure

end

require 'stringio'
require 'closure/compiler'
43 changes: 22 additions & 21 deletions lib/closure/compiler.rb
@@ -1,4 +1,5 @@
require 'closure/popen'
require 'stringio'
require 'tempfile'

module Closure

Expand All @@ -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)
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.flush

begin
result = `#{command} --js #{tempfile.path}`
rescue Exception
raise Error, "compression failed"
ensure
tempfile.close!
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
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