Skip to content

Instantly share code, notes, and snippets.

@ayosec
Created October 14, 2010 00:39
Show Gist options
  • Save ayosec/625286 to your computer and use it in GitHub Desktop.
Save ayosec/625286 to your computer and use it in GitHub Desktop.
# To be used with http://github.com/setepo/asset-pocket
#
# require 'clousure_compressor'
# compressor :clousure, :handler => ClousureCompressor.new
#
# js "public/javascripts/application.js" do
# compress :clousure
# use "app/views/js/*"
# end
class ClousureCompressor
def compress(content)
if ENV["JS_COMPRESS"] == "skip"
STDERR.puts "Skipping JavaScript compressor"
return content
end
if `which java`.empty?
STDERR.puts "*** Java was not found in your path."
STDERR.puts "*** You can install it with openjdk-6-jre-headless"
STDERR.puts "*** Java is needed for Google Clousure Compressor was not found."
STDERR.puts "*** The JavaScript code will be generated without any compression."
return content
end
jar_file = File.expand_path("~/.javascript_closure_compiler/compiler.jar")
if not File.exist?(jar_file)
STDERR.puts "*** Google Clousure Compressor was not found."
STDERR.puts "*** You still can generate the JavaScript files, but they can not be compressed."
STDERR.puts "*** It is recommended to download http://closure-compiler.googlecode.com/files/compiler-latest.zip and extract the compiler.jar file into #{jar_file}"
return content
end
require "open3"
stdin, stdout, stderr = Open3.popen3("java", "-jar", jar_file, "--compilation_level", "SIMPLE_OPTIMIZATIONS")
stdin.write content
stdin.close
STDERR.write stderr.read
compressed = stdout.read
compressed
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment