Reimplement smusher for thor
This commit is contained in:
parent
d643a8e041
commit
c6b0723156
5 changed files with 56 additions and 38 deletions
|
@ -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
|
||||
Middleman::Builder.start
|
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -1,37 +1,47 @@
|
|||
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)
|
||||
Marshal.restore(File.read(cache_file))
|
||||
else
|
||||
{}
|
||||
end
|
||||
|
||||
smush_dir = File.join(Middleman::Server.build_dir, Middleman::Server.images_dir)
|
||||
|
||||
# Read cache
|
||||
cache_file = File.join(Middleman::Server.root, ".smush-cache")
|
||||
cache_data = if File.exists?(cache_file)
|
||||
Marshal.restore(File.read(cache_file))
|
||||
else
|
||||
{}
|
||||
end
|
||||
files = ::Smusher.class_eval do
|
||||
images_in_folder(smush_dir)
|
||||
end
|
||||
|
||||
require "smusher"
|
||||
require "json/pure"
|
||||
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
|
||||
|
||||
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::Server.build_dir+"/", '')
|
||||
end
|
||||
original_file_size = size(file)
|
||||
return if original_file_size.zero?
|
||||
return if cache_data[file] && cache_data[file] == original_file_size
|
||||
|
||||
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
|
||||
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
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
module Middleman
|
||||
VERSION = "1.1.0.beta.1"
|
||||
VERSION = "1.1.0.beta.2"
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue