-
-
Notifications
You must be signed in to change notification settings - Fork 363
/
add-check.go
148 lines (123 loc) · 2.85 KB
/
add-check.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
//go:build ignore
package main
import (
"bytes"
"go/format"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"text/template"
)
var tmpl = `
package {{.lname}}
import (
"honnef.co/go/tools/analysis/lint"
"golang.org/x/tools/go/analysis"
)
var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{
Analyzer: &analysis.Analyzer{
Name: "{{.name}}",
Run: run,
Requires: []*analysis.Analyzer{},
},
Doc: &lint.RawDocumentation{
Title: "",
Text: {{.emptyRaw}},
{{- if .quickfix }}
Before: {{.emptyRaw}},
After: {{.emptyRaw}},
{{- end }}
Since: "Unreleased",
Severity: lint.SeverityWarning,
},
})
var Analyzer = SCAnalyzer.Analyzer
func run(pass *analysis.Pass) (any, error) {
return nil, nil
}
`
func main() {
log.SetFlags(0)
var t template.Template
if _, err := t.Parse(tmpl); err != nil {
log.Fatalln("couldn't parse template:", err)
}
if len(os.Args) != 2 {
log.Fatalf("Usage: %s <new check's name>", os.Args[0])
}
name := os.Args[1]
checkRe := regexp.MustCompile(`^([A-Za-z]+)\d{4}$`)
parts := checkRe.FindStringSubmatch(name)
if parts == nil {
log.Fatalf("invalid check name %q", name)
}
var catDir string
prefix := strings.ToUpper(parts[1])
switch prefix {
case "SA":
catDir = "staticcheck"
case "S":
catDir = "simple"
case "ST":
catDir = "stylecheck"
case "QF":
catDir = "quickfix"
default:
log.Fatalf("unknown check prefix %q", prefix)
}
lname := strings.ToLower(name)
dir := filepath.Join(catDir, lname)
dst := filepath.Join(dir, lname+".go")
mkdirp(dir)
buf := bytes.NewBuffer(nil)
vars := map[string]any{
"name": name,
"lname": lname,
"emptyRaw": "``",
"quickfix": prefix == "QF",
}
if err := t.Execute(buf, vars); err != nil {
log.Fatalf("couldn't generate %s: %s", dst, err)
}
b, err := format.Source(buf.Bytes())
if err != nil {
log.Fatalf("couldn't gofmt %s: %s", dst, err)
}
writeFile(dst, b)
testdata := filepath.Join(dir, "testdata", "src", "example.com", "pkg")
mkdirp(testdata)
writeFile(filepath.Join(testdata, "pkg.go"), []byte("package pkg\n"))
out, err := exec.Command("go", "generate", "./...").CombinedOutput()
if err != nil {
log.Printf("could not run 'go generate ./...': %s", err)
log.Println("Output:")
log.Fatalln(string(out))
}
flags := []string{
"add",
"--intent-to-add",
"--verbose",
filepath.Join(dir, lname+"_test.go"),
filepath.Join(testdata, "pkg.go"),
dst,
}
cmd := exec.Command("git", flags...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalln("could not run 'git add':", err)
}
}
func writeFile(path string, data []byte) {
if err := os.WriteFile(path, data, 0677); err != nil {
log.Fatalf("couldn't write %s: %s", path, err)
}
}
func mkdirp(path string) {
if err := os.MkdirAll(path, 0777); err != nil {
log.Fatalf("couldn't create directory %s: %s", path, err)
}
}