Class: Stylist::Processors::YuiCompressorProcessor

Inherits:
Processor
  • Object
show all
Defined in:
lib/stylist/processors/yui_compressor_processor.rb

Defined Under Namespace

Classes: MinificationCommandFailed

Class Method Summary collapse

Methods inherited from Processor

configuration, expand_stylesheet_sources

Class Method Details

.command_optionsObject



34
35
36
37
38
39
40
# File 'lib/stylist/processors/yui_compressor_processor.rb', line 34

def command_options
  config = configuration.yui_compressor
  { :type => 'css', :charset => config.charset, :line_break => nil }.inject([]) do |options, (name, argument)|
    options.concat(["--#{name.to_s.gsub '_', '-'}", argument.to_s]) if argument
    options
  end
end

.configureObject



7
8
9
# File 'lib/stylist/processors/yui_compressor_processor.rb', line 7

def configure
  configuration.add_option_group :yui_compressor, { :path_to_jar => nil, :path_to_java => 'java', :charset => 'utf-8', :line_break => nil, :production_only => true }
end

.minified_stylesheet_name_for(stylesheet) ⇒ Object



42
43
44
45
# File 'lib/stylist/processors/yui_compressor_processor.rb', line 42

def minified_stylesheet_name_for(stylesheet)
  dir, filename = stylesheet.split
  dir.join([filename.basename(filename.extname), '.min', filename.extname].join)
end

.minify(stylesheet) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/stylist/processors/yui_compressor_processor.rb', line 26

def minify(stylesheet)
  config = configuration.yui_compressor
  command = [config.path_to_java, '-jar', config.path_to_jar, '-o', minified_stylesheet_name_for(stylesheet), *command_options] << stylesheet
  unless system(*command) 
    raise MinificationCommandFailed, "Oops! Minification command failed with error code: #{$?}. The command executed was: #{command.join ' '}"
  end
end

.process!(collection) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/stylist/processors/yui_compressor_processor.rb', line 11

def process!(collection)
  return if configuration.yui_compressor.production_only and not Rails.env.production?
  
  stylesheets_to_process(collection).each do |stylesheet|
    minify(stylesheet)
  end
  replace_files_in_collection_with_minified_files(collection)
end

.replace_files_in_collection_with_minified_files(collection) ⇒ Object



20
21
22
23
24
# File 'lib/stylist/processors/yui_compressor_processor.rb', line 20

def replace_files_in_collection_with_minified_files(collection)
  collection.each_pair do |media, stylesheets|
    collection[media] = expand_stylesheet_sources(stylesheets).map { |stylesheet| minified_stylesheet_name_for(stylesheet).basename.to_s }
  end
end

.source_has_been_changed?(file) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/stylist/processors/yui_compressor_processor.rb', line 54

def source_has_been_changed?(file)
  minified_path = minified_stylesheet_name_for(file)
  minified_path.exist? && (File.new(file).mtime >= File.new(minified_path).mtime)
end

.stylesheets_to_process(collection) ⇒ Object



47
48
49
50
51
52
# File 'lib/stylist/processors/yui_compressor_processor.rb', line 47

def stylesheets_to_process(collection)
  expand_stylesheet_sources(collection.values.flatten).select do |source_file|
    minified_file = minified_stylesheet_name_for(source_file)
    (minified_file.exist? and source_has_been_changed?(source_file)) or (source_file.exist? and not minified_file.exist?)
  end
end