class Middleman::Imageoptim::Optimizer
Optimizer
class that accepts an options object and processes files and passes them off to image_optim to be processed
Attributes
app[R]
builder[R]
byte_savings[R]
options[R]
Public Class Methods
new(app, builder, options)
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 15 def initialize(app, builder, options) @app = app @builder = builder @options = options @byte_savings = 0 end
optimize!(app, builder, options)
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 11 def self.optimize!(app, builder, options) new(app, builder, options).process_images end
Public Instance Methods
process_images()
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 22 def process_images images = updated_images modes = preoptimize_modes(images) optimizer.optimize_images(images) do |source, destination| process_image(source, destination, modes.fetch(source.to_s)) end update_manifest say_status 'Total savings: %{data}', data: Utils.format_size(byte_savings) end
Private Instance Methods
build_dir()
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 78 def build_dir if Gem::Version.new(Middleman::VERSION) >= Gem::Version.new('4.0.0') app.config[:build_dir] else app.build_dir end end
build_files()
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 74 def build_files ::Middleman::Util.all_files_under(build_dir) end
builder_thor()
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 90 def builder_thor if Gem::Version.new(Middleman::VERSION) >= Gem::Version.new('4.0.0') builder.thor else builder end end
ensure_file_mode(mode, file)
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 114 def ensure_file_mode(mode, file) return if mode == get_file_mode(file) FileUtils.chmod(mode.to_i(8), file) say_status 'fixed file mode on %{file} file to match source', file: file end
file_updated?(file_path)
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 62 def file_updated?(file_path) return true unless options.manifest File.mtime(file_path) != manifest.resource(file_path) end
get_file_mode(file)
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 110 def get_file_mode(file) sprintf('%o', File.stat(file).mode)[-4, 4].gsub(/^0*/, '') end
manifest()
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 102 def manifest @manifest ||= Manifest.new(app) end
optimizable_images()
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 56 def optimizable_images build_files.select do |path| options.image_extensions.include?(File.extname(path)) && optimizer.optimizable?(path) end end
optimizer()
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 98 def optimizer @optimizer ||= ImageOptim.new(options.imageoptim_options) end
preoptimize_modes(images)
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 67 def preoptimize_modes(images) images.inject({}) do |modes, image| modes[image.to_s] = get_file_mode(image) modes end end
process_image(source, destination = nil, mode = nil)
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 40 def process_image(source, destination = nil, mode = nil) if destination update_bytes_saved(source.size - destination.size) say_status '%{source} (%{percent_change} / %{size_change} %{size_change_type})', Utils.file_size_stats(source, destination) FileUtils.move(destination, source) else say_status '[skipped] %{source} not updated', source: source end ensure ensure_file_mode(mode, source) unless mode.nil? end
say_status(status, interpolations = {})
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 86 def say_status(status, interpolations = {}) builder_thor.say_status(:imageoptim, status % interpolations) if builder end
update_bytes_saved(bytes)
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 106 def update_bytes_saved(bytes) @byte_savings += bytes end
update_manifest()
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 34 def update_manifest return unless options.manifest manifest.build_and_write(optimizable_images) say_status '%{manifest_path} updated', manifest_path: manifest.path end
updated_images()
click to toggle source
# File lib/middleman-imageoptim/optimizer.rb, line 52 def updated_images optimizable_images.select { |path| file_updated?(path) } end