Class: Closure::Compiler

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(options = {}) ⇒ Compiler

When you create a Compiler, pass in the flags and options.



11
12
13
14
15
# File 'lib/closure/compiler.rb', line 11

def initialize(options={})
  @java     = options.delete(:java)     || JAVA_COMMAND
  @jar      = options.delete(:jar_file) || COMPILER_JAR
  @options  = serialize_options(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.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/closure/compiler.rb', line 20

def compile(io)
  Open3.popen3(*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)
    end
    stdin.close
    block_given? ? yield(stdout) : stdout.read
  end
end