class ImageOptim::BinResolver::Bin

Holds bin name and path, gets version

Constants

FAIL_CHECKS
WARN_CHECKS

Attributes

name[R]
path[R]
version[R]

Public Class Methods

new(name, path) click to toggle source
# File lib/image_optim/bin_resolver/bin.rb, line 15
def initialize(name, path)
  @name = name.to_sym
  @path = path.to_s
  @version = detect_version
end

Public Instance Methods

check!() click to toggle source

Run check_fail!, otherwise warn if version is known to misbehave

# File lib/image_optim/bin_resolver/bin.rb, line 53
def check!
  check_fail!

  WARN_CHECKS.each do |bin_name, matcher, message|
    next unless bin_name == name
    next unless matcher.match(version)
    warn "WARN: #{self} (#{matcher}) #{message}"
  end
end
check_fail!() click to toggle source

Fail if version will not work properly

# File lib/image_optim/bin_resolver/bin.rb, line 42
def check_fail!
  fail UnknownVersion, "didn't get version of #{self}" unless version

  FAIL_CHECKS.each do |bin_name, matcher, message|
    next unless bin_name == name
    next unless matcher.match(version)
    fail BadVersion, "#{self} (#{matcher}) #{message}"
  end
end
to_s() click to toggle source
# File lib/image_optim/bin_resolver/bin.rb, line 21
def to_s
  "#{name} #{version || '?'} at #{path}"
end

Private Instance Methods

capture(cmd) click to toggle source
# File lib/image_optim/bin_resolver/bin.rb, line 96
def capture(cmd)
  Cmd.capture(cmd)
end
detect_version() click to toggle source

Wrap version_string with SimpleVersion

# File lib/image_optim/bin_resolver/bin.rb, line 66
def detect_version
  str = version_string
  str && SimpleVersion.new(str)
end
escaped_path() click to toggle source
# File lib/image_optim/bin_resolver/bin.rb, line 100
def escaped_path
  path.shellescape
end
version_string() click to toggle source

Getting version of bin, will fail for an unknown name

# File lib/image_optim/bin_resolver/bin.rb, line 72
def version_string
  case name
  when :advpng, :gifsicle, :jpegoptim, :optipng, :pngquant
    capture("#{escaped_path} --version 2> /dev/null")[/\d+(\.\d+){1,}/]
  when :svgo
    capture("#{escaped_path} --version 2>&1")[/\d+(\.\d+){1,}/]
  when :jhead, :'jpeg-recompress'
    capture("#{escaped_path} -V 2> /dev/null")[/\d+(\.\d+){1,}/]
  when :jpegtran
    capture("#{escaped_path} -v - 2>&1")[/version (\d+\S*)/, 1]
  when :pngcrush
    capture("#{escaped_path} -version 2>&1")[/\d+(\.\d+){1,}/]
  when :pngout
    date_regexp = /[A-Z][a-z]{2} (?: |\d)\d \d{4}/
    date_str = capture("#{escaped_path} 2>&1")[date_regexp]
    Date.parse(date_str).strftime('%Y%m%d') if date_str
  when :jpegrescan
    # jpegrescan has no version so just check presence
    path && '-'
  else
    fail "getting `#{name}` version is not defined"
  end
end