-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.go
316 lines (288 loc) · 9.89 KB
/
gen.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (c) 2019, Daniel Martí <[email protected]>
// See LICENSE for licensing information
// +build ignore
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
func main() {
pkgs, err := listPackages(context.TODO(), nil,
"cmd/gofmt",
// These are internal cmd dependencies. Copy them.
"cmd/internal/diff",
"golang.org/x/tools/cmd/goimports",
// These are internal goimports dependencies. Copy them.
"golang.org/x/tools/internal/event",
"golang.org/x/tools/internal/event/core",
"golang.org/x/tools/internal/event/keys",
"golang.org/x/tools/internal/event/label",
"golang.org/x/tools/internal/fastwalk",
"golang.org/x/tools/internal/gocommand",
"golang.org/x/tools/internal/gopathwalk",
"golang.org/x/tools/internal/imports",
"golang.org/x/tools/internal/module",
"golang.org/x/tools/internal/semver",
"golang.org/x/tools/internal/telemetry/event",
)
if err != nil {
panic(err)
}
for _, pkg := range pkgs {
switch pkg.ImportPath {
case "cmd/gofmt":
copyGofmt(pkg)
case "golang.org/x/tools/cmd/goimports":
copyGoimports(pkg)
default:
parts := strings.Split(pkg.ImportPath, "/")
if parts[0] == "cmd" {
copyInternal(pkg, filepath.Join(parts[1:]...))
} else {
dir := filepath.Join(append([]string{"gofumports"}, parts[3:]...)...)
copyInternal(pkg, dir)
}
}
}
}
type Module struct {
Path string // module path
Version string // module version
Versions []string // available module versions (with -versions)
Replace *Module // replaced by this module
Time *time.Time // time version was created
Update *Module // available update, if any (with -u)
Main bool // is this the main module?
Indirect bool // is this module only an indirect dependency of main module?
Dir string // directory holding files for this module, if any
GoMod string // path to go.mod file used when loading this module, if any
GoVersion string // go version used in module
Error *ModuleError // error loading module
}
type ModuleError struct {
Err string // the error itself
}
type Package struct {
Dir string // directory containing package sources
ImportPath string // import path of package in dir
ImportComment string // path in import comment on package statement
Name string // package name
Doc string // package documentation string
Target string // install path
Shlib string // the shared library that contains this package (only set when -linkshared)
Goroot bool // is this package in the Go root?
Standard bool // is this package part of the standard Go library?
Stale bool // would 'go install' do anything for this package?
StaleReason string // explanation for Stale==true
Root string // Go root or Go path dir containing this package
ConflictDir string // this directory shadows Dir in $GOPATH
BinaryOnly bool // binary-only package (no longer supported)
ForTest string // package is only for use in named test
Export string // file containing export data (when using -export)
Module *Module // info about package's containing module, if any (can be nil)
Match []string // command-line patterns matching this package
DepOnly bool // package is only a dependency, not explicitly listed
// Source files
GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
CgoFiles []string // .go source files that import "C"
CompiledGoFiles []string // .go files presented to compiler (when using -compiled)
IgnoredGoFiles []string // .go source files ignored due to build constraints
CFiles []string // .c source files
CXXFiles []string // .cc, .cxx and .cpp source files
MFiles []string // .m source files
HFiles []string // .h, .hh, .hpp and .hxx source files
FFiles []string // .f, .F, .for and .f90 Fortran source files
SFiles []string // .s source files
SwigFiles []string // .swig files
SwigCXXFiles []string // .swigcxx files
SysoFiles []string // .syso object files to add to archive
TestGoFiles []string // _test.go files in package
XTestGoFiles []string // _test.go files outside package
// Cgo directives
CgoCFLAGS []string // cgo: flags for C compiler
CgoCPPFLAGS []string // cgo: flags for C preprocessor
CgoCXXFLAGS []string // cgo: flags for C++ compiler
CgoFFLAGS []string // cgo: flags for Fortran compiler
CgoLDFLAGS []string // cgo: flags for linker
CgoPkgConfig []string // cgo: pkg-config names
// Dependency information
Imports []string // import paths used by this package
ImportMap map[string]string // map from source import to ImportPath (identity entries omitted)
Deps []string // all (recursively) imported dependencies
TestImports []string // imports from TestGoFiles
XTestImports []string // imports from XTestGoFiles
// Error information
Incomplete bool // this package or a dependency has an error
Error *PackageError // error loading package
DepsErrors []*PackageError // errors loading dependencies
}
type PackageError struct {
ImportStack []string // shortest path from package named on command line to this one
Pos string // position of error (if present, file:line:col)
Err string // the error itself
}
func getEnv(env []string, name string) string {
for _, kv := range env {
if i := strings.IndexByte(kv, '='); i > 0 && name == kv[:i] {
return kv[i+1:]
}
}
return ""
}
// listPackages is a wrapper for 'go list -json -e', which can take arbitrary
// environment variables and arguments as input. The working directory can be
// fed by adding $PWD to env; otherwise, it will default to the current
// directory.
//
// Since -e is used, the returned error will only be non-nil if a JSON result
// could not be obtained. Such examples are if the Go command is not installed,
// or if invalid flags are used as arguments.
//
// Errors encountered when loading packages will be returned for each package,
// in the form of PackageError. See 'go help list'.
func listPackages(ctx context.Context, env []string, args ...string) (pkgs []*Package, finalErr error) {
goArgs := append([]string{"list", "-json", "-e"}, args...)
cmd := exec.CommandContext(ctx, "go", goArgs...)
cmd.Env = env
cmd.Dir = getEnv(env, "PWD")
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
var stderrBuf bytes.Buffer
cmd.Stderr = &stderrBuf
defer func() {
if finalErr != nil && stderrBuf.Len() > 0 {
// TODO: wrap? but the format is backwards, given that
// stderr is likely multi-line
finalErr = fmt.Errorf("%v\n%s", finalErr, stderrBuf.Bytes())
}
}()
if err := cmd.Start(); err != nil {
return nil, err
}
dec := json.NewDecoder(stdout)
for dec.More() {
var pkg Package
if err := dec.Decode(&pkg); err != nil {
return nil, err
}
pkgs = append(pkgs, &pkg)
}
if err := cmd.Wait(); err != nil {
return nil, err
}
return pkgs, nil
}
func readFile(path string) string {
body, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
return string(body)
}
func writeFile(path, body string) {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
panic(err)
}
if err := ioutil.WriteFile(path, []byte(body), 0o644); err != nil {
panic(err)
}
}
func sourceFiles(pkg *Package) (paths []string) {
var combined []string
for _, list := range [...][]string{
pkg.GoFiles,
pkg.IgnoredGoFiles,
} {
for _, name := range list {
if strings.HasSuffix(name, "_test.go") {
// IgnoredGoFiles can contain test files too.
continue
}
combined = append(combined, filepath.Join(pkg.Dir, name))
}
}
return combined
}
const extraImport = `gformat "mvdan.cc/gofumpt/format"; `
func copyGofmt(pkg *Package) {
const extraSrc = `
// This is the only gofumpt change on gofmt's codebase, besides changing
// the name in the usage text.
gformat.File(fileSet, file, "")
`
for _, path := range sourceFiles(pkg) {
body := readFile(path)
body = fixImports(body)
name := filepath.Base(path)
switch name {
case "doc.go":
continue // we have our own
case "gofmt.go":
if i := strings.Index(body, "\t\"mvdan.cc/gofumpt"); i > 0 {
body = body[:i] + "\n" + extraImport + "\n" + body[i:]
}
if i := strings.Index(body, "res, err := format("); i > 0 {
body = body[:i] + "\n" + extraSrc + "\n" + body[i:]
}
}
body = strings.Replace(body, "gofmt", "gofumpt", -1)
writeFile(name, body)
}
}
func copyGoimports(pkg *Package) {
const extraSrc = `
// This is the only gofumpt change on goimports's codebase, besides
// changing the name in the usage text.
res, err = gformat.Source(res, "")
if err != nil {
return err
}
`
for _, path := range sourceFiles(pkg) {
body := readFile(path)
body = fixImports(body)
name := filepath.Base(path)
switch name {
case "doc.go":
continue // we have our own
case "goimports.go":
if i := strings.Index(body, "\t\"mvdan.cc/gofumpt"); i > 0 {
body = body[:i] + "\n" + extraImport + "\n" + body[i:]
}
if i := strings.Index(body, "if !bytes.Equal"); i > 0 {
body = body[:i] + "\n" + extraSrc + "\n" + body[i:]
}
}
body = strings.Replace(body, "goimports", "gofumports", -1)
writeFile(filepath.Join("gofumports", name), body)
}
}
func copyInternal(pkg *Package, dir string) {
for _, path := range sourceFiles(pkg) {
body := readFile(path)
body = fixImports(body)
name := filepath.Base(path)
writeFile(filepath.Join(dir, name), body)
}
}
func fixImports(body string) string {
body = strings.Replace(body,
"golang.org/x/tools/internal/",
"mvdan.cc/gofumpt/gofumports/internal/",
-1)
body = strings.Replace(body,
"cmd/internal/",
"mvdan.cc/gofumpt/internal/",
-1)
return body
}