Class: Juicer::Minifyer::YuiCompressor

Inherits:
Object
  • Object
show all
Includes:
Chainable, JavaBase
Defined in:
lib/juicer/minifyer/yui_compressor.rb

Overview

Provides an interface to the YUI compressor library using Juicer::Shell::Binary. The YUI compressor library is implemented using Java, and as such Java is required when running this code. Also, the YUI jar file has to be provided.

The YUI Compressor is invoked using the java binary and the YUI Compressor jar file.

Providing the Jar file (usually yuicompressor-x.y.z.jar) can be done in several ways. The following directories are searched (in preferred order)

1. The directory specified by the option :bin_path
2. The directory specified by the environment variable $YUIC_HOME, if set
3. Current working directory

For more information on how the Jar is located, see Juicer::Minify::YuiCompressor.locate_jar

Author

Christian Johansen ([email protected])

Copyright

Copyright © 2008-2009 Christian Johansen

License

MIT

Usage example =

yuic = Juicer::Minifyer::YuiCompressor.new yuic.java = “/usr/local/bin/java” # If ‘java’ is not on path yuic.path << “/home/user/java/yui_compressor/” yuic.save(“”, “”)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Chainable

included, #next_in_chain, #next_in_chain=

Methods included from JavaBase

#command, #initialize, #jar_args, #set_opts

Methods included from Binary

#command, #execute, #get_opt, #initialize, #locate, #method_missing, #options, #path, #set_opt, #set_opts

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Juicer::Binary

Class Method Details

.bin_base_nameObject



70
71
72
# File 'lib/juicer/minifyer/yui_compressor.rb', line 70

def self.bin_base_name
  "yuicompressor"
end

.env_nameObject



74
75
76
# File 'lib/juicer/minifyer/yui_compressor.rb', line 74

def self.env_name
  "YUIC_HOME"
end

Instance Method Details

#save(file, output = nil, type = nil) ⇒ Object

Compresses a file using the YUI Compressor. Note that the :bin_path option needs to be set in order for YuiCompressor to find and use the YUI jar file. Please refer to the class documentation for how to set this.

file = The file to compress output = A file or stream to save the results to. If not provided the

original file will be overwritten

type = Either :js or :css. If this parameter is not provided, the type

is guessed from the suffix on the input file name


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/juicer/minifyer/yui_compressor.rb', line 52

def save(file, output = nil, type = nil)
  type = type.nil? ? file.split('.')[-1].to_sym : type

  output ||= file
  use_tmp = !output.is_a?(String)
  output = File.join(Dir::tmpdir, File.basename(file) + '.min.tmp.' + type.to_s) if use_tmp
  FileUtils.mkdir_p(File.dirname(output))

  result = execute("-jar", "#{locate_jar}#{jar_args}", "-o", output, file)

  if use_tmp                            # If no output file is provided, YUI compressor will
    output.puts IO.read(output)         # compress to a temp file. This file should be cleared
    File.delete(output)                 # out after we fetch its contents.
  end
end