-
Notifications
You must be signed in to change notification settings - Fork 4
/
semv.go
157 lines (133 loc) Β· 2.74 KB
/
semv.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
package semv
import (
"fmt"
"github.com/blang/semver"
)
var defaultPreVersion = "0"
var defaultPreVersionPrefix = "alpha"
var defaultTagPrefix = "v"
// Semv struct
type Semv struct {
data semver.Version
list *List
}
// MustNew creates Semv
func MustNew(s string) *Semv {
v, err := semver.ParseTolerant(s)
if err != nil {
panic(err)
}
return &Semv{data: v}
}
// Latest returns latest version
func Latest() (*Semv, error) {
list, err := GetList()
if err != nil {
return nil, err
}
semv := list.WithoutPreRelease().Latest()
semv.list = list
return semv, nil
}
// String to string
func (v *Semv) String() string {
vv := v.data.String()
if vv == defaultVersion {
return ""
}
return defaultTagPrefix + vv
}
// IsEmpty checks empty
func (v *Semv) IsEmpty() bool {
return v.String() == ""
}
// Next returns next version
func (v *Semv) Next(target string) *Semv {
vv := *v
copied := &vv
switch target {
case "major":
copied.incrementMajor()
case "minor":
copied.incrementMinor()
case "patch":
copied.incrementPatch()
}
return copied
}
// PreRelease retuns
func (v *Semv) PreRelease(name string) (*Semv, error) {
list := v.list.OnlyPreRelease()
prefix := name
if name == "" {
prefix = defaultPreVersionPrefix
}
similar := list.FindSimilar(v.data)
if !similar.IsEmpty() {
v.cloneForPreRelease(similar)
}
if len(v.data.Pre) > 0 {
incremented := false
mustIncremnt := false
for i, pre := range v.data.Pre {
if pre.IsNumeric() && mustIncremnt && i < 3 {
v.data.Pre[i].VersionNum++
incremented = true
} else if !pre.IsNumeric() && i == 0 {
if pre.VersionStr == prefix {
mustIncremnt = true
} else if pre.Compare(semver.PRVersion{VersionStr: prefix, IsNum: false}) == 1 {
return nil, fmt.Errorf("%s is less than %s", prefix, pre.VersionStr)
} else {
v.data.Pre[i].VersionStr = prefix
incremented = true
}
}
}
if incremented {
return v, nil
}
}
prV, err := semver.NewPRVersion(prefix)
if err == nil {
v.data.Pre = append(v.data.Pre, prV)
}
prever, err := semver.NewPRVersion(defaultPreVersion)
if err == nil {
v.data.Pre = append(v.data.Pre, prever)
}
return v, nil
}
// Build retuns
func (v *Semv) Build(name string) (*Semv, error) {
if name == "" {
m, err := meta()
if err != nil {
return nil, err
}
v.data.Build = m
} else {
v.data.Build = []string{name}
}
return v, nil
}
func (v *Semv) cloneForPreRelease(vv *Semv) *Semv {
v.data = vv.data
v.data.Build = []string{}
return v
}
func (v *Semv) incrementMajor() {
v.data.Major++
v.data.Minor = 0
v.data.Patch = 0
v.data.Pre = nil
}
func (v *Semv) incrementMinor() {
v.data.Minor++
v.data.Patch = 0
v.data.Pre = nil
}
func (v *Semv) incrementPatch() {
v.data.Patch++
v.data.Pre = nil
}