-
-
Notifications
You must be signed in to change notification settings - Fork 377
/
generate.go
110 lines (88 loc) · 2.07 KB
/
generate.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
//go:build ignore
package main
import (
"bytes"
"go/format"
"log"
"os"
"path/filepath"
"regexp"
"text/template"
)
var tmpl = `
{{define "analyzers"}}
// Code generated by generate.go. DO NOT EDIT.
package {{.dir}}
import (
"honnef.co/go/tools/analysis/lint"
{{- range $check := .checks}}
"honnef.co/go/tools/{{$.dir}}/{{$check}}"
{{- end}}
)
var Analyzers = []*lint.Analyzer{
{{- range $check := .checks}}
{{$check}}.SCAnalyzer,
{{- end}}
}
{{end}}
{{define "tests"}}
// Code generated by generate.go. DO NOT EDIT.
package {{.check}}
import (
"testing"
"honnef.co/go/tools/analysis/lint/testutil"
)
func TestTestdata(t *testing.T) {
testutil.Run(t, SCAnalyzer)
}
{{end}}
`
func main() {
log.SetFlags(0)
dir, err := os.Getwd()
if err != nil {
log.Fatalln("couldn't determine current directory:", err)
}
dir = filepath.Base(dir)
var t template.Template
if _, err = t.Parse(tmpl); err != nil {
log.Fatalln("couldn't parse templates:", err)
}
dirs, err := filepath.Glob("*")
if err != nil {
log.Fatalln("couldn't enumerate checks:", err)
}
checkRe := regexp.MustCompile(`^[a-z]+\d{4}$`)
out := dirs[:0]
for _, dir := range dirs {
if checkRe.MatchString(dir) {
out = append(out, dir)
}
}
dirs = out
buf := bytes.NewBuffer(nil)
if err := t.ExecuteTemplate(buf, "analyzers", map[string]any{"checks": dirs, "dir": dir}); err != nil {
log.Fatalln("couldn't generate analysis.go:", err)
}
b, err := format.Source(buf.Bytes())
if err != nil {
log.Fatalln("couldn't gofmt analysis.go:", err)
}
if err := os.WriteFile("analysis.go", b, 0666); err != nil {
log.Fatalln("couldn't write analysis.go:", err)
}
for _, dir := range dirs {
buf.Reset()
dst := filepath.Join(dir, dir+"_test.go")
if err := t.ExecuteTemplate(buf, "tests", map[string]any{"check": dir}); 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)
}
if err := os.WriteFile(dst, b, 0666); err != nil {
log.Fatalf("couldn't write %s: %s", dst, err)
}
}
}