Class: Composer::Package::LinkConstraint::VersionConstraint

Inherits:
SpecificConstraint show all
Defined in:
lib/composer/package/link_constraint/version_constraint.rb

Constant Summary collapse

@@cache =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SpecificConstraint

#matches, #pretty_string, #pretty_string=

Methods inherited from BaseConstraint

#matches, #pretty_string, #pretty_string=

Constructor Details

#initialize(operator, version) ⇒ VersionConstraint

Sets operator and version to compare a package with

Parameters:

  • string

    $operator A comparison operator

  • string

    $version A version to compare to



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/composer/package/link_constraint/version_constraint.rb', line 23

def initialize(operator, version)
    if operator === '='
      operator = '=='
    end

    if operator === '<>'
      operator = '!='
    end

    @operator = operator
    @version = version
end

Instance Attribute Details

#operatorObject (readonly)

Returns the value of attribute operator.



16
17
18
# File 'lib/composer/package/link_constraint/version_constraint.rb', line 16

def operator
  @operator
end

#versionObject (readonly)

Returns the value of attribute version.



16
17
18
# File 'lib/composer/package/link_constraint/version_constraint.rb', line 16

def version
  @version
end

Instance Method Details

#match_specific(provider, compare_branches = false) ⇒ Object

Returns bool.

Parameters:

  • VersionConstraint

    provider

  • bool

    compare_branches

Returns:

  • bool



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/composer/package/link_constraint/version_constraint.rb', line 160

def match_specific(provider, compare_branches = false)
  @@cache = {} unless @@cache
  @@cache[@operator] = {} unless @@cache.key?(@operator)
  @@cache[@operator][@version] = {} unless @@cache[@operator].key?(@version)
  @@cache[@operator][@version][provider.operator] = {} unless @@cache[@operator][@version].key?(provider.operator)
  @@cache[@operator][@version][provider.operator][provider.version] = {} unless @@cache[@operator][@version][provider.operator].key?(provider.version)

  if @@cache[@operator][@version][provider.operator][provider.version].key?(compare_branches)
    return @@cache[@operator][@version][provider.operator][provider.version][compare_branches]
  end

  (@@cache[@operator][@version][provider.operator][provider.version][compare_branches] = do_match_specific(provider, compare_branches))
end

#to_sObject



174
175
176
# File 'lib/composer/package/link_constraint/version_constraint.rb', line 174

def to_s
  "#{@operator} #{@version}"
end

#version_compare(a, b, operator, compare_branches = false) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/composer/package/link_constraint/version_constraint.rb', line 36

def version_compare(a, b, operator, compare_branches = false)

  a_is_branch = 'dev-' === a[0...4]
  b_is_branch = 'dev-' === b[0...4]
  if a_is_branch && b_is_branch
    return operator == '==' && a === b
  end

  # when branches are not comparable, we make sure dev branches never match anything
  if !compare_branches && (a_is_branch || b_is_branch)
    return false
  end

  # Standardise versions
  ver_a = a.strip.gsub(/([\-?\_?\+?])/, '.').gsub(/([^0-9\.]+)/, '.$1.').gsub(/\.\./, '.').split('.')
  ver_b = b.strip.gsub(/([\-?\_?\+?])/, '.').gsub(/([^0-9\.]+)/, '.$1.').gsub(/\.\./, '.').split('.')

  # Replace empty entries at the start of the array
  while ver_a[0] && ver_a[0].empty?
    ver_a.shift
  end
  while ver_b[0] && ver_b[0].empty?
    ver_b.shift
  end

  # Release state order
  # '#' stands for any number
  versions = {
    'dev'   => 0,
    'alpha' => 1,
    'a'     => 1,
    'beta'  => 2,
    'b'     => 2,
    'RC'    => 3,
    '#'     => 4,
    'p'     => 5,
    'pl'    => 5
  }

  # Loop through each segment in the version string
  compare = 0
  for i in 0..([ver_a.length, ver_b.length].min - 1)
  # for (i = 0, $x = [ver_a.length, ver_b.length].min; i < $x; i++) {

    next if ver_a[i] == ver_b[i]

    i1 = ver_a[i]
    i2 = ver_b[i]

    i1_is_numeric = true if Float(i1) rescue false
    i2_is_numeric = true if Float(i2) rescue false

    if i1_is_numeric && i2_is_numeric
      compare = (i1 < i2) ? -1 : 1
      break
    end

    # We use the position of '#' in the versions list
    # for numbers... (so take care of # in original string)
    if i1 == '#'
        i1 = ''
    elsif i1_is_numeric
      i1 = '#';
    end

    if i2 == '#'
      i2 = ''
    elsif i2_is_numeric
      i2 = '#'
    end

    if !versions[i1].nil? && versions[i2].nil?
      compare = versions[i1] < versions[i2] ? -1 : 1
    elsif !versions[i1].nil?
      compare = 1;
    elsif !versions[i2].nil?
      compare = -1;
    else
      compare = 0;
    end

    break;

  end

  # If previous loop didn't find anything, compare the "extra" segments
  if compare == 0
    if ver_b.length > ver_a.length
      if !ver_b[i].nil && !versions[ver_b[i]].nil
        compare = (versions[ver_b[i]] < 4) ? 1 : -1;
      else
        compare = -1;
      end
    elsif ver_b.length < ver_a.length
      if !ver_a[i].nil && !versions[ver_a[i]].nil
        compare = (versions[ver_a[i]] < 4) ? -1 : 1;
      else
        compare = 1;
      end
    end
  end

  # Compare the versions
  case operator
  when '>', 'gt'
    return compare > 0
  when '>=', 'ge'
    return compare >= 0
  when '<=', 'le'
    return compare <= 0
  when '==', '=', 'eq'
    return compare == 0
  when '<>', '!=', 'ne'
    return compare != 0
  when '', '<', 'lt'
    return compare < 0
  end

  false
end