-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
versionfile.go
215 lines (196 loc) · 4.79 KB
/
versionfile.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
package tagpr
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
"syscall"
"github.com/saracen/walker"
)
const (
versionRegBase = `(?i)((?:^|[^-_0-9a-zA-Z])version[^-_0-9a-zA-Z].{0,50}?)`
semverRegBase = `([0-9]+\.[0-9]+\.[0-9]+)`
)
var (
versionReg = regexp.MustCompile(versionRegBase + semverRegBase)
versionRegFallback = regexp.MustCompile(semverRegBase)
skipDirs = map[string]bool{
// The "testdata" directory is ommited because of the test code for tagpr itself
"testdata": true,
".git": true,
"node_modules": true,
"vendor": true,
"third_party": true,
"extlib": true,
"docs": true,
// The directory for storing python test code, but it may be inappropriate to omit this directory
// uniformly because it may be used by test libraries for other languages.
"tests": true,
}
skipFiles = map[string]bool{
"requirements.txt": true,
"cpanfile.snapshot": true,
"package-lock.json": true,
}
skipExt = map[string]bool{
".md": true,
".rst": true,
".adoc": true,
}
)
func isSkipFile(n string) bool {
n = strings.ToLower(n)
return strings.HasSuffix(n, ".lock") || skipFiles[n] || skipExt[filepath.Ext(n)]
}
func detectVersionFile(root string, ver *semv) (string, error) {
verReg, err := regexp.Compile(versionRegBase + regexp.QuoteMeta(ver.Naked()))
if err != nil {
return "", err
}
errorCb := func(fpath string, err error) error {
// When running a tagpr binary under the repository root, "text file busy" occurred,
// so I did error handling as this, but it did not solve the problem, and it is a special case,
// so we may not need to do the check in particular.
if os.IsPermission(err) || errors.Is(err, syscall.ETXTBSY) {
return nil
}
return err
}
fl := &fileList{}
if err := walker.Walk(root, func(fpath string, fi os.FileInfo) error {
if fi.IsDir() {
if skipDirs[fi.Name()] {
return filepath.SkipDir
}
return nil
}
if !fi.Mode().IsRegular() || isSkipFile(fi.Name()) {
return nil
}
bs, err := os.ReadFile(fpath)
if err != nil {
return errorCb(fpath, err)
}
if verReg.Match(bs) {
f, _ := filepath.Rel(root, fpath)
fl.append(filepath.ToSlash(f))
}
return nil
}, walker.WithErrorCallback(errorCb)); err != nil {
return "", err
}
// XXX: Whether to adopt a version file when the language is not identifiable?
f, _ := versionFile(fl.list())
return f, nil
}
// The second argument returns the language name in lowercase, but this is a temporary interface
// and is not currently used anywhere.
func versionFile(files []string) (string, string) {
if len(files) < 1 {
return "", ""
}
files = fileOrder(files)
var meta string
for _, f := range files {
if strings.HasSuffix(f, ".gemspec") {
return f, "ruby"
}
if strings.HasSuffix(f, ".go") {
return f, "go"
}
if meta != "" {
if strings.HasPrefix(f, "lib/") && strings.HasSuffix(f, ".pm") {
return f, "perl"
}
}
base := strings.ToLower(filepath.Base(f))
switch base {
case "setup.py", "setup.cfg":
return f, "python"
case "package.json":
return f, "node"
case "manifest.json": // for chrome extension
return f, "javascript"
case "pom.xml":
return f, "java"
case "meta.json":
if meta == "" {
meta = f
}
}
}
if meta != "" {
return meta, "perl"
}
return files[0], ""
}
func fileOrder(list []string) []string {
sort.Slice(list, func(i, j int) bool {
x := list[i]
y := list[j]
xdepth := strings.Count(x, "/")
ydepth := strings.Count(y, "/")
if xdepth != ydepth {
return xdepth < ydepth
}
return strings.Compare(x, y) < 0
})
return list
}
type fileList struct {
l []string
mu sync.RWMutex
}
func (fl *fileList) append(fpath string) {
fl.mu.Lock()
defer fl.mu.Unlock()
fl.l = append(fl.l, fpath)
}
func (fl *fileList) list() []string {
fl.mu.RLock()
defer fl.mu.RUnlock()
return fl.l
}
func bumpVersionFile(fpath string, from, to *semv) error {
verReg, err := regexp.Compile(`(v|\b)` + regexp.QuoteMeta(from.Naked()) + `\b`)
if err != nil {
return err
}
bs, err := os.ReadFile(fpath)
if err != nil {
return err
}
replaced := false
updated := verReg.ReplaceAllFunc(bs, func(match []byte) []byte {
if replaced {
return match
}
replaced = true
return verReg.ReplaceAll(match, []byte(`${1}`+to.Naked()))
})
return os.WriteFile(fpath, updated, 0666)
}
func retrieveVersionFromFile(fpath string, vPrefix bool) (*semv, error) {
bs, err := os.ReadFile(fpath)
if err != nil {
return nil, err
}
var ver string
if m := versionReg.FindSubmatch(bs); len(m) >= 3 {
ver = string(m[2])
} else {
m := versionRegFallback.FindSubmatch(bs)
if len(m) < 2 {
return nil, fmt.Errorf("no version detected from file: %s", fpath)
}
ver = string(m[1])
}
if vPrefix {
ver = "v" + ver
}
return newSemver(ver)
}