Class: YMDP::Compressor::Base
- Defined in:
- lib/ymdp/compressor/compressor.rb
Overview
Compresses a file using the specified compressor/minifier (currently YUI Compressor 2.4.2).
Usage
YMDP::Compressor::Base.compress("filename.js", "type" => "js")
YMDP::Compressor::Base.compress("filename.css", "type" => "css")
Outputs the compressed file to path.min.
Options
"type":: "js" or "css". Identifies the file type.
"obfuscate":: true or false. Change internal variable names within the code.
"verbose":: true or false. Output warnings about code quality.
"preserve_semi":: true or false. Preserve unnecessary semicolons.
TODO: Make it support multiple compressors.
TODO: Convert it to an object, so you instantiate the Compressor class and then call it on a file or a string.
TODO: Get some stringify_keys! action going so you can send in symbols or strings.
Direct Known Subclasses
Class Method Summary collapse
Methods inherited from Base
#base_path, base_path, #configuration, configuration, configure, #content_variables, display_path, #display_path, #paths, #servers
Class Method Details
.compress(path, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ymdp/compressor/compressor.rb', line 36 def self.compress(path, ={}) .stringify_keys! compressed_display_path = display_path(path) compressed_path = "#{path}.min" ["type"] ||= "js" ["type"] = ["type"].to_s # if the compressed_file exists, don't create it again # unless File.exists?(compressed_path) $stdout.print " #{compressed_display_path} compressing . . . " compressed = '' # set options and defaults # if .delete("obfuscate") ["nomunge"] = "" end if .delete("verbose") ["verbose"] = "" end ["charset"] = "utf-8" if ["type"].to_s == "js" && !["preserve_semi"] ["preserve-semi"] = "" end # join the options together for appending to the command line # = .map {|k,v| "--#{k} #{v}"}.join(" ") # call the compressor # compressor_path = File.("#{File.dirname(__FILE__)}/yuicompressor-2.4.2.jar") raise "#{compressor_path} does not exist" unless File.exists?(compressor_path) command = "java -jar #{compressor_path} #{options_string} #{path} -o #{compressed_path} 2>&1" result = F.execute(command, :return => true) result.split("\n").each do |line| if line =~ /\[ERROR\] (\d+):(\d+):(.*)/ line_number = $1.to_i error = "Error at #{compressed_display_path} line #{line_number} character #{$2}: #{$3}" error += F.get_line_from_file(path, line_number) $stdout.puts error g(error) end end if result =~ /ERROR/ raise "JavaScript errors in #{compressed_display_path}" else $stdout.puts "OK" end end # The compressed file should exist now. If it does, use it. If not, raise an error. # if File.exists?(compressed_path) compressed = File.read(compressed_path) else raise "File does not exist: #{compressed_display_path}" end compressed end |