-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathversion.go
274 lines (246 loc) · 7.43 KB
/
version.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
package cask
import (
"fmt"
"regexp"
"strings"
)
// A Version represents a version cask stanza.
type Version struct {
BaseStanza
// Value specifies the stanza value.
Value string
}
// NewVersion creates a new Version instance and returns its pointer. Requires
// Version.Value to be passed as argument.
func NewVersion(value string) *Version {
return &Version{
Value: value,
}
}
// HasVersionStringInterpolation check whether the provided string has a Ruby
// syntax version string interpolation.
func (v Version) HasVersionStringInterpolation(str string) bool {
re := regexp.MustCompile(`#{version.*}`)
if re.MatchString(str) {
return true
}
return false
}
// Major extracts the major semantic version part from Version.Value and returns
// the result string.
func (v Version) Major() (string, error) {
re := regexp.MustCompile(`^\d`)
if re.MatchString(v.Value) {
return re.FindAllString(v.Value, -1)[0], nil
}
return "", fmt.Errorf(`version "%s": no Major() match`, v.Value)
}
// Minor extracts the minor semantic version part from Version.Value and returns
// the result string.
func (v Version) Minor() (string, error) {
re := regexp.MustCompile(`^(?:\d)\.(\d)`)
if re.MatchString(v.Value) {
return re.FindAllStringSubmatch(v.Value, -1)[0][1], nil
}
return "", fmt.Errorf(`version "%s": no Minor() match`, v.Value)
}
// Patch extracts the patch semantic version part from Version.Value and returns
// the result string.
func (v Version) Patch() (string, error) {
re := regexp.MustCompile(`^(?:\d)\.(?:\d)\.(\d)`)
if re.MatchString(v.Value) {
return re.FindAllStringSubmatch(v.Value, -1)[0][1], nil
}
return "", fmt.Errorf(`version "%s": no Patch() match`, v.Value)
}
// MajorMinor extracts the major and minor semantic version parts from
// Version.Value and returns the result string.
func (v Version) MajorMinor() (string, error) {
re := regexp.MustCompile(`^((?:\d)\.(?:\d))`)
if re.MatchString(v.Value) {
return re.FindAllString(v.Value, -1)[0], nil
}
return "", fmt.Errorf(`version "%s": no MajorMinor() match`, v.Value)
}
// MajorMinorPatch extracts the major, minor and patch semantic version parts
// from Version.Value and returns the result string.
func (v Version) MajorMinorPatch() (string, error) {
re := regexp.MustCompile(`^((?:\d)\.(?:\d)\.(?:\d))`)
if re.MatchString(v.Value) {
return re.FindAllString(v.Value, -1)[0], nil
}
return "", fmt.Errorf(`version "%s": no MajorMinorPatch() match`, v.Value)
}
// BeforeComma extracts the Version.Value part before comma and returns the
// result string.
func (v Version) BeforeComma() (string, error) {
re := regexp.MustCompile(`(^.*)\,`)
if re.MatchString(v.Value) {
return re.FindAllStringSubmatch(v.Value, -1)[0][1], nil
}
return "", fmt.Errorf(`version "%s": no BeforeComma() match`, v.Value)
}
// AfterComma extracts the Version.Value part after comma and returns the result
// string.
func (v Version) AfterComma() (string, error) {
re := regexp.MustCompile(`\,(.*$)`)
if re.MatchString(v.Value) {
return re.FindAllStringSubmatch(v.Value, -1)[0][1], nil
}
return "", fmt.Errorf(`version "%s": no AfterComma() match`, v.Value)
}
// BeforeColon extracts the Version.Value part before colon and returns the
// result string.
func (v Version) BeforeColon() (string, error) {
re := regexp.MustCompile(`(^.*)\:`)
if re.MatchString(v.Value) {
return re.FindAllStringSubmatch(v.Value, -1)[0][1], nil
}
return "", fmt.Errorf(`version "%s": no BeforeColon() match`, v.Value)
}
// AfterColon extracts the Version.Value part after colon and returns the result
// string.
func (v Version) AfterColon() (string, error) {
re := regexp.MustCompile(`\:(.*$)`)
if re.MatchString(v.Value) {
return re.FindAllStringSubmatch(v.Value, -1)[0][1], nil
}
return "", fmt.Errorf(`version "%s": no AfterColon() match`, v.Value)
}
// NoDots removes all Version.Value dots and returns the result string.
func (v Version) NoDots() (string, error) {
re := regexp.MustCompile(`\.`)
if re.MatchString(v.Value) {
return re.ReplaceAllString(v.Value, ""), nil
}
return "", fmt.Errorf(`version "%s": no NoDots() match`, v.Value)
}
// DotsToUnderscores converts all Version.Value dots to underscores and returns
// the result string.
func (v Version) DotsToUnderscores() (string, error) {
re := regexp.MustCompile(`\.`)
if re.MatchString(v.Value) {
return re.ReplaceAllString(v.Value, "_"), nil
}
return "", fmt.Errorf(`version "%s": no DotsToUnderscores() match`, v.Value)
}
// DotsToHyphens converts all Version.Value dots to hyphens and returns the
// result string.
func (v Version) DotsToHyphens() (string, error) {
re := regexp.MustCompile(`\.`)
if re.MatchString(v.Value) {
return re.ReplaceAllString(v.Value, "-"), nil
}
return "", fmt.Errorf(`version "%s": no DotsToHyphens() match`, v.Value)
}
// InterpolateIntoString interpolates existing version into the provided string
// with Ruby interpolation syntax.
func (v Version) InterpolateIntoString(str string) (result string) {
result = str
regexInterpolations := regexp.MustCompile(`(#{version})|(#{version\.[^}]*.[^{]*})`)
regexAllMethods := regexp.MustCompile(`(?:^#{version\.)(.*)}`)
// find all version interpolations
matches := regexInterpolations.FindAllStringSubmatch(str, -1)
// for every version interpolation
for _, m := range matches {
match := m[0]
// extract all methods
methodsAll := regexAllMethods.FindAllStringSubmatch(match, -1)
if len(methodsAll) < 1 {
// when no methods, then it's just a version replace
re := regexp.MustCompile(regexp.QuoteMeta(match))
result = re.ReplaceAllString(result, v.Value)
continue
}
methods := strings.Split(methodsAll[0][1], ".")
// for every method
part := v.Value
for _, method := range methods {
switch method {
case "major":
r, err := NewVersion(part).Major()
if err == nil {
part = r
}
break
case "minor":
r, err := NewVersion(part).Minor()
if err == nil {
part = r
}
break
case "patch":
r, err := NewVersion(part).Patch()
if err == nil {
part = r
}
break
case "major_minor":
r, err := NewVersion(part).MajorMinor()
if err == nil {
part = r
}
break
case "major_minor_patch":
r, err := NewVersion(part).MajorMinorPatch()
if err == nil {
part = r
}
break
case "before_comma":
r, err := NewVersion(part).BeforeComma()
if err == nil {
part = r
}
break
case "after_comma":
r, err := NewVersion(part).AfterComma()
if err == nil {
part = r
}
break
case "before_colon":
r, err := NewVersion(part).BeforeColon()
if err == nil {
part = r
}
break
case "after_colon":
r, err := NewVersion(part).AfterColon()
if err == nil {
part = r
}
break
case "no_dots":
r, err := NewVersion(part).NoDots()
if err == nil {
part = r
}
break
case "dots_to_underscores":
r, err := NewVersion(part).DotsToUnderscores()
if err == nil {
part = r
}
break
case "dots_to_hyphens":
r, err := NewVersion(part).DotsToHyphens()
if err == nil {
part = r
}
break
default:
// if one of the methods is unknown, then return full string without any replacements
return result
}
}
re := regexp.MustCompile(regexp.QuoteMeta(match))
result = re.ReplaceAllString(result, part)
}
return result
}
// String returns a string representation of the Version struct which is the
// Version.Value.
func (v Version) String() string {
return v.Value
}