-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
releaser.go
191 lines (150 loc) · 3.32 KB
/
releaser.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
package pkg
import (
"fmt"
"strconv"
"strings"
"golang.org/x/mod/semver"
)
type version struct {
major int
minor int
patch int
preString string
}
type Releaser struct {
raw string
v version
next version
preTag string
}
func (r *Releaser) HasPrerelease() bool {
return len(r.v.preString) > 0
}
func (r *Releaser) String() (s string) {
var b strings.Builder
b.Write([]byte("v"))
b.WriteString(strconv.Itoa(r.next.major))
b.WriteString(".")
b.WriteString(strconv.Itoa(r.next.minor))
b.WriteString(".")
b.WriteString(strconv.Itoa(r.next.patch))
if len(r.next.preString) > 0 {
b.Write([]byte("-"))
b.Write([]byte(r.next.preString))
}
s = b.String()
return
}
type ReleaserMod func(*Releaser)
func CreateNewReleaser(vString string, mods ...ReleaserMod) (r *Releaser, err error) {
if !semver.IsValid(vString) {
err = fmt.Errorf("invalid version string")
return
}
r = &Releaser{}
r.raw = vString
simplifiedV := semver.Canonical(vString)
vParts := strings.Split(simplifiedV, ".")
preParts := strings.Split(vParts[2], "-")
if len(preParts) > 1 {
r.v.patch, err = strconv.Atoi(preParts[0])
} else {
r.v.patch, err = strconv.Atoi(vParts[2])
}
if err != nil {
return
}
r.v.minor, err = strconv.Atoi(vParts[1])
if err != nil {
return
}
majorStr := strings.Replace(vParts[0], "v", "", -1)
r.v.major, err = strconv.Atoi(majorStr)
if err != nil {
return
}
r.v.preString = semver.Prerelease(vString)
r.preTag = strings.Split(r.v.preString, ".")[0]
r.next.major = r.v.major
r.next.minor = r.v.minor
r.next.patch = r.v.patch
r.next.preString = r.v.preString
for _, mod := range mods {
mod(r)
}
return
}
func WithPrerelease(pre string) ReleaserMod {
return func(r *Releaser) {
r.next.preString = pre
}
}
func WithPrereleaseIncrement() ReleaserMod {
return func(r *Releaser) {
// if a pre string already exists
if r.HasPrerelease() {
preParts := strings.Split(r.v.preString, ".")
prePointer, _ := strconv.Atoi(preParts[1])
prePointer += 1
preParts[1] = strconv.Itoa(prePointer)
preParts[0] = r.preTag
r.next.preString = strings.Join(preParts[:], ".")
return
}
preToCreate := []string{
r.preTag,
"0",
}
r.next.preString = strings.Join(preToCreate[:], ".")
}
}
func WithMajorIncrement() ReleaserMod {
return func(r *Releaser) {
r.next.major += 1
}
}
func WithMinorIncrement() ReleaserMod {
return func(r *Releaser) {
r.next.minor += 1
}
}
func WithPatchIncrement() ReleaserMod {
return func(r *Releaser) {
r.next.patch += 1
}
}
func WithMajorReset() ReleaserMod {
return func(r *Releaser) {
r.next.major = 0
}
}
func WithMinorReset() ReleaserMod {
return func(r *Releaser) {
r.next.minor = 0
}
}
func WithPatchReset() ReleaserMod {
return func(r *Releaser) {
r.next.patch = 0
}
}
func WithPrereleaseReset() ReleaserMod {
return func(r *Releaser) {
preParts := strings.Split(r.v.preString, ".")
// reset something like `beta.1` to `beta.0`
preParts[1] = strconv.Itoa(0)
nextPreString := strings.Join(preParts[:], ".")
nextPreString = strings.TrimPrefix(nextPreString, "-")
r.next.preString = nextPreString
}
}
func WithClearPrerelease() ReleaserMod {
return func(r *Releaser) {
r.next.preString = ""
}
}
func WithPreTag(preTag string) ReleaserMod {
return func(r *Releaser) {
r.preTag = preTag
}
}