File: cliver.rb

package info (click to toggle)
ruby-cliver 0.3.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208 kB
  • sloc: ruby: 760; makefile: 3
file content (85 lines) | stat: -rw-r--r-- 3,228 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# encoding: utf-8

require "core_ext/file"

require 'cliver/version'
require 'cliver/dependency'
require 'cliver/shell_capture'
require 'cliver/detector'
require 'cliver/filter'

# Cliver is tool for making dependency assertions against
# command-line executables.
module Cliver

  # The primary interface for the Cliver gem allows detection of an executable
  # on your path that matches a version requirement, or raise an appropriate
  # exception to make resolution simple and straight-forward.
  # @see Cliver::Dependency
  # @overload (see Cliver::Dependency#initialize)
  # @param (see Cliver::Dependency#initialize)
  # @raise (see Cliver::Dependency#detect!)
  # @return (see Cliver::Dependency#detect!)
  def self.detect!(*args, &block)
    Dependency::new(*args, &block).detect!
  end

  # A non-raising variant of {::detect!}, simply returns false if dependency
  # cannot be found.
  # @see Cliver::Dependency
  # @overload (see Cliver::Dependency#initialize)
  # @param (see Cliver::Dependency#initialize)
  # @raise (see Cliver::Dependency#detect)
  # @return (see Cliver::Dependency#detect)
  def self.detect(*args, &block)
    Dependency::new(*args, &block).detect
  end

  # A legacy interface for {::detect} with the option `strict: true`, ensures
  # that the first executable on your path matches the requirements.
  # @see Cliver::Dependency
  # @overload (see Cliver::Dependency#initialize)
  # @param (see Cliver::Dependency#initialize)
  # @option options [Boolean] :strict (true) @see Cliver::Dependency::initialize
  # @raise (see Cliver::Dependency#detect!)
  # @return (see Cliver::Dependency#detect!)
  def self.assert(*args, &block)
    options = args.last.kind_of?(Hash) ? args.pop : {}
    args << options.merge(:strict => true)
    Dependency::new(*args, &block).detect!
  end

  # Verify an absolute-path to an executable.
  # @overload verify!(executable, *requirements, options = {})
  #   @param executable [String] absolute path to an executable
  #   @param requirements (see Cliver::Dependency#initialize)
  #   @option options (see Cliver::Dependency::initialize)
  #   @raise (see Cliver::Dependency#detect!)
  #   @return (see Cliver::Dependency#detect!)
  def self.verify!(executable, *args, &block)
    unless File.absolute_path?(executable)
      raise ArgumentError, "executable path must be absolute, " +
                           "got '#{executable.inspect}'."
    end
    options = args.last.kind_of?(Hash) ? args.pop : {}
    args << options.merge(:path => '.') # ensure path non-empty.
    Dependency::new(executable, *args, &block).detect!
  end

  extend self

  # Wraps Cliver::assert and returns truthy/false instead of raising
  # @see Cliver::assert
  # @overload (see Cliver::Assertion#initialize)
  # @param (see Cliver::Assertion#initialize)
  # @return [False,String] either returns false or the reason why the
  #                        assertion was unmet.
  def dependency_unmet?(*args, &block)
    Cliver.assert(*args, &block)
    false
  rescue Dependency::NotMet => error
    # Cliver::Assertion::VersionMismatch -> 'Version Mismatch'
    reason = error.class.name.split(':').last.gsub(/([a-z])([A-Z])/, '\\1 \\2')
    "#{reason}: #{error.message}"
  end
end