Class: Closure::Compiler
- Inherits:
-
Object
- Object
- Closure::Compiler
- Defined in:
- lib/closure/compiler.rb
Overview
The Closure::Compiler is a basic wrapper around the actual JAR. There’s not much to see here.
Instance Method Summary collapse
-
#compile(io) ⇒ Object
(also: #compress)
Can compile a JavaScript string or open IO object.
-
#initialize(options = {}) ⇒ Compiler
constructor
When you create a Compiler, pass in the flags and options.
Constructor Details
#initialize(options = {}) ⇒ Compiler
When you create a Compiler, pass in the flags and options.
15 16 17 18 19 |
# File 'lib/closure/compiler.rb', line 15 def initialize(={}) @java = .delete(:java) || JAVA_COMMAND @jar = .delete(:jar_file) || COMPILER_JAR @options = () end |
Instance Method Details
#compile(io) ⇒ Object Also known as: compress
Can compile a JavaScript string or open IO object. Returns the compiled JavaScript as a string or yields an IO object containing the response to a block, for streaming.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/closure/compiler.rb', line 24 def compile(io) result, error = nil, nil status = POpen4.popen4(*command) do |stdout, stderr, stdin, pid| 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 RUBY_PLATFORM =~ /mswin32/ stderr.close result = stdout.read error = "unknown" 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? end raise Error, error unless status.success? result end |