after_build support in class-based extension

This commit is contained in:
Thomas Reynolds 2013-05-24 15:49:15 -07:00
parent 7611f4ef4a
commit 5104579623
2 changed files with 26 additions and 18 deletions

View file

@ -165,6 +165,16 @@ module Middleman
ext.app.sitemap.register_resource_list_manipulator(ext.class.extension_name, ext) ext.app.sitemap.register_resource_list_manipulator(ext.class.extension_name, ext)
end end
end end
if ext.respond_to?(:after_build)
klass.after_build do |builder|
if ext.method(:after_build).arity === 1
ext.after_build(builder)
else
ext.after_build
end
end
end
end end
end end
end end

View file

@ -18,30 +18,28 @@ class Middleman::Extensions::Gzip < ::Middleman::Extension
require 'zlib' require 'zlib'
require 'stringio' require 'stringio'
require 'find' require 'find'
end
gzip_ext = self def after_build(builder)
paths = ::Middleman::Util.all_files_under(app.build_dir)
total_savings = 0
app.after_build do |builder| paths.each do |path|
paths = ::Middleman::Util.all_files_under(self.class.inst.build_dir) next unless options.exts.include? path.extname
total_savings = 0
paths.each do |path| output_filename, old_size, new_size = gzip_file(path.to_s)
next unless gzip_ext.options.exts.include? path.extname
output_filename, old_size, new_size = gzip_ext.gzip_file(path.to_s) if output_filename
total_savings += (old_size - new_size)
if output_filename size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger'
total_savings += (old_size - new_size) old_locale = I18n.locale
size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger' I18n.locale = :en # use the english localizations for printing out file sizes to make sure the localizations exist
old_locale = I18n.locale builder.say_status :gzip, "#{output_filename} (#{app.number_to_human_size((old_size - new_size).abs)} #{size_change_word})"
I18n.locale = :en # use the english localizations for printing out file sizes to make sure the localizations exist I18n.locale = old_locale
builder.say_status :gzip, "#{output_filename} (#{number_to_human_size((old_size - new_size).abs)} #{size_change_word})"
I18n.locale = old_locale
end
end end
builder.say_status :gzip, "Total gzip savings: #{number_to_human_size(total_savings)}", :blue
end end
builder.say_status :gzip, "Total gzip savings: #{app.number_to_human_size(total_savings)}", :blue
end end
def gzip_file(path) def gzip_file(path)