-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemver.go
398 lines (330 loc) · 11.8 KB
/
semver.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package duat
import (
"bufio"
"encoding/binary"
"fmt"
"html"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/eknkc/basex" // MIT License
"github.com/karlmutch/duat/version"
"gopkg.in/src-d/go-git.v4/plumbing"
// The following packages are forked to retain copies in the event github accounts are shutdown
//
// I am torn between this and just letting dep ensure with a checkedin vendor directory
// to do this. In any event I ended up doing both with my own forks
"github.com/Masterminds/semver"
"github.com/go-stack/stack" // Forked copy of https://github.com/go-stack/stack
"github.com/jjeffery/kv" // Forked copy of https://github.com/jjeffery/kv
)
// docHandler allows us to have different document types with regular expressions for each
// that can be used by the code for scraping and saving versions into human readable documents
type docHandler struct {
ext string
fnMatcher *regexp.Regexp
find *regexp.Regexp
replace *regexp.Regexp
html *regexp.Regexp
subst string
}
var (
handlers = []*docHandler{}
)
func (docHandler) GetExts() (exts []string) {
for _, handler := range handlers {
exts = append(exts, handler.ext)
}
return exts
}
func addHandler(fnMatcher string, find string, replace string, html string, subst string) (err kv.Error) {
handler := &docHandler{
ext: filepath.Ext(fnMatcher),
subst: subst,
}
r, errGo := regexp.Compile(fnMatcher)
if errGo != nil {
return kv.Wrap(errGo, "internal error please notify [email protected]").With("stack", stack.Trace().TrimRuntime()).With("version", version.GitHash)
return
}
handler.fnMatcher = r
if r, errGo = regexp.Compile(find); errGo != nil {
return kv.Wrap(errGo, "internal error please notify [email protected]").With("stack", stack.Trace().TrimRuntime()).With("version", version.GitHash)
}
handler.find = r
if r, errGo = regexp.Compile(replace); errGo != nil {
return kv.Wrap(errGo, "internal error please notify [email protected]").With("stack", stack.Trace().TrimRuntime()).With("version", version.GitHash)
}
handler.replace = r
if len(html) == 0 {
return nil
}
if r, errGo = regexp.Compile(html); errGo != nil {
return kv.Wrap(errGo, "internal error please notify [email protected]").With("stack", stack.Trace().TrimRuntime()).With("version", version.GitHash)
}
handler.html = r
handlers = append(handlers, handler)
return nil
}
func getHandler(fn string) (handler *docHandler, err kv.Error) {
for _, h := range handlers {
if h.fnMatcher.Match([]byte(fn)) {
return h, nil
}
}
return nil, kv.NewError("no version handler for file type").With("file", filepath.Base(fn)).With("stack", stack.Trace().TrimRuntime()).With("version", version.GitHash)
}
func init() {
if err := addHandler(".*\\.adoc", ":Revision:.*", ":Revision:\\s*(.*)", ":Revision:\\s*", ":Revision: %s"); err != nil {
fmt.Fprintf(os.Stderr, "asciidoc %v\n", err)
}
if err := addHandler(".*\\.md", "\\<repo-version\\>.*?\\</repo-version\\>", "\\<repo-version\\>(.*?)\\</repo-version\\>", "<[^>]*>", "<repo-version>%s</repo-version>"); err != nil {
fmt.Fprintf(os.Stderr, "markdown %v\n", err)
}
}
func (md *MetaData) LoadVer(fn string) (ver *semver.Version, err kv.Error) {
if md.SemVer != nil {
return nil, kv.NewError("version already loaded").With("stack", stack.Trace().TrimRuntime()).With("file", fn)
}
handler, err := getHandler(fn)
if err != nil {
return nil, err
}
file, errGo := os.Open(fn)
if errGo != nil {
return nil, kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime()).With("file", fn)
}
defer file.Close()
scan := bufio.NewScanner(file)
for scan.Scan() {
versions := handler.find.FindAllString(scan.Text(), -1)
if len(versions) == 0 {
continue
}
for _, version := range versions {
if ver == nil {
extracted := html.UnescapeString(handler.html.ReplaceAllString(version, ""))
if len(extracted) == 0 {
continue
}
ver, errGo = semver.NewVersion(extracted)
if errGo != nil {
return nil, kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime()).With("file", fn).With("extracted", extracted).With("version", version)
}
continue
}
newVer := html.UnescapeString(handler.html.ReplaceAllString(version, ""))
if newVer != ver.String() {
return nil, kv.NewError("all repo-version trimming tags must have the same version string").With("stack", stack.Trace().TrimRuntime()).With("file", fn)
}
}
}
if ver == nil {
return nil, kv.NewError("version not found").With("stack", stack.Trace().TrimRuntime()).With("file", fn)
}
md.SemVer, errGo = semver.NewVersion(ver.String())
if errGo != nil {
md.SemVer = nil
return nil, kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime()).With("file", fn)
}
return ver, nil
}
func (md *MetaData) Apply(files []string) (err kv.Error) {
if len(files) == 0 {
return kv.NewError("the apply command requires that files are specified with the -t option").With("stack", stack.Trace().TrimRuntime())
}
checkedFiles := make([]string, 0, len(files))
for _, file := range files {
if len(file) != 0 {
if _, errGo := os.Stat(file); errGo != nil {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime()).With("file", file)
}
checkedFiles = append(checkedFiles, file)
}
}
if len(checkedFiles) != len(files) {
return kv.NewError("no usable targets were found to apply the version to").With("stack", stack.Trace().TrimRuntime())
}
// Process the files but stop on any errors
for _, file := range checkedFiles {
if err = md.Replace(file, file, false); err != nil {
return err
}
}
return nil
}
// BumpPrerelease will first bump the release, adn then write the results into
// the file nominated as the version file
//
func (md *MetaData) BumpPrerelease() (result *semver.Version, err kv.Error) {
if _, err := md.Prerelease(); err != nil {
return nil, err
}
if err := md.Replace(md.VerFile, md.VerFile, false); err != nil {
return nil, err
}
return md.SemVer, nil
}
var (
alphaEncoder = &basex.Encoding{}
)
func init() {
alphaEncoder, _ = basex.NewEncoding("abcdefghijkmnopqrstuvwxyz")
}
func (md *MetaData) IncRC() (result *semver.Version, err kv.Error, warnings []kv.Error) {
warnings = []kv.Error{}
// Open the Github repo and get all tags for the release
// and go through looking for other release candidate tags.
repo := md.Git.Repo
iter, errGo := repo.Tags()
if errGo != nil {
return nil, kv.Wrap(err).With("stack", stack.Trace().TrimRuntime()), warnings
}
// Set some defaults for the release candidate
nextRCParts := []string{"rc", "1"}
nextRC := 1
result = md.SemVer
if errGo := iter.ForEach(func(ref *plumbing.Reference) error {
tag := ref.Name().String()
tagParts := strings.SplitN(tag, "/", 3)
tag = tagParts[2]
ver, errGo := semver.NewVersion(tag)
if errGo != nil {
warnings = append(warnings, kv.Wrap(errGo).With("semver", tag, "stack", stack.Trace().TrimRuntime()))
return nil
}
// Ensure the main version portion is the version for which we want an incremented rc version number
if ver.Major() != md.SemVer.Major() || ver.Minor() != md.SemVer.Minor() || ver.Patch() != md.SemVer.Patch() {
return nil
}
tag = ver.Prerelease()
if strings.HasPrefix(tag, "rc.") {
parts := strings.Split(tag, ".")
if len(parts) < 2 {
warnings = append(warnings, kv.NewError("unrecognized release candidate format expected rc.nnn.").With("prerelease", tag, "stack", stack.Trace().TrimRuntime()))
return nil
}
rc, errGo := strconv.Atoi(parts[1])
if errGo != nil {
warnings = append(warnings, kv.NewError("unrecognized release candidate format expected rc.nnn.").With("prerelease", tag, "stack", stack.Trace().TrimRuntime()))
return nil
}
if rc >= nextRC {
nextRC = rc + 1
nextRCParts = parts
nextRCParts[1] = strconv.Itoa(nextRC)
}
}
return nil
}); errGo != nil {
return nil, kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime()), warnings
}
result.SetMetadata("")
pre := strings.Join(nextRCParts, ".")
ver, errGo := md.SemVer.SetPrerelease(pre)
if errGo != nil {
return nil, kv.Wrap(errGo).With("preRelease", pre, "stack", stack.Trace().TrimRuntime()), warnings
}
return &ver, nil, warnings
}
func (md *MetaData) Prerelease() (result *semver.Version, err kv.Error) {
if md.Git == nil || md.Git.Err != nil {
if md.Git.Err != nil {
return nil, md.Git.Err
} else {
return nil, kv.NewError("an operation that required git could not locate git information").With("stack", stack.Trace().TrimRuntime())
}
}
// Generate a pre-release suffix for semver that uses a mixture of the branch name
// with nothing but hyphens and alpha numerics, followed by a timestamp encoded using
// semver compatible Base24 in a way that preserves sort ordering and that uses all
// lower case letters only to respect DNS naming standards
//
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(time.Now().Unix()))
build := alphaEncoder.Encode(b)
// Git branch names can contain characters that would confuse semver including the
// _ (underscore), and + (plus) characters, https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
cleanBranch := ""
for _, aChar := range md.Git.Branch {
if aChar < '0' || aChar > 'z' || (aChar > '9' && aChar < 'A') || (aChar > 'Z' && aChar < 'a') {
cleanBranch += "-"
} else {
cleanBranch += string(aChar)
}
}
result = md.SemVer
newVer, errGo := result.SetPrerelease(fmt.Sprintf("%s-%s", cleanBranch, build))
if errGo != nil {
return nil, kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
md.SemVer = &newVer
return md.SemVer, nil
}
func (md *MetaData) Replace(fn string, dest string, substitute bool) (err kv.Error) {
// To prevent destructive replacements first copy the file then modify the copy
// and in an atomic operation copy the copy back over the original file, then
// delete the working file
origFn, errGo := filepath.Abs(fn)
if errGo != nil {
return kv.Wrap(errGo, "input file could not be resolved to an absolute file path").With("stack", stack.Trace().TrimRuntime()).With("file", fn)
}
tmp, errGo := ioutil.TempFile(filepath.Dir(origFn), filepath.Base(origFn))
if errGo != nil {
return kv.Wrap(errGo, "temporary file could not be generated").With("stack", stack.Trace().TrimRuntime()).With("file", fn)
}
defer func() {
defer os.Remove(tmp.Name())
tmp.Close()
}()
handler, err := getHandler(fn)
if err != nil {
return err
}
file, errGo := os.OpenFile(origFn, os.O_RDWR, 0600)
if errGo != nil {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime()).With("file", fn)
}
ver := md.SemVer.String()
if len(md.SemVer.Original()) != 0 {
ver = md.SemVer.Original()
}
newVer := fmt.Sprintf(handler.subst, ver)
if substitute {
newVer = ver
}
scan := bufio.NewScanner(file)
for scan.Scan() {
tmp.WriteString(handler.replace.ReplaceAllString(scan.Text(), newVer) + "\n")
}
tmp.Sync()
if fn == dest {
defer file.Close()
} else {
file.Close()
// Overwrite the output file if it is present
file, errGo = os.OpenFile(dest, os.O_CREATE|os.O_RDWR, 0600)
if errGo != nil {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime()).With("file", fn)
}
defer file.Close()
}
// Ignore kv.if the rewind fails as this could be a stdout style file
_, _ = file.Seek(0, io.SeekStart)
if _, errGo = tmp.Seek(0, io.SeekStart); errGo != nil {
return kv.Wrap(errGo, "failed to rewind a temporary file").With("stack", stack.Trace().TrimRuntime()).With("file", fn)
}
// Copy the output file on top of the original file
written, errGo := io.Copy(file, tmp)
if errGo != nil {
return kv.Wrap(errGo, "failed to update the output file").With("stack", stack.Trace().TrimRuntime()).With("file", fn)
}
// Because we overwrote the file we need to trim off the end of the file if it shrank in size
file.Truncate(written)
return nil
}