This repository has been archived by the owner on Aug 27, 2018. It is now read-only.
/
gogenerate.go
294 lines (228 loc) · 8 KB
/
gogenerate.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) 2016 Paul Jolly <paul@myitcv.org.uk>, all rights reserved.
// Use of this document is governed by a license found in the LICENSE document.
// Package gogenerate exposes some of the unexported internals of the go generate command as a convenience
// for the authors of go generate generators. See https://github.com/myitcv/gogenerate/wiki/Go-Generate-Notes
// for further notes on such generators. It also exposes some convenience functions that might be useful
// to authors of generators
//
package gogenerate // import "myitcv.io/gogenerate"
import (
"bufio"
"bytes"
"flag"
"fmt"
"go/build"
"os"
"path/filepath"
"strings"
)
// These constants correspond in name and value to the details given in
// go generate --help
const (
GOARCH = "GOARCH"
GOFILE = "GOFILE"
GOLINE = "GOLINE"
GOOS = "GOOS"
GOPACKAGE = "GOPACKAGE"
GOPATH = "GOPATH"
GoGeneratePrefix = "//go:generate"
)
const (
// genStr is the string used in the prefix of generated files
genStr = "gen"
// sep is the separator used between the parts of a file name; the prefix used to identify
// generated files, the name (body) and the suffix used to identify the generator
sep = "_"
// genFilePrefix is the prefix used on all generated files (which strictly speaking
// is limited to Go files as far as this definition is concerned, but in practice need not be)
genFilePrefix = genStr + sep
)
const (
// FlagLog is the name of the common flag shared between go generate generators
// to control logging verbosity.
FlagLog = "gglog"
// FlagOutPkgPrefix is the prefix used for flags generated by OutPkgFlag.
FlagOutPkgPrefix = "outpkg:"
// FlagLicenseFile is the name of the common flag shared between go generate generators
// to provide a license header file.
FlagLicenseFile = "licenseFile"
)
type LogLevel string
// The various log levels supported by the flag returned by LogFlag()
const (
LogInfo LogLevel = "info"
LogWarning LogLevel = "warning"
LogError LogLevel = "error"
LogFatal LogLevel = "fatal"
)
// FileIsGenerated determines wheter the Go file located at path is generated or not
// and if it is generated returns the base name of the generator that generated it
func FileIsGenerated(path string) (string, bool) {
fn := filepath.Base(path)
if !strings.HasPrefix(fn, genFilePrefix) || !strings.HasSuffix(fn, ".go") {
return "", false
}
fn = strings.TrimPrefix(fn, genFilePrefix)
fn = strings.TrimSuffix(fn, ".go")
if strings.HasSuffix(fn, "_test") {
fn = strings.TrimSuffix(fn, "_test")
}
// deals with the edge case gen_.go or gen__test.go
if fn == "" {
return "", false
}
parts := strings.Split(fn, sep)
return parts[len(parts)-1], true
}
// FileGeneratedBy returns true if the base name of the supplied path is a Go file that
// would have been generated by the supplied cmd
func FileGeneratedBy(path string, cmd string) bool {
cmd = filepath.Base(cmd)
c, ok := FileIsGenerated(path)
return ok && c == cmd
}
// NameFileFromFile uses the provided filename as a template and returns a generated filename consistent with
// the provided command
func NameFileFromFile(name string, cmd string) (string, bool) {
dir := filepath.Dir(name)
name = filepath.Base(name)
if !strings.HasSuffix(name, ".go") {
return "", false
}
name = strings.TrimSuffix(name, ".go")
cmd = filepath.Base(cmd)
var res string
if strings.HasSuffix(name, "_test") {
name = strings.TrimSuffix(name, "_test")
res = NameTestFile(name, cmd)
} else {
res = NameFile(name, cmd)
}
return filepath.Join(dir, res), true
}
func nameBase(name string, cmd string) string {
res := genStr
if name != "" {
res += sep + name
}
res += sep + cmd
return res
}
// NameFile returns a file name that conforms with the pattern associated with
// files generated by the provided command
func NameFile(name string, cmd string) string {
cmd = filepath.Base(cmd)
return nameBase(name, cmd) + ".go"
}
// NameTestFile returns a file name that conforms with the pattern associated
// with files generated by the provided command
func NameTestFile(name string, cmd string) string {
cmd = filepath.Base(cmd)
return nameBase(name, cmd) + "_test.go"
}
type outputs []string
func (o *outputs) String() string {
return fmt.Sprint(*o)
}
func (o *outputs) Set(value string) error {
*o = append(*o, value)
return nil
}
var outputsSet = make(map[string]bool)
// OutPkgFlag defines a new flag "outpkg:"+key that can accept a list of
// package specifications that represent output targets above and beyond the
// default of self.
func OutPkgFlag(key string) *outputs {
if outputsSet[key] {
panic(fmt.Errorf("already defined outpkg flag for key %q", key))
}
// safe doing this because we should be in init phase
outputsSet[key] = true
var res outputs
flag.Var(&res, "outpkg:"+key, "list of output packages in addition to self")
return &res
}
// LogFlag defines a command line string flag named according to the constant
// FlagLog and returns a pointer to the string the flag sets
func LogFlag() *string {
return flag.String(FlagLog, string(LogFatal), "log level; one of info, warning, error, fatal")
}
// LicenseFileFlag defines a command line string flag named according to the
// constant FlagLicenseFile and returns a pointer ot the string that flag set
func LicenseFileFlag() *string {
return flag.String(FlagLicenseFile, "", "file that contains a license header to be inserted at the top of each generated file")
}
// CommentLicenseHeader is a convenience function to be used in conjunction
// with LicenseFileFlag; if a filename is provided it reads the contents of the
// file and returns a line-commented transformation of the contents with a
// final blank newline
func CommentLicenseHeader(file *string) (string, error) {
if file == nil || *file == "" {
return "", nil
}
fi, err := os.Open(*file)
if err != nil {
return "", fmt.Errorf("could not open file %q: %v", *file, err)
}
res := bytes.NewBuffer(nil)
lastLineEmpty := false
scanner := bufio.NewScanner(fi)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
lastLineEmpty = line == ""
fmt.Fprintln(res, "//", line)
}
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("failed to scan file %v: %v", *file, err)
}
// ensure we have a space before package
if !lastLineEmpty {
fmt.Fprintln(res)
}
return res.String(), nil
}
// DefaultLogLevel is provided simply as a convenience along with LogFlag to ensure a default LogLevel
// in a flag variable. This is necessary because of the interplay between go generate argument parsing
// and the advice given for log levels via gg.
func DefaultLogLevel(f *string, ll LogLevel) {
if f != nil && *f == "" {
*f = string(ll)
}
}
// FilesContainingCmd returns a map of Go file name (defined by go list as
// GoFiles + CgoFiles + TestGoFiles + XTestGoFiles) in the directory dir, to a
// count of the number of times directive command appears in that file. (after
// quote and variable expansion as described by go generate -help). When
// comparing commands, the filepath.Base of each is compared. The file names
// will, by definition, be relative to dir
func FilesContainingCmd(dir string, command string) (map[string]int, error) {
command = filepath.Base(strings.TrimSpace(command))
if command == "" {
return nil, nil
}
pkg, err := build.ImportDir(dir, build.IgnoreVendor)
if err != nil {
return nil, err
}
// GoFiles+CgoFiles+TestGoFiles+XTestGoFiles per go list
// these are all relative to path
gofiles := make([]string, 0, len(pkg.GoFiles)+len(pkg.CgoFiles)+len(pkg.TestGoFiles)+len(pkg.XTestGoFiles))
gofiles = append(gofiles, pkg.GoFiles...)
gofiles = append(gofiles, pkg.CgoFiles...)
gofiles = append(gofiles, pkg.TestGoFiles...)
gofiles = append(gofiles, pkg.XTestGoFiles...)
matchMap := make(map[string]int)
for _, f := range gofiles {
checkMatch := func(line int, args []string) error {
if filepath.Base(args[0]) == command {
matchMap[f] += 1
}
return nil
}
err = DirFunc(pkg.ImportPath, dir, f, checkMatch)
if err != nil {
return nil, err
}
}
return matchMap, nil
}