def assets_compressor(kind, sources)
options = sources.extract_options!.symbolize_keys
bundle = options.delete(:cache)
return sources if bundle.nil?
began_at = Time.now
asset_folder = case kind
when :css then 'stylesheets'
when :js then 'javascripts'
else raise "YUI Compressor didn't support yet #{kind} compression"
end
bundle = settings.app_name.downcase if bundle.is_a?(TrueClass)
path = Padrino.root("public", uri_root_path(asset_folder, bundle.to_s))
stamps = sources.inject(0) do |memo, source|
asset_path_without_compression(kind, source) =~ /\?(\d{10})$/
memo += $1.to_i
memo
end
bundle = "#{bundle}.#{stamps}" if stamps > 0
path = Padrino.root("public", uri_root_path(asset_folder, bundle.to_s))
path += ".#{kind}" unless path =~ /\.#{kind}/
return bundle if File.exist?(path)
Dir[path.gsub(/\.\d{10}\.#{kind}/, "*")].each { |file| FileUtils.rm_f(file) }
errors = []
code = sources.map do |source|
source = asset_path(kind, source).sub(/\?\d{10}$/, '')
begin
source = source =~ /^http/ ? open(source) : File.read(Padrino.root("public", source))
rescue Exception => e
logger.error e.message
errors << source
next
end
if cs = source =~ /\/\*\!/
cr = source.slice(cs, source.length)
ce = cr =~ /\*\//
cr = source.slice(cs, ce+2)
source.sub!(cr,'')
end
source.each_line.reject { |l| l.strip == "" }.join
end
Dir.mkdir(File.dirname(path)) unless File.exist?(File.dirname(path))
File.open(path, "w") { |f| f.write(settings.compressor[kind].compress(code.join("\n"))) }
logger.debug "Compressed (%0.2fms) %s" % [Time.now-began_at, path] if defined?(logger)
errors.unshift bundle
end