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)
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

View file

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