Skip to content

Instantly share code, notes, and snippets.

@macek
Created June 15, 2010 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macek/439363 to your computer and use it in GitHub Desktop.
Save macek/439363 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require 'json'
response = Net::HTTP.post_form(
URI.parse("http://closure-compiler.appspot.com/compile"),
{
:js_code => STDIN.read,
:compilation_level => ENV['TM_CLOSURE_COMPILATION_LEVEL'] || 'SIMPLE_OPTIMIZATIONS',
:output_info => ["compiled_code","statistics"],
:output_format => "json"
}
)
# parse json
d = JSON::parse(response.body)
# create new file
new_file = "#{File.basename(ENV['TM_FILEPATH'], '.js')}.compiled.js"
File.open(new_file, "w") do |f|
f.write d["compiledCode"]
end
# open the file
`mate #{new_file}`
# statistics
s = d["statistics"]
# tooltip values
t = {
:in => s['originalSize'],
:out => s['compressedSize'],
:ratio => "%0.2f%%" % (s['compressedSize']*100.0/s['originalSize'])
}
# tooltip display
%x{ "$DIALOG" tooltip --text "Input: #{t[:in]} bytes\nOutput: #{t[:out]} bytes\nCompression: #{t[:ratio]}" }
{ shellVariables = (
{ name = 'TM_CLOSURE_COMPILATION_LEVEL';
value = 'SIMPLE_OPTIMIZATIONS';
},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment