lib/locomotive/steam/initializers/sprockets.rb in locomotivecms_steam-1.1.0.rc3 vs lib/locomotive/steam/initializers/sprockets.rb in locomotivecms_steam-1.1.0
- old
+ new
@@ -1,13 +1,70 @@
require 'sprockets'
require 'sass'
require 'coffee_script'
require 'compass'
require 'autoprefixer-rails'
+require 'open3'
module Locomotive::Steam
+ class YUICompressorRuntimeError < RuntimeError
+ attr_reader :errors
+ #:nocov:
+ def initialize(msg, errors)
+ super(msg)
+ @errors = errors
+ end
+ end
+
+ module YUICompressorErrors
+
+ #:nocov:
+ def compress(stream_or_string)
+ streamify(stream_or_string) do |stream|
+ tempfile = new_tempfile(stream)
+ full_command = "%s %s" % [command, tempfile.path]
+
+ output, errors, exit_status = _compress(full_command, tempfile)
+
+ if exit_status.exitstatus.zero?
+ output
+ else
+ # Bourne shells tend to blow up here when the command fails, usually
+ # because java is missing
+ raise YUICompressorRuntimeError.new("Command '%s' returned non-zero exit status" %
+ full_command, errors)
+ end
+ end
+ end
+
+ #:nocov:
+ def _compress(command, tempfile)
+ begin
+ # FIXME: catch only useful information from the stderr output
+ # output, errors, exit_status = '', [], nil
+ output, errors, exit_status = Open3.capture3(command)
+ errors = errors.split("\n").find_all { |l| l =~ /\s+[0-9]/ }
+ [output, errors, exit_status]
+ rescue Exception => e
+ # windows shells tend to blow up here when the command fails
+ raise RuntimeError, "compression failed: %s" % e.message
+ ensure
+ tempfile.close!
+ end
+ end
+
+ #:nocov:
+ def new_tempfile(stream)
+ Tempfile.new('yui_compress').tap do |tempfile|
+ tempfile.write stream.read
+ tempfile.flush
+ end
+ end
+
+ end
+
class SprocketsEnvironment < ::Sprockets::Environment
def initialize(root, options = {})
super(root)
@@ -31,12 +88,16 @@
end
def install_yui_compressor(options)
return unless options[:minify]
- require 'yui/compressor'
-
if is_java_installed?
+ require 'yui/compressor'
+
+ [YUI::JavaScriptCompressor, YUI::CssCompressor].each do |klass|
+ klass.send(:include, YUICompressorErrors)
+ end
+
# minify javascripts and stylesheets
self.js_compressor = YUI::JavaScriptCompressor.new
self.css_compressor = YUI::CssCompressor.new
else
message = "[Important] YUICompressor requires java to be installed. The JAVA_HOME variable should also be set.\n"