From 51045796238526ba9d4401d0511a244757be9322 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Fri, 24 May 2013 15:49:15 -0700 Subject: [PATCH] after_build support in class-based extension --- .../lib/middleman-core/extensions.rb | 10 ++++++ .../lib/middleman-more/extensions/gzip.rb | 34 +++++++++---------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/middleman-core/lib/middleman-core/extensions.rb b/middleman-core/lib/middleman-core/extensions.rb index 32571b8c..aceabbad 100644 --- a/middleman-core/lib/middleman-core/extensions.rb +++ b/middleman-core/lib/middleman-core/extensions.rb @@ -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 diff --git a/middleman-core/lib/middleman-more/extensions/gzip.rb b/middleman-core/lib/middleman-more/extensions/gzip.rb index d979c935..f5ff6d38 100644 --- a/middleman-core/lib/middleman-more/extensions/gzip.rb +++ b/middleman-core/lib/middleman-more/extensions/gzip.rb @@ -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)