This repository has been archived by the owner on Mar 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
group.go
300 lines (251 loc) · 7.96 KB
/
group.go
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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package version
import (
"regexp"
"strconv"
"strings"
"sync"
)
type ConstraintGroup struct {
constraints []*Constraint
}
// NewConstrainGroup returns a new NewConstrainGroup
func NewConstrainGroup() *ConstraintGroup {
group := new(ConstraintGroup)
return group
}
// NewConstrainGroupFromString returns a new NewConstrainGroup and create the constraints based on a string
//
// Version constraints can be specified in a few different ways:
//
// Exact version: You can specify the exact version of a package, for
// example 1.0.2.
//
// Range: By using comparison operators you can specify ranges of valid versions.
// Valid operators are >, >=, <, <=, !=. An example range would be >=1.0. You can
// define multiple ranges, separated by a comma: >=1.0,<2.0.
//
// Wildcard: You can specify a pattern with a * wildcard. 1.0.* is the equivalent
// of >=1.0,<1.1.
//
// Next Significant Release (Tilde Operator): The ~ operator is best explained by
// example: ~1.2 is equivalent to >=1.2,<2.0, while ~1.2.3 is equivalent to
// >=1.2.3,<1.3. As you can see it is mostly useful for projects respecting
// semantic versioning. A common usage would be to mark the minimum minor
// version you depend on, like ~1.2 (which allows anything up to, but not
// including, 2.0). Since in theory there should be no backwards compatibility
// breaks until 2.0, that works well. Another way of looking at it is that
// using ~ specifies a minimum version, but allows the last digit specified
// to go up.
//
// By default only stable releases are taken into consideration. If you would like
// to also get RC, beta, alpha or dev versions of your dependencies you can do so
// using stability flags. To change that for all packages instead of doing per
// dependency you can also use the minimum-stability setting.
//
// From: http://getcomposer.org/doc/01-basic-usage.md#package-versions
func NewConstrainGroupFromString(name string) *ConstraintGroup {
group := new(ConstraintGroup)
group.fromString(name)
return group
}
// AddConstraint adds a Contraint to the group
func (self *ConstraintGroup) AddConstraint(constraint ...*Constraint) {
if self.constraints == nil {
self.constraints = make([]*Constraint, 0)
}
self.constraints = append(self.constraints, constraint...)
}
// GetConstraints returns all the constraints
func (self *ConstraintGroup) GetConstraints() []*Constraint {
return self.constraints
}
// Match a given version againts the group
//
// Usage
// c := version.NewConstrainGroupFromString(">2.0,<=3.0")
// c.Match("2.5.0beta")
// Returns: true
//
// c := version.NewConstrainGroupFromString("~1.2.3")
// c.Match("1.2.3.5")
// Returns: true
func (self *ConstraintGroup) Match(version string) bool {
for _, constraint := range self.constraints {
if constraint.Match(version) == false {
return false
}
}
return true
}
func (self *ConstraintGroup) fromString(constraint string) bool {
result := RegFind(`(?i)^([^,\s]*?)@(stable|RC|beta|alpha|dev)$`, constraint)
if result != nil {
constraint = result[1]
if constraint == "" {
constraint = "*"
}
}
result = RegFind(`(?i)^(dev-[^,\s@]+?|[^,\s@]+?\.x-dev)#.+$`, constraint)
if result != nil {
if result[1] != "" {
constraint = result[1]
}
}
constraints := RegSplit(`\s*,\s*`, strings.Trim(constraint, " "))
if len(constraints) > 1 {
for _, part := range constraints {
self.AddConstraint(self.parseConstraint(part)...)
}
return true
}
self.AddConstraint(self.parseConstraint(constraints[0])...)
return true
}
func (self *ConstraintGroup) parseConstraint(constraint string) []*Constraint {
stabilityModifier := ""
result := RegFind(`(?i)^([^,\s]+?)@(stable|RC|beta|alpha|dev)$`, constraint)
if result != nil {
constraint = result[1]
if result[2] != "stable" {
stabilityModifier = result[2]
}
}
result = RegFind(`^[x*](\.[x*])*$`, constraint)
if result != nil {
return make([]*Constraint, 0)
}
highVersion := ""
lowVersion := ""
result = RegFind(`(?i)^~(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?`+modifierRegex+`?$`, constraint)
if result != nil {
if len(result) > 4 && result[4] != "" {
last, _ := strconv.Atoi(result[3])
highVersion = result[1] + "." + result[2] + "." + strconv.Itoa(last+1) + ".0-dev"
lowVersion = result[1] + "." + result[2] + "." + result[3] + "." + result[4]
} else if len(result) > 3 && result[3] != "" {
last, _ := strconv.Atoi(result[2])
highVersion = result[1] + "." + strconv.Itoa(last+1) + ".0.0-dev"
lowVersion = result[1] + "." + result[2] + "." + result[3] + ".0"
} else {
last, _ := strconv.Atoi(result[1])
highVersion = strconv.Itoa(last+1) + ".0.0.0-dev"
if len(result) > 2 && result[2] != "" {
lowVersion = result[1] + "." + result[2] + ".0.0"
} else {
lowVersion = result[1] + ".0.0.0"
}
}
if len(result) > 5 && result[5] != "" {
lowVersion = lowVersion + "-" + expandStability(result[5])
}
if len(result) > 6 && result[6] != "" {
lowVersion = lowVersion + result[6]
}
if len(result) > 7 && result[7] != "" {
lowVersion = lowVersion + "-dev"
}
return []*Constraint{
{">=", lowVersion},
{"<", highVersion},
}
}
result = RegFind(`^(\d+)(?:\.(\d+))?(?:\.(\d+))?\.[x*]$`, constraint)
if result != nil {
if len(result) > 3 && result[3] != "" {
highVersion = result[1] + "." + result[2] + "." + result[3] + ".9999999"
if result[3] == "0" {
last, _ := strconv.Atoi(result[2])
lowVersion = result[1] + "." + strconv.Itoa(last-1) + ".9999999.9999999"
} else {
last, _ := strconv.Atoi(result[3])
lowVersion = result[1] + "." + result[2] + "." + strconv.Itoa(last-1) + ".9999999"
}
} else if len(result) > 2 && result[2] != "" {
highVersion = result[1] + "." + result[2] + ".9999999.9999999"
if result[2] == "0" {
last, _ := strconv.Atoi(result[1])
lowVersion = strconv.Itoa(last-1) + ".9999999.9999999.9999999"
} else {
last, _ := strconv.Atoi(result[2])
lowVersion = result[1] + "." + strconv.Itoa(last-1) + ".9999999.9999999"
}
} else {
highVersion = result[1] + ".9999999.9999999.9999999"
if result[1] == "0" {
return []*Constraint{{"<", highVersion}}
} else {
last, _ := strconv.Atoi(result[1])
lowVersion = strconv.Itoa(last-1) + ".9999999.9999999.9999999"
}
}
return []*Constraint{
{">", lowVersion},
{"<", highVersion},
}
}
// match operators constraints
result = RegFind(`^(<>|!=|>=?|<=?|==?)?\s*(.*)`, constraint)
if result != nil {
version := Normalize(result[2])
if stabilityModifier != "" && parseStability(version) == "stable" {
version = version + "-" + stabilityModifier
} else if result[1] == "<" {
match := RegFind(`(?i)-stable$`, result[2])
if match == nil {
version = version + "-dev"
}
}
if len(result) > 1 && result[1] != "" {
return []*Constraint{{result[1], version}}
} else {
return []*Constraint{{"=", version}}
}
}
return []*Constraint{{constraint, stabilityModifier}}
}
// PCRegMap : PreCompiled Regex Map
type PCRegMap struct {
sync.RWMutex
m map[string]*regexp.Regexp
}
// MustCompile : to replace regexp.MustCompile in RegFind.
func (p *PCRegMap) MustCompile(pattern string) *regexp.Regexp {
p.RLock()
ret, exist := p.m[pattern]
p.RUnlock()
if exist {
return ret
}
ret = regexp.MustCompile(pattern)
p.Lock()
p.m[pattern] = ret
p.Unlock()
return ret
}
var (
regexpCache *PCRegMap
)
func init() {
regexpCache = new(PCRegMap)
regexpCache.m = make(map[string]*regexp.Regexp)
}
func RegFind(pattern, subject string) []string {
reg := regexpCache.MustCompile(pattern)
matched := reg.FindAllStringSubmatch(subject, -1)
if matched != nil {
return matched[0]
}
return nil
}
func RegSplit(pattern, subject string) []string {
reg := regexp.MustCompile(pattern)
indexes := reg.FindAllStringIndex(subject, -1)
laststart := 0
result := make([]string, len(indexes)+1)
for i, element := range indexes {
result[i] = subject[laststart:element[0]]
laststart = element[1]
}
result[len(indexes)] = subject[laststart:len(subject)]
return result
}