Class: ImageOptim::BinResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/image_optim/bin_resolver.rb,
lib/image_optim/bin_resolver/bin.rb,
lib/image_optim/bin_resolver/error.rb,
lib/image_optim/bin_resolver/simple_version.rb,
lib/image_optim/bin_resolver/comparable_condition.rb

Overview

Handles resolving binaries and checking versions

If there is an environment variable XXX_BIN when resolving xxx, then a symlink to binary will be created in a temporary directory which will be added to PATH

Defined Under Namespace

Classes: Bin, BinNotFound, ComparableCondition, Error, SimpleVersion

Constant Summary collapse

VENDOR_PATH =

Path to vendor at root of image_optim

File.expand_path('../../../vendor', __FILE__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_optim) ⇒ BinResolver

Returns a new instance of BinResolver.



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

def initialize(image_optim)
  @image_optim = image_optim
  @bins = {}
  @lock = Mutex.new
  init_pack
end

Instance Attribute Details

#dirObject (readonly)

Directory for symlinks to bins if XXX_BIN was used



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

def dir
  @dir
end

#pack_pathObject (readonly)

Path to pack from image_optim_pack if used



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

def pack_path
  @pack_path
end

Class Method Details

.collect_errors(enumerable) ⇒ Object

Collect resolving errors when running block over items of enumerable



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

def self.collect_errors(enumerable)
  errors = []
  enumerable.each do |item|
    begin
      yield item
    rescue Error => e
      errors << e
    end
  end
  errors
end

Instance Method Details

#env_pathObject

Prepand ‘dir` and append `VENDOR_PATH` to `PATH` from environment



60
61
62
63
64
65
66
67
# File 'lib/image_optim/bin_resolver.rb', line 60

def env_path
  [
    dir,
    pack_path,
    ENV.fetch('PATH', nil),
    VENDOR_PATH,
  ].compact.join(File::PATH_SEPARATOR)
end

#resolve!(name) ⇒ Object

Binary resolving: create symlink if there is XXX_BIN environment variable, build Bin with full path, check binary version Return Bin instance



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/image_optim/bin_resolver.rb', line 33

def resolve!(name)
  name = name.to_sym

  resolving(name) do
    path = symlink_custom_bin!(name) || full_path(name)
    bin = Bin.new(name, path) if path

    if bin && @image_optim.verbose
      $stderr << "Resolved #{bin}\n"
    end

    @bins[name] = bin

    bin.check! if bin
  end

  fail BinNotFound, "`#{name}` not found" unless @bins[name]

  @bins[name].check_fail!

  @bins[name]
end