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

Run the compiler in-process on JRuby #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/closure-compiler.rb
Original file line number Diff line number Diff line change
@@ -13,3 +13,4 @@ module Closure
end

require 'closure/compiler'
require 'closure/jruby_runner' if defined?(JRUBY_VERSION)
19 changes: 12 additions & 7 deletions lib/closure/compiler.rb
Original file line number Diff line number Diff line change
@@ -52,14 +52,19 @@ def compile(io)
def compile_files(files)
@options.merge!({js: files})

begin
result = `#{command} 2>&1`
rescue Exception
raise Error, "compression failed: #{result}"
end
if defined?(JRUBY_VERSION)
args = serialize_options(@options)
result = JRubyRunner.run(args)
else
begin
result = `#{command} 2>&1`
rescue Exception
raise Error, "compression failed: #{result}"
end

unless $?.exitstatus.zero?
raise Error, result
unless $?.exitstatus.zero?
raise Error, result
end
end

yield(StringIO.new(result)) if block_given?
32 changes: 32 additions & 0 deletions lib/closure/jruby_runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'java'
module Closure
require COMPILER_JAR
class JRubyRunner < com.google.javascript.jscomp.CommandLineRunner
def self.run(args)
output_contents = java.io.ByteArrayOutputStream.new
output = java.io.PrintStream.new(output_contents)
error_contents = java.io.ByteArrayOutputStream.new
error = java.io.PrintStream.new(error_contents)
runner = JRubyRunner.new(args.to_java(:String), output, error)
runner.do_run
if !error_contents.to_s.empty?
raise Error, "compression failed: #{error_contents.to_s}"
end
output_contents.to_s
ensure
output.close
error.close
end

def createCompiler
compiler = com.google.javascript.jscomp.Compiler.new(getErrorPrintStream())
# It would be nice to leave threads enabled but if we do then
# any process using this must wait at least 60 seconds after
# using closure-compiler before the process will exit, due to
# the underlying JVM thread pool keeping threads alive until
# they time out, which is 60 seconds.
compiler.disable_threads
compiler
end
end
end