-
Notifications
You must be signed in to change notification settings - Fork 4
/
version_file.go
45 lines (36 loc) · 949 Bytes
/
version_file.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
package release
import (
"bytes"
"regexp"
"strings"
"github.com/gobuffalo/genny"
"github.com/pkg/errors"
)
var versionRx = regexp.MustCompile("[const|var] [vV]ersion = [`\"](.+)[`\"]")
func writeVersionFile(opts *Options) genny.RunFn {
return func(r *genny.Runner) error {
f, err := r.FindFile(opts.VersionFile)
if err != nil {
return errors.WithStack(err)
}
var matches []string
bb := &bytes.Buffer{}
for _, line := range strings.Split(f.String(), "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "//") {
bb.WriteString(line + "\n")
continue
}
matches = versionRx.FindStringSubmatch(line)
if len(matches) > 1 {
v := matches[1]
bb.WriteString(strings.Replace(line, v, opts.Version, 1) + "\n")
continue
}
bb.WriteString(line + "\n")
}
body := strings.TrimSpace(bb.String())
f = genny.NewFile(f.Name(), strings.NewReader(body+"\n"))
return r.File(f)
}
}