Class: ImageOptim::BinResolver::Bin

Inherits:
Object
  • Object
show all
Defined in:
lib/image_optim/bin_resolver/bin.rb

Overview

Holds bin name and path, gets version

Defined Under Namespace

Classes: BadVersion, UnknownVersion

Constant Summary collapse

FAIL_CHECKS =
[
  [:pngcrush, is.between?('1.7.60', '1.7.65'), 'is known to produce '\
        'broken pngs'],
  [:pngcrush, is == '1.7.80', 'loses one color in indexed images'],
  [:pngquant, is < '2.0', 'is not supported'],
].freeze
WARN_CHECKS =
[
  [:advpng, is < '1.17', 'does not use zopfli'],
  [:gifsicle, is < '1.85', 'does not support removing extension blocks'],
  [:pngcrush, is < '1.7.38', 'does not have blacken flag'],
  [:pngquant, is < '2.1', 'may be lossy even with quality `100-`'],
  [:optipng, is < '0.7', 'does not support -strip option'],
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path) ⇒ Bin

Returns a new instance of Bin.



17
18
19
20
21
# File 'lib/image_optim/bin_resolver/bin.rb', line 17

def initialize(name, path)
  @name = name.to_sym
  @path = path.to_s
  @version = detect_version
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/image_optim/bin_resolver/bin.rb', line 16

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/image_optim/bin_resolver/bin.rb', line 16

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version.



16
17
18
# File 'lib/image_optim/bin_resolver/bin.rb', line 16

def version
  @version
end

Instance Method Details

#check!Object

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



63
64
65
66
67
68
69
70
71
# File 'lib/image_optim/bin_resolver/bin.rb', line 63

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!Object

Fail if version will not work properly



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/image_optim/bin_resolver/bin.rb', line 50

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

#digestObject



23
24
25
26
# File 'lib/image_optim/bin_resolver/bin.rb', line 23

def digest
  return @digest if defined?(@digest)
  @digest = File.exist?(@path) && Digest::SHA1.file(@path).hexdigest
end

#to_sObject



28
29
30
# File 'lib/image_optim/bin_resolver/bin.rb', line 28

def to_s
  "#{name} #{version || '?'} at #{path}"
end