Class: Cliver::Detector

Inherits:
Struct
  • Object
show all
Defined in:
lib/cliver/detector.rb

Overview

Default implementation of the detector needed by Cliver::Assertion, which will take anything that #respond_to?(:to_proc)

Constant Summary collapse

DEFAULT_VERSION_PATTERN =

Default pattern to use when searching #version_command output

/(version ?)?[0-9][.0-9a-z]+/i.freeze
DEFAULT_COMMAND_ARG =

Default command argument to use against the executable to get version output

'--version'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*command_args) ⇒ Detector #initialize(version_pattern) ⇒ Detector #initialize(*command_args, version_pattern) ⇒ Detector

Forgiving input, allows either argument if only one supplied.

Overloads:

  • #initialize(*command_args) ⇒ Detector

    Parameters:

    • command_args (Array<String>)
  • #initialize(version_pattern) ⇒ Detector

    Parameters:

    • version_pattern (Regexp)
  • #initialize(*command_args, version_pattern) ⇒ Detector

    Parameters:

    • command_args (Array<String>)
    • version_pattern (Regexp)


32
33
34
35
36
37
# File 'lib/cliver/detector.rb', line 32

def initialize(*args)
  version_pattern = args.pop if args.last.kind_of?(Regexp)
  command_args = args unless args.empty?

  super(command_args, version_pattern)
end

Instance Attribute Details

#command_argString+

The argument to pass to the executable to get current version Defaults to DEFAULT_COMMAND_ARG

Returns:

  • (String, Array<String>)


7
8
9
# File 'lib/cliver/detector.rb', line 7

def command_arg
  @command_arg
end

#version_patternRegexp

The pattern to match the version in #version_command‘s output. Defaults to DEFAULT_VERSION_PATTERN

Returns:

  • (Regexp)
    • the pattern used against the output

    of the #version_command, which should contain a Gem::Version-parsable substring.



7
8
9
# File 'lib/cliver/detector.rb', line 7

def version_pattern
  @version_pattern
end

Class Method Details

.generate(detector_argument) ⇒ Object

Parameters:

  • detector_argument (#call, Object)

    If detector_argument responds to #call, return it; otherwise attempt to create an instance of self.



11
12
13
14
# File 'lib/cliver/detector.rb', line 11

def self.generate(detector_argument)
  return detector_argument if detector_argument.respond_to?(:call)
  new(*Array(detector_argument))
end

Instance Method Details

#detect_version(executable_path) ⇒ String

Returns - should be contain Gem::Version-parsable version number.

Parameters:

  • executable_path (String)
    • the path to the executable to test

Returns:

  • (String)
    • should be contain Gem::Version-parsable

    version number.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cliver/detector.rb', line 42

def detect_version(executable_path)
  output = shell_out_and_capture version_command(executable_path).shelljoin
  if $?.exitstatus == 127
    raise Cliver::Dependency::NotFound.new(
        "Could not find an executable at given path '#{executable_path}'." +
        "If this path was not specified explicitly, it is probably a " +
        "bug in [Cliver](https://github.com/yaauie/cliver/issues)."
      )
  end
  output[version_pattern]
end

#to_procProc

This is the interface that any detector must have. If not overridden, returns a proc that wraps #detect_version

Returns:

See Also:



58
59
60
# File 'lib/cliver/detector.rb', line 58

def to_proc
  method(:detect_version).to_proc
end

#version_command(executable_path) ⇒ Array<String>

Parameters:

  • executable_path (String)

    the executable to test

Returns:

  • (Array<String>)


80
81
82
# File 'lib/cliver/detector.rb', line 80

def version_command(executable_path)
  [executable_path, *Array(command_arg)]
end