Reimplement smusher for thor

This commit is contained in:
tdreyno 2011-01-30 15:11:54 -08:00
parent d643a8e041
commit c6b0723156
5 changed files with 56 additions and 38 deletions

View file

@ -4,10 +4,6 @@ ENV['MM_ENV'] = "build"
# Require app
require File.join(File.dirname(__FILE__), "..", "lib", "middleman")
require 'middleman/builder'
# Initialize server once so features are loaded
Middleman::Server.new
# Middleman::ThorBuilder.source_root(Dir.pwd)
Middleman::Builder.start

View file

@ -12,7 +12,7 @@ module Middleman
source = File.expand_path(find_in_source_paths(source.to_s))
context = instance_eval('binding')
@@rack_test ||= Rack::Test::Session.new(Rack::MockSession.new(Middleman::Server))
@@rack_test ||= ::Rack::Test::Session.new(::Rack::MockSession.new(Middleman::Server))
create_file destination, nil, config do
# The default render just requests the page over Rack and writes the response
@ -28,6 +28,7 @@ module Middleman
include Middleman::ThorActions
def initialize(*args)
Middleman::Server.new
::Tilt.mappings.keys << "js"
super
end
@ -46,6 +47,17 @@ module Middleman
def build_dynamic_files
action Directory.new(self, Middleman::Server.views, Middleman::Server.build_dir)
end
@@hooks = {}
def self.after_run(name, &block)
@@hooks[name] = block
end
def run_hooks
@@hooks.each do |name, proc|
instance_eval(&proc)
end
end
end
class Directory < ::Thor::Actions::EmptyDirectory

View file

@ -8,7 +8,7 @@ module Middleman::Features
autoload :MinifyCss, "middleman/features/minify_css"
autoload :MinifyJavascript, "middleman/features/minify_javascript"
autoload :Slickmap, "middleman/features/slickmap"
autoload :SmushPNGs, "middleman/features/smush_pngs"
autoload :SmushPngs, "middleman/features/smush_pngs"
autoload :CodeRay, "middleman/features/code_ray"
autoload :Lorem, "middleman/features/lorem"
# autoload :LiveReload, "middleman/features/live_reload"

View file

@ -1,13 +1,6 @@
module Middleman::Features::SmushPNGs
class << self
def registered(app)
require "middleman/builder"
app.alias_method :pre_smush_after_run, :after_run
app.define_method :after_run do
pre_smush_after_run
smush_dir = File.join(Middleman::Server.build_dir, Middleman::Server.images_dir)
module Middleman::Features::SmushPngs
module ThorActions
def smush_pngs
# Read cache
cache_file = File.join(Middleman::Server.root, ".smush-cache")
cache_data = if File.exists?(cache_file)
@ -16,10 +9,14 @@ module Middleman::Features::SmushPNGs
{}
end
require "smusher"
require "json/pure"
smush_dir = File.join(Middleman::Server.build_dir, Middleman::Server.images_dir)
files = ::Smusher.class_eval do
images_in_folder(smush_dir)
end
files.each do |file|
::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
@ -28,10 +25,23 @@ module Middleman::Features::SmushPNGs
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::Server.build_dir+"/", '')
end
end
end
end
end
say_status :smushed, file.gsub(Middleman::Server.build_dir+"/", "")
end
end
end
class << self
def registered(app)
require "middleman/builder"
require "smusher"
require "json/pure"
Middleman::Builder.send :include, ThorActions
Middleman::Builder.after_run "smush_pngs" do
smush_pngs
end
end
end

View file

@ -1,3 +1,3 @@
module Middleman
VERSION = "1.1.0.beta.1"
VERSION = "1.1.0.beta.2"
end