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'],
    [is == '1.7.80', 'loses one color in indexed images'],
  ],
  pngquant: [
    [is < '2.0', 'is not supported'],
  ],
}.freeze
WARN_CHECKS =
{
  advpng: [
    [is == 'none', 'is of unknown version'],
    [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.



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

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.



20
21
22
# File 'lib/image_optim/bin_resolver/bin.rb', line 20

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/image_optim/bin_resolver/bin.rb', line 20

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version.



20
21
22
# File 'lib/image_optim/bin_resolver/bin.rb', line 20

def version
  @version
end

Instance Method Details

#check!Object

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



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/image_optim/bin_resolver/bin.rb', line 85

def check!
  check_fail!

  return unless WARN_CHECKS[name]

  WARN_CHECKS[name].each do |matcher, message|
    next unless matcher.match(version)

    warn "WARN: #{self} (#{matcher}) #{message}"

    break
  end
end

#check_fail!Object

Fail if version will not work properly



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/image_optim/bin_resolver/bin.rb', line 70

def check_fail!
  unless version
    fail UnknownVersion, "could not get version of #{name} at #{path}"
  end

  return unless FAIL_CHECKS[name]

  FAIL_CHECKS[name].each do |matcher, message|
    next unless matcher.match(version)

    fail BadVersion, "#{self} (#{matcher}) #{message}"
  end
end

#digestObject



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

def digest
  return @digest if defined?(@digest)

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

#to_sObject



34
35
36
# File 'lib/image_optim/bin_resolver/bin.rb', line 34

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