Class: Epic::Compressor

Inherits:
Base
  • Object
show all
Includes:
Errors
Defined in:
lib/epic/compressor.rb

Overview

Compresses a file using the specified compressor/minifier (currently YUI Compressor 2.4.2).

Usage

Epic::Compressor.compress("filename.js")

Epic::Compressor.compress("filename.css")

Outputs the compressed file to path.min.

Options

"type"                "js" or "css". Identifies the file type. Only needed if the file does
                      not have a standard extension.

"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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Errors

#display_errors, #errors, #valid?

Methods inherited from Base

#base_path, #configuration, configuration, configure, #display_path, #tmp_path

Constructor Details

#initialize(path) ⇒ Compressor

Returns a new instance of Compressor.



36
37
38
39
40
41
42
43
44
# File 'lib/epic/compressor.rb', line 36

def initialize(path)
  @path = path
  if path =~ /\.js$/
    @type = :js
  elsif path =~ /\.css$/
    @type = :css
  end
  @errors = []
end

Instance Attribute Details

#pathObject

The path of the original file being compressed.



30
31
32
# File 'lib/epic/compressor.rb', line 30

def path
  @path
end

#typeObject

The type of file, whether :js or :css.



34
35
36
# File 'lib/epic/compressor.rb', line 34

def type
  @type
end

Instance Method Details

#compress(params = {}) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/epic/compressor.rb', line 46

def compress(params={})
  # if the compressed_file exists, don't create it again
  #
  compress_path(params) unless File.exists?(compressed_path)
  
  File.read(compressed_path)
end

#compress_path(params) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/epic/compressor.rb', line 62

def compress_path(params)
  params.stringify_keys!
  
  $stdout.print "   #{compressed_display_path}  compressing . . . "
  compressed = ''
  
  options_string = convert_params_to_string(params)

  result = execute_compressor(options_string)

  parse_errors(result)
  
  if valid?
    $stdout.puts "OK"
  else
    display_errors
    raise "JavaScript errors in #{compressed_display_path}"
  end          
end

#compressed_display_pathObject



58
59
60
# File 'lib/epic/compressor.rb', line 58

def compressed_display_path
  @compressed_display_path ||= display_path(path)
end

#compressed_pathObject



54
55
56
# File 'lib/epic/compressor.rb', line 54

def compressed_path
  @compressed_path ||= "#{path}.min"
end

#compressor_pathObject



129
130
131
132
133
# File 'lib/epic/compressor.rb', line 129

def compressor_path
  compressor_path = File.expand_path("#{vendor_path}/ext/yuicompressor-2.4.2.jar")
  raise "#{compressor_path} does not exist" unless File.exists?(compressor_path)
  compressor_path
end

#convert_params_to_string(params) ⇒ Object

join the options together for appending to the command line



103
104
105
106
# File 'lib/epic/compressor.rb', line 103

def convert_params_to_string(params)
  options = parse_options(params)
  options.map {|k,v| "--#{k} #{v}"}.join(" ")
end

#execute_compressor(options_string) ⇒ Object

call the compressor



84
85
86
87
# File 'lib/epic/compressor.rb', line 84

def execute_compressor(options_string)
  command = "java -jar #{compressor_path} #{options_string} #{path} -o #{compressed_path} 2>&1"
  result = F.execute(command, :return => true)          
end

#parse_errors(result = "") ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/epic/compressor.rb', line 89

def parse_errors(result="")
  result.to_s.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)

      errors << error
    end
  end          
end

#parse_options(options) ⇒ Object

set options and defaults



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/epic/compressor.rb', line 110

def parse_options(options)
  options["type"] ||= type
  options["type"] = options["type"].to_s
  
  if options.delete("obfuscate")
    options["nomunge"] = ""
  end
  if options.delete("verbose")
    options["verbose"] = ""
  end
  options["charset"] = "utf-8"

  if options["type"].to_s == "js" && !options["preserve_semi"]
    options["preserve-semi"] = ""
  end 
  
  options         
end

#vendor_pathObject



135
136
137
# File 'lib/epic/compressor.rb', line 135

def vendor_path
  File.join(File.dirname(__FILE__), "..", "..", "vendor")
end