Sha256: 3767638df4f2b3a6c004850998dc3e0eb78644eb233f7a1483dc25993b30fadf

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module Overcommit::Hook::PreCommit
  # Checks for images that can be optimized with `image_optim`.
  class ImageOptim < Base
    def run
      begin
        require 'image_optim'
      rescue LoadError
        return :warn, 'image_optim not installed -- run `gem install image_optim`'
      end

      optimized_images =
        begin
          optimize_images(applicable_files)
        rescue ::ImageOptim::BinNotFoundError => e
          return :fail, "#{e.message}. The image_optim gem is dependendent on this binary."
        end

      if optimized_images.any?
        return :fail,
          "The following images are optimizable:\n#{optimized_images.join("\n")}" \
          "\n\nOptimize them by running:\n" \
          "  image_optim #{optimized_images.join(' ')}"
      end

      :pass
    end

  private

    def optimize_images(image_paths)
      image_optim = ::ImageOptim.new(:pngout => false)

      optimized_images =
        image_optim.optimize_images!(image_paths) do |path, optimized|
          path if optimized
        end

      optimized_images.compact
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
overcommit-0.17.0 lib/overcommit/hook/pre_commit/image_optim.rb
overcommit-0.16.0 lib/overcommit/hook/pre_commit/image_optim.rb
overcommit-0.15.0 lib/overcommit/hook/pre_commit/image_optim.rb