Sha256: 09e54b2854bb727487db1e267597c610df1200c7343d322092660ceef5700d0b

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

# encoding: utf-8
require 'open3'

module Cliver
  # Default implementation of Cliver::Detector
  # Requires a command argument (default '--version')
  # and a pattern-matcher Regexp with sensible default.
  class Detector::Default < Struct.new(:command_arg, :version_pattern)
    include Detector

    # Default pattern to use when searching {#version_command} output
    DEFAULT_VERSION_PATTERN = /(?<=version )[0-9][.0-9a-z]+/i.freeze

    # Default command argument to use against the executable to get
    # version output
    DEFAULT_COMMAND_ARG = '--version'.freeze

    # Forgiving input, allows either argument if only one supplied.
    #
    # @overload initialize(command_arg)
    # @overload initialize(version_pattern)
    # @overload initialize(command_arg, version_pattern)
    # @param command_arg [String] ('--version')
    # @param version_pattern [Regexp] (/(?<=version )[0-9][.0-9a-z]+/i)
    def initialize(*args)
      command_arg = args.shift if args.first.kind_of?(String)
      version_pattern = args.shift
      super(command_arg, version_pattern)
    end

    # The pattern to match the version in {#version_command}'s output.
    # Defaults to {DEFAULT_VERSION_PATTERN}
    # @overload (see Cliver::Detector#version_pattern)
    # @return (see Cliver::Detector#version_pattern)
    def version_pattern
      super || DEFAULT_VERSION_PATTERN
    end

    # The argument to pass to the executable to get current version
    # Defaults to {DEFAULT_COMMAND_ARG}
    # @return [String]
    def command_arg
      super || DEFAULT_COMMAND_ARG
    end

    # @overload (see Cliver::Detector#version_command)
    # @return (see Cliver::Detector#version_command)
    def version_command(executable_path)
      [executable_path, command_arg]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cliver-0.1.3 lib/cliver/detector/default.rb