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 18 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 64 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 51 def check_fail! unless version fail UnknownVersion, "could not get version of #{name} at #{path}" end 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
digest()
click to toggle source
# File lib/image_optim/bin_resolver/bin.rb, line 24 def digest return @digest if defined?(@digest) @digest = File.exist?(@path) && Digest::SHA1.file(@path).hexdigest end
to_s()
click to toggle source
# File lib/image_optim/bin_resolver/bin.rb, line 29 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 107 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 77 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 111 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 83 def version_string case name when :advpng, :gifsicle, :jpegoptim, :optipng capture("#{escaped_path} --version 2> #{Path::NULL}")[/\d+(\.\d+)+/] when :svgo, :pngquant capture("#{escaped_path} --version 2>&1")[/\d+(\.\d+)+/] when :jhead, :'jpeg-recompress' capture("#{escaped_path} -V 2> #{Path::NULL}")[/\d+(\.\d+)+/] when :jpegtran capture("#{escaped_path} -v - 2>&1")[/version (\d+\S*)/, 1] when :pngcrush capture("#{escaped_path} -version 2>&1")[/pngcrush (\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 use first 8 characters of sha1 hex Digest::SHA1.file(path).hexdigest[0, 8] if path else fail "getting `#{name}` version is not defined" end end