-
Notifications
You must be signed in to change notification settings - Fork 8
/
vers.go
317 lines (273 loc) · 6.14 KB
/
vers.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"regexp"
"sort"
"strconv"
"strings"
)
var reSemVer = regexp.MustCompile(`^\d+(?:\.\d+){1,2}`)
func IsSemVer(ver string) bool {
return reSemVer.MatchString(ver)
}
var reVersionNum = regexp.MustCompile(`^\d+$`)
func toInt(s string) int {
if reVersionNum.MatchString(s) {
n, err := strconv.Atoi(s)
if err != nil {
fatalf("failed to strconv.Atoi(%#v): %v", s, err)
}
return n
}
return 0
}
type SemVer struct {
Major int
Minor int
Patch int
Tag string
}
var reVersionTag = regexp.MustCompile(`^(\d+)[.+-]?(.*)$`)
func NewSemVer(ver string) *SemVer {
segments := strings.SplitN(ver, ".", 3)
for i, s := range segments {
if !reVersionNum.MatchString(s) {
segments[i] = strings.Join(segments[i:], ".")
break
}
}
tail := len(segments) - 1
if tail >= 0 {
matches := reVersionTag.FindStringSubmatch(segments[tail])
if len(matches) == 3 {
segments[tail] = matches[1]
segments = append(segments, matches[2])
}
}
sv := &SemVer{}
switch len(segments) {
case 4:
sv.Tag = segments[3]
fallthrough
case 3:
sv.Patch = toInt(segments[2])
fallthrough
case 2:
sv.Minor = toInt(segments[1])
fallthrough
case 1:
sv.Major = toInt(segments[0])
}
return sv
}
func (a *SemVer) Greater(b *SemVer) bool {
if a.Major > b.Major {
return true
} else if a.Major < b.Major {
return false
} else if a.Minor > b.Minor {
return true
} else if a.Minor < b.Minor {
return false
} else if a.Patch > b.Patch {
return true
} else if a.Patch < b.Patch {
return false
} else if len(a.Tag) == 0 && len(b.Tag) > 0 {
return true
} else if len(b.Tag) == 0 && len(a.Tag) > 0 {
return false
}
return a.Tag > b.Tag
}
func SortVersions(vers []string) {
sort.Slice(vers, func(i, j int) bool {
a := NewSemVer(vers[i])
b := NewSemVer(vers[j])
return a.Greater(b)
})
}
type VerItem struct {
Name string
Remote string
Ver string
Sum string
Ext string
SemVer *SemVer
}
func NewVerItem(name, remote, ver, sum, ext string) *VerItem {
if remote != "" {
return &VerItem{
Name: name,
Remote: remote,
Ver: name,
}
}
return &VerItem{
Name: name,
Ver: ver,
Sum: sum,
Ext: ext,
SemVer: NewSemVer(ver),
}
}
type VerItems []*VerItem
func (items VerItems) Sort() {
sort.Slice(items, func(i, j int) bool {
if items[i].Remote != "" {
return items[i].Name > items[j].Name
}
if items[i].SemVer == nil {
items[i].SemVer = NewSemVer(items[i].Ver)
}
if items[j].SemVer == nil {
items[j].SemVer = NewSemVer(items[j].Ver)
}
return items[i].SemVer.Greater(items[j].SemVer)
})
}
type Versions struct {
dict map[string]*VerItem
}
func NewVersions() *Versions {
return &Versions{
dict: make(map[string]*VerItem),
}
}
func NewVersionsFromFile(filename string) (*Versions, error) {
vers := NewVersions()
if err := vers.ReadFile(filename); err != nil {
return nil, err
}
return vers, nil
}
func (vers *Versions) WriteFile(filename string) error {
b, err := json.MarshalIndent(vers.dict, "", " ")
if err != nil {
return err
}
return writeFile(filename, 0, bytes.NewReader(b))
}
func (vers *Versions) ReadFile(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return err
}
dict := make(map[string]*VerItem)
if err = json.Unmarshal(b, &dict); err != nil {
return err
}
vers.dict = dict
return nil
}
func (vers *Versions) Add(name, ver, sum, ext string) bool {
if IsSemVer(ver) {
vers.dict[ver] = NewVerItem(name, "", ver, sum, ext)
return true
}
return false
}
func (vers *Versions) AddBranch(name, remote string) {
vers.dict[name] = NewVerItem(name, remote, "", "", "")
}
func (vers *Versions) GetItem(ver string) *VerItem {
return vers.dict[ver]
}
var reSemVerWithoutTag = regexp.MustCompile(`^\d+[.]\d+([.]\d+)?$`)
func (vers *Versions) PickItem(ver string) *VerItem {
if item := vers.GetItem(ver); item != nil {
return item
}
items := VerItems{}
for _, item := range vers.dict {
if reSemVerWithoutTag.Match([]byte(item.Ver)) {
items = append(items, item)
}
}
items.Sort()
// pick a latest version
if ver == "latest" {
return items[0]
}
// pick a latest version that matches the prefix
for _, item := range items {
if strings.HasPrefix(item.Ver, ver) {
return item
}
}
return nil
}
func (vers *Versions) GetList() (VerItems, int) {
vitems := VerItems{}
maxlen := 0
for ver, item := range vers.dict {
vitems = append(vitems, item)
if len(ver) > maxlen {
maxlen = len(ver)
}
}
vitems.Sort()
return vitems, maxlen
}
func ListTargetVersions(cfg *TargetConfig) string {
var b strings.Builder
fmt.Fprintf(&b, "list available %q versions:\n", cfg.Name)
vers, err := NewVersionsFromFile(cfg.VersionFile)
if err != nil {
fmt.Fprintf(&b, "failed to read version file %q: %v\n", cfg.VersionFile, err)
} else if items, maxlen := vers.GetList(); len(items) > 0 {
format := fmt.Sprintf("%%-%ds", maxlen)
ncols := 0
arr := []string{}
for _, item := range items {
v := fmt.Sprintf(format, item.Ver)
ncols += maxlen + 2
if ncols/80 > 0 {
fmt.Fprintf(&b, "%s\n", strings.Join(arr, " "))
arr = arr[:0]
ncols = maxlen + 2
}
arr = append(arr, v)
}
fmt.Fprintf(&b, "%s\n", strings.Join(arr, " "))
}
return b.String()
}
func PickTargetVersionItem(cfg *TargetConfig, ver string, exactMatch bool) *VerItem {
print("check %s version %q definition ... ", cfg.Name, ver)
vers, err := NewVersionsFromFile(cfg.VersionFile)
if err != nil {
fatalf("failed to read version file %q: %v", cfg.VersionFile, err)
}
var item *VerItem
if exactMatch {
item = vers.GetItem(ver)
} else {
item = vers.PickItem(ver)
}
if item == nil {
printf("not found")
fatalf("%s version %q does not defined in %q\n%s", cfg.Name, ver, cfg.VersionFile, ListTargetVersions(cfg))
} else if item.SemVer != nil && item.Ext != ".tar.gz" {
fatalf("unsupported media-type %q", item.Name)
}
printf("found %q", item.Ver)
return item
}
func CmdVers() {
for _, cfg := range []*TargetConfig{
LuaCfg, LuaJitCfg, LuaRocksCfg,
} {
print(ListTargetVersions(cfg))
print("\n")
}
}