Class: Cliver::Assertion

Inherits:
Object
  • Object
show all
Includes:
Which
Defined in:
lib/cliver/assertion.rb

Overview

The core of Cliver, Assertion is responsible for detecting the installed version of a binary and determining if it meets the requirements

Constant Summary collapse

DependencyNotMet =

An exception class raised when assertion is not met

Class.new(ArgumentError)
DependencyVersionMismatch =

An exception that is raised when executable present is the wrong version

Class.new(DependencyNotMet)
DependencyNotFound =

An exception that is raised when executable is not present

Class.new(DependencyNotMet)
PARSABLE_GEM_VERSION =

A pattern for extracting a Gem::Version-parsable version

/[0-9]+(.[0-9]+){0,4}(.[a-zA-Z0-9]+)?/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(executable, *requirements, options = {}) ⇒ Assertion

Returns a new instance of Assertion.

Parameters:

  • executable (String)
  • requirements (Array<String>, String)

    splat of strings whose elements follow the pattern

    [<operator>] <version>
    

    Where <operator> is optional (default ‘=”) and in the set

    '=', '!=', '>', '<', '>=', '<=', or '~>'
    

    And <version> is dot-separated integers with optional alphanumeric pre-release suffix. See also Specifying Versions

  • options (Hash<Symbol,Object>)

Yield Parameters:

  • full (String)

    path to executable

Yield Returns:

  • (String)

    containing a Gem::Version-parsable substring



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cliver/assertion.rb', line 39

def initialize(executable, *args, &detector)
  options = args.last.kind_of?(Hash) ? args.pop : {}
  @detector = detector || options.fetch(:detector) { Detector.new }
  @filter = options.fetch(:filter, Filter::IDENTITY).extend(Filter)

  @executable = executable.dup.freeze

  unless args.empty?
    @requirement = Gem::Requirement.new(@filter.requirements(args))
  end
end

Instance Method Details

#assert!Object

Raises:



53
54
55
56
57
58
59
60
61
# File 'lib/cliver/assertion.rb', line 53

def assert!
  version = installed_version
  raise(DependencyNotFound, "'#{@executable}' missing.") unless version

  if @requirement && !@requirement.satisfied_by?(Gem::Version.new(version))
    raise DependencyVersionMismatch,
          "expected '#{@executable}' to be #{@requirement}, got #{version}"
  end
end

#installed_versionnil, ...

Finds the executable on your path using Which; if the executable is present and version requirements are specified, uses the specified detector to get the current version.

Returns:

  • (nil)

    if no version present

  • (String)

    Gem::Version-parsable string version

  • (true)

    if present and no requirements (optimization)



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cliver/assertion.rb', line 70

def installed_version
  executable_path = which(@executable)
  return nil unless executable_path
  return true unless @requirement

  version_string = @detector.to_proc.call(executable_path)
  version_string &&= @filter.to_proc.call(version_string)
  (version_string && version_string[PARSABLE_GEM_VERSION]).tap do |version|
    unless version
      raise ArgumentError,
            "found #{@executable} at '#{executable_path}' " +
            'but could not detect its version.'
    end
  end
end