forked from kardianos/govendor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewrite.go
207 lines (189 loc) · 4.81 KB
/
rewrite.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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package context
import (
"go/ast"
"go/parser"
"go/printer"
"go/token"
"strconv"
"strings"
"github.com/dchest/safefile"
"github.com/kardianos/govendor/internal/pathos"
os "github.com/kardianos/govendor/internal/vos"
)
// Rewrite rewrites files to the local path.
func (ctx *Context) rewrite() error {
if !ctx.rewriteImports {
return nil
}
if ctx.dirty {
ctx.loadPackage()
}
ctx.dirty = true
fileImports := make(map[string]map[string]*File) // map[ImportPath]map[FilePath]File
for _, pkg := range ctx.Package {
for _, f := range pkg.Files {
for _, imp := range f.Imports {
fileList := fileImports[imp]
if fileList == nil {
fileList = make(map[string]*File, 1)
fileImports[imp] = fileList
}
fileList[f.Path] = f
}
}
}
filePaths := make(map[string]*File, len(ctx.RewriteRule))
for from, to := range ctx.RewriteRule {
// Add files that contain an import path to rewrite.
for _, f := range fileImports[from] {
filePaths[f.Path] = f
}
// Add files that contain import comments to remove.
if pkg := ctx.Package[from]; pkg != nil {
for _, f := range pkg.Files {
if len(f.ImportComment) != 0 {
filePaths[f.Path] = f
}
}
}
if pkg := ctx.Package[to]; pkg != nil {
for _, f := range pkg.Files {
if len(f.ImportComment) != 0 {
filePaths[f.Path] = f
}
}
}
}
/*
RULE: co2/internal/co3/pk3 -> co1/internal/co3/pk3
i co1/internal/co2/pk2 [co2/pk2] < ["co1/pk1"]
i co1/internal/co3/pk3 [co3/pk3] < ["co1/pk1"]
e co2/internal/co3/pk3 [co3/pk3] < ["co1/internal/co2/pk2"]
l co1/pk1 < []
s strings < ["co1/internal/co3/pk3" "co2/internal/co3/pk3"]
Rewrite the package "co1/internal/co2/pk2" because it references a package with a rewrite.from package.
*/
ctx.updatePackageReferences()
for from := range ctx.RewriteRule {
pkg := ctx.Package[from]
if pkg == nil {
continue
}
for _, ref := range pkg.referenced {
for _, f := range ref.Files {
dprintf("REF RW %s\n", f.Path)
filePaths[f.Path] = f
}
}
}
defer func() {
ctx.RewriteRule = make(map[string]string, 3)
}()
if len(ctx.RewriteRule) == 0 {
return nil
}
goprint := &printer.Config{
Mode: printer.TabIndent | printer.UseSpaces,
Tabwidth: 8,
}
for _, fileInfo := range filePaths {
if pathos.FileHasPrefix(fileInfo.Path, ctx.RootDir) == false {
continue
}
// Read the file into AST, modify the AST.
fileset := token.NewFileSet()
f, err := parser.ParseFile(fileset, fileInfo.Path, nil, parser.ParseComments)
if f == nil {
return nil
}
pkgNameNormalized := strings.TrimSuffix(f.Name.Name, "_test")
// Files with package name "documentation" should be ignored, per go build tool.
if pkgNameNormalized == "documentation" {
return nil
}
dprintf("RW:: File: %s\n", fileInfo.Path)
for _, impNode := range f.Imports {
imp, err := strconv.Unquote(impNode.Path.Value)
if err != nil {
return err
}
for from, to := range ctx.RewriteRule {
if imp != from {
continue
}
impNode.Path.Value = strconv.Quote(to)
for i, metaImport := range fileInfo.Imports {
if from == metaImport {
dprintf("\tImport: %s -> %s\n", from, to)
fileInfo.Imports[i] = to
}
}
break
}
}
// Remove import comment.
st := fileInfo.Package.Status
if st.Location == LocationVendor || st.Location == LocationExternal {
var ic *ast.Comment
if f.Name != nil {
pos := f.Name.Pos()
big:
// Find the next comment after the package name.
for _, cblock := range f.Comments {
for _, c := range cblock.List {
if c.Pos() > pos {
ic = c
break big
}
}
}
}
if ic != nil {
// If it starts with the import text, assume it is the import comment and remove.
if index := strings.Index(ic.Text, " import "); index > 0 && index < 5 {
ic.Text = strings.Repeat(" ", len(ic.Text))
}
}
}
// Don't sort or modify the imports to minimize diffs.
// Write the AST back to disk.
fi, err := os.Stat(fileInfo.Path)
if err != nil {
return err
}
w, err := safefile.Create(fileInfo.Path, fi.Mode())
if err != nil {
return err
}
err = goprint.Fprint(w, fileset, f)
if err != nil {
w.Close()
return err
}
err = w.Commit()
if err != nil {
return err
}
}
return nil
}
func (ctx *Context) makeSet(pkg *Package, mvSet map[*Package]struct{}) {
mvSet[pkg] = struct{}{}
for _, f := range pkg.Files {
for _, imp := range f.Imports {
next := ctx.Package[imp]
switch {
default:
if _, has := mvSet[next]; !has {
ctx.makeSet(next, mvSet)
}
case next == nil:
case next.Path == next.Local:
case next.Status.Location != LocationExternal:
}
}
}
}