class Sprockets::StaticAsset
def write_to(filename, options = {})
def write_to(filename, options = {}) # Gzip contents if filename has '.gz' options[:compress] ||= File.extname(filename) == '.gz' FileUtils.mkdir_p File.dirname(filename) if options[:compress] # Open file and run it through `Zlib` pathname.open('rb') do |rd| File.open("#{filename}+", 'wb') do |wr| gz = Zlib::GzipWriter.new(wr, Zlib::BEST_COMPRESSION) gz.mtime = mtime.to_i buf = "" while rd.read(16384, buf) gz.write(buf) end gz.close end end else # If no compression needs to be done, we can just copy it into place. FileUtils.cp(pathname, "#{filename}+") end # Atomic write FileUtils.mv("#{filename}+", filename) # Set mtime correctly File.utime(mtime, mtime, filename) nil ensure # Ensure tmp file gets cleaned up FileUtils.rm("#{filename}+") if File.exist?("#{filename}+") end