2009-10-29 12:17:15 -04:00
|
|
|
require "middleman/builder"
|
2009-10-22 20:25:15 -04:00
|
|
|
|
|
|
|
module Middleman
|
2009-10-29 12:17:15 -04:00
|
|
|
class Builder
|
|
|
|
alias_method :pre_smush_after_run, :after_run
|
|
|
|
def after_run
|
|
|
|
pre_smush_after_run
|
|
|
|
smush_dir = File.join(Middleman::Base.build_dir, Middleman::Base.images_dir)
|
|
|
|
|
|
|
|
# Read cache
|
|
|
|
cache_file = File.join(Middleman::Base.root, ".smush-cache")
|
|
|
|
cache_data = if File.exists?(cache_file)
|
|
|
|
Marshal.restore(File.read(cache_file))
|
2009-10-22 20:25:15 -04:00
|
|
|
else
|
2009-10-29 12:17:15 -04:00
|
|
|
{}
|
2009-10-22 20:25:15 -04:00
|
|
|
end
|
2009-10-29 12:17:15 -04:00
|
|
|
|
2009-11-24 17:24:20 -05:00
|
|
|
require "smusher"
|
|
|
|
require "json/pure"
|
2009-10-29 12:17:15 -04:00
|
|
|
::Smusher.class_eval do
|
|
|
|
images_in_folder(smush_dir).each do |file|
|
|
|
|
original_file_size = size(file)
|
|
|
|
return if original_file_size.zero?
|
|
|
|
return if cache_data[file] && cache_data[file] == original_file_size
|
2009-10-22 20:25:15 -04:00
|
|
|
|
2009-10-29 12:17:15 -04:00
|
|
|
with_logging(file, true) do
|
|
|
|
write_optimized_data(file)
|
|
|
|
cache_data[file] = size(file) # Add or update cache
|
|
|
|
File.open(cache_file, "w") { |f| f.write Marshal.dump(cache_data) } # Write cache
|
|
|
|
say "<%= color('#{"[SMUSHED]".rjust(12)}', :yellow) %> " + file.gsub(Middleman::Base.build_dir+"/", '')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-10-22 20:25:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|