1- require 'closure/popen'
1+ require 'stringio'
2+ require 'tempfile'
23
34module 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
0 commit comments