1
2
3
4
5 package gover
6
7 import (
8 "sort"
9 "strings"
10
11 "golang.org/x/mod/module"
12 "golang.org/x/mod/semver"
13 )
14
15
16
17
18
19
20
21
22
23
24
25 func IsToolchain(path string) bool {
26 return path == "go" || path == "toolchain"
27 }
28
29
30
31
32
33
34 func ModCompare(path string, x, y string) int {
35 if path == "go" {
36 return Compare(x, y)
37 }
38 if path == "toolchain" {
39 return Compare(maybeToolchainVersion(x), maybeToolchainVersion(y))
40 }
41 return semver.Compare(x, y)
42 }
43
44
45
46 func ModSort(list []module.Version) {
47 sort.Slice(list, func(i, j int) bool {
48 mi := list[i]
49 mj := list[j]
50 if mi.Path != mj.Path {
51 return mi.Path < mj.Path
52 }
53
54
55
56 vi := mi.Version
57 vj := mj.Version
58 var fi, fj string
59 if k := strings.Index(vi, "/"); k >= 0 {
60 vi, fi = vi[:k], vi[k:]
61 }
62 if k := strings.Index(vj, "/"); k >= 0 {
63 vj, fj = vj[:k], vj[k:]
64 }
65 if vi != vj {
66 return ModCompare(mi.Path, vi, vj) < 0
67 }
68 return fi < fj
69 })
70 }
71
72
73 func ModIsValid(path, vers string) bool {
74 if IsToolchain(path) {
75 if path == "toolchain" {
76 return IsValid(FromToolchain(vers))
77 }
78 return IsValid(vers)
79 }
80 return semver.IsValid(vers)
81 }
82
83
84
85 func ModIsPrefix(path, vers string) bool {
86 if IsToolchain(path) {
87 if path == "toolchain" {
88 return IsLang(FromToolchain(vers))
89 }
90 return IsLang(vers)
91 }
92
93 dots := 0
94 for i := 0; i < len(vers); i++ {
95 switch vers[i] {
96 case '-', '+':
97 return false
98 case '.':
99 dots++
100 if dots >= 2 {
101 return false
102 }
103 }
104 }
105 return true
106 }
107
108
109
110 func ModIsPrerelease(path, vers string) bool {
111 if IsToolchain(path) {
112 return IsPrerelease(vers)
113 }
114 return semver.Prerelease(vers) != ""
115 }
116
117
118
119 func ModMajorMinor(path, vers string) string {
120 if IsToolchain(path) {
121 if path == "toolchain" {
122 return "go" + Lang(FromToolchain(vers))
123 }
124 return Lang(vers)
125 }
126 return semver.MajorMinor(vers)
127 }
128
View as plain text