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.


18
19
20
21
22
# 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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


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

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path


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

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version


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

def version
  @version
end

Instance Method Details

#check!Object

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


81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/image_optim/bin_resolver/bin.rb', line 81

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


66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/image_optim/bin_resolver/bin.rb', line 66

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


24
25
26
27
28
# 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_sObject


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

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