-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathgazelle.go
More file actions
172 lines (156 loc) · 3.92 KB
/
gazelle.go
File metadata and controls
172 lines (156 loc) · 3.92 KB
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
// Package stringer is a Gazelle extension that generates go_stringer rules
// from //go:generate stringer directives in Go source files.
package stringer
import (
"bufio"
"flag"
"io"
"os"
"path/filepath"
"strings"
"github.com/bazelbuild/bazel-gazelle/language"
"github.com/bazelbuild/bazel-gazelle/rule"
)
// NewLanguage is called by Gazelle to instantiate this extension.
func NewLanguage() language.Language {
return &lang{}
}
type lang struct {
language.BaseLang
}
func (*lang) Name() string { return "stringer" }
func (*lang) Kinds() map[string]rule.KindInfo {
return map[string]rule.KindInfo{
"go_stringer": {
MatchAttrs: []string{"output"},
NonEmptyAttrs: map[string]bool{"src": true, "type": true, "mod": true, "output": true},
MergeableAttrs: map[string]bool{},
},
}
}
func (*lang) Loads() []rule.LoadInfo {
return []rule.LoadInfo{{
Name: "//bazel/rules/go_stringer:defs.bzl",
Symbols: []string{"go_stringer"},
}}
}
func (*lang) GenerateRules(args language.GenerateArgs) language.GenerateResult {
mod := findGomod(args.Config.RepoRoot, args.Dir)
var rules []*rule.Rule
for _, f := range args.RegularFiles {
if !strings.HasSuffix(f, ".go") {
continue
}
directives, err := parseFile(filepath.Join(args.Dir, f))
if err != nil {
continue
}
for _, d := range directives {
out := d.output
if out == "" {
out = strings.ToLower(strings.SplitN(d.typ, ",", 2)[0]) + "_string.go"
}
r := rule.NewRule("go_stringer", strings.TrimSuffix(out, ".go"))
r.SetAttr("src", f)
r.SetAttr("type", d.typ)
r.SetAttr("mod", mod)
r.SetAttr("output", out)
if d.trimprefix != "" {
r.SetAttr("trimprefix", d.trimprefix)
}
if d.linecomment {
r.SetAttr("linecomment", true)
}
if d.tags != "" {
r.SetAttr("go_tags", d.tags)
}
rules = append(rules, r)
}
}
if len(rules) == 0 {
return language.GenerateResult{}
}
return language.GenerateResult{
Gen: rules,
Imports: make([]interface{}, len(rules)),
}
}
type directive struct {
typ string
output string
trimprefix string
linecomment bool
tags string
}
func parseFile(path string) ([]directive, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
var result []directive
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "//go:generate ") {
continue
}
if d, ok := parseDirective(strings.TrimPrefix(line, "//go:generate ")); ok {
result = append(result, d)
}
}
return result, scanner.Err()
}
func parseDirective(s string) (directive, bool) {
fields := strings.Fields(s)
if len(fields) == 0 {
return directive{}, false
}
var args []string
switch {
case isStringerCmd(fields[0]):
args = fields[1:]
case fields[0] == "go" && len(fields) >= 3 && fields[1] == "run" && isStringerCmd(fields[2]):
args = fields[3:]
default:
return directive{}, false
}
fs := flag.NewFlagSet("stringer", flag.ContinueOnError)
fs.SetOutput(io.Discard)
typ := fs.String("type", "", "")
output := fs.String("output", "", "")
trimprefix := fs.String("trimprefix", "", "")
linecomment := fs.Bool("linecomment", false, "")
tags := fs.String("tags", "", "")
if err := fs.Parse(args); err != nil || *typ == "" {
return directive{}, false
}
return directive{
typ: *typ,
output: *output,
trimprefix: *trimprefix,
linecomment: *linecomment,
tags: *tags,
}, true
}
func isStringerCmd(s string) bool {
return s == "stringer" ||
strings.HasSuffix(s, "/stringer") ||
strings.HasSuffix(s, "/cmd/stringer")
}
func findGomod(repoRoot, dir string) string {
for d := dir; ; {
if _, err := os.Stat(filepath.Join(d, "go.mod")); err == nil {
rel, err := filepath.Rel(repoRoot, d)
if err != nil || rel == "." {
return "//:go.mod"
}
return "//" + filepath.ToSlash(rel) + ":go.mod"
}
parent := filepath.Dir(d)
if parent == d {
return "//:go.mod"
}
d = parent
}
}