forked from cloudfoundry/libbuildpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
versions.go
100 lines (83 loc) · 2.39 KB
/
versions.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
package libbuildpack
import (
"fmt"
"sort"
semver2 "github.com/Masterminds/semver"
semver1 "github.com/blang/semver"
)
type versionWithOriginal struct {
original string
version semver1.Version
}
type versionsWithOriginal []versionWithOriginal
func (v versionsWithOriginal) Len() int { return len(v) }
func (v versionsWithOriginal) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
func (v versionsWithOriginal) Less(i, j int) bool { return v[i].version.LT(v[j].version) }
func FindMatchingVersion(constraint string, versions []string) (string, error) {
vs, err := FindMatchingVersions(constraint, versions)
if err != nil {
return "", err
}
return vs[len(vs)-1], nil
}
func FindMatchingVersions(constraint string, versions []string) ([]string, error) {
matchedVersions, err := matchSemver1(constraint, versions)
if err == nil {
return matchedVersions, nil
}
return matchSemver2(constraint, versions)
}
func matchSemver1(constraint string, versions []string) ([]string, error) {
var depVersions versionsWithOriginal
versionConstraint, err := semver1.ParseRange(constraint)
if err != nil {
return []string{}, err
}
for _, ver := range versions {
depVersion, err := semver1.Parse(ver)
if err != nil {
return []string{}, err
}
versionWithOriginal := versionWithOriginal{
original: ver,
version: depVersion,
}
if versionConstraint(depVersion) {
depVersions = append(depVersions, versionWithOriginal)
}
}
if len(depVersions) != 0 {
sort.Sort(depVersions)
var vs []string
for _, depV := range depVersions {
vs = append(vs, depV.original)
}
return vs, nil
}
return []string{}, fmt.Errorf("no match found for %s in %v", constraint, versions)
}
func matchSemver2(constraint string, versions []string) ([]string, error) {
var depVersions []*semver2.Version
versionConstraint, err := semver2.NewConstraint(constraint)
if err != nil {
return []string{}, err
}
for _, ver := range versions {
depVersion, err := semver2.NewVersion(ver)
if err != nil {
return []string{}, err
}
if versionConstraint.Check(depVersion) {
depVersions = append(depVersions, depVersion)
}
}
if len(depVersions) != 0 {
sort.Sort(semver2.Collection(depVersions))
var vs []string
for _, depV := range depVersions {
vs = append(vs, depV.Original())
}
return vs, nil
}
return []string{}, fmt.Errorf("no match found for %s in %v", constraint, versions)
}