-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefactor.go
176 lines (170 loc) · 4.42 KB
/
refactor.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
package refactor
import (
_ "golang.org/x/tools/go/gcimporter"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/types"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/printer"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
)
func Rename(importPath, recv, name, newName string) error {
paths, err := importers(importPath)
if err != nil {
return err
}
config := loader.Config{
ParserMode: parser.ParseComments,
TypeCheckFuncBodies: func(path string) bool { return paths[path] },
AllowErrors: true,
}
for p := range paths {
config.Import(p)
}
prog, err := config.Load()
if err != nil {
return err
}
pkg := prog.Imported[importPath]
for _, err := range pkg.Errors {
fmt.Println(err)
}
obj := pkg.Pkg.Scope().Lookup(name)
if recv != "" {
tn, ok := pkg.Pkg.Scope().Lookup(recv).(*types.TypeName)
if !ok {
return fmt.Errorf("type %s.%s not found", importPath, recv)
}
t := tn.Type().(*types.Named)
for i := 0; i < t.NumMethods(); i++ {
m := t.Method(i)
if m.Name() == name {
obj = m
break
}
}
if t, ok := t.Underlying().(*types.Struct); ok && obj == nil {
for i := 0; i < t.NumFields(); i++ {
f := t.Field(i)
if f.Name() == name {
obj = f
break
}
}
}
if obj == nil {
return fmt.Errorf("field or method %s.%s.%s not found", importPath, recv, name)
}
}
if obj == nil {
return fmt.Errorf("object %s.%s not found", importPath, name)
}
for path := range paths {
pkg := prog.Imported[path]
for _, f := range pkg.Files {
modified := false
ast.Inspect(f, func(node ast.Node) bool {
if id, ok := node.(*ast.Ident); ok && pkg.ObjectOf(id) == obj {
id.Name = newName
modified = true
}
return true
})
if !modified {
continue
}
file, err := os.Create(prog.Fset.File(f.Package).Name())
if err != nil {
return err
}
printer.Fprint(file, prog.Fset, f)
file.Close()
}
}
return nil
}
func importers(importPath string) (map[string]bool, error) {
paths := map[string]bool{importPath: true}
for _, srcDir := range build.Default.SrcDirs() {
if err := filepath.Walk(srcDir, func(path string, f os.FileInfo, err error) error {
p, err := build.ImportDir(path, 0)
if err != nil {
return nil
}
i := sort.SearchStrings(p.Imports, importPath)
if i < len(p.Imports) && p.Imports[i] == importPath {
paths[p.ImportPath] = true
}
return nil
}); err != nil {
return nil, err
}
}
return paths, nil
}
// MovePackage moves the package from the old import path to the new one. Any
// packages in subdirectories are also moved. All uses of the old import path
// in the Go environment are updated to the new import path.
//
func MovePackage(old, new string) error {
p, err := build.Import(old, "", build.FindOnly)
if err != nil {
return err
}
oldFullPath := p.Dir
if p, err := build.Import(new, "", build.FindOnly); err == nil {
return fmt.Errorf("package %s already exists at %s", new, p.Dir)
}
newFullPath := filepath.Join(p.SrcRoot, new)
for _, srcDir := range build.Default.SrcDirs() {
if err := filepath.Walk(srcDir, func(path string, f os.FileInfo, err error) error {
if c := filepath.Base(path)[0]; c == '_' || c == '.' {
if f.IsDir() {
return filepath.SkipDir
}
return nil
}
if f.Mode().IsRegular() && filepath.Ext(path) == ".go" {
astFile, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ImportsOnly)
if err != nil {
return nil
}
// TODO: correctly rewrite multiple imports. should read the file once outside the loop
for _, imp := range astFile.Imports {
importPath := imp.Path.Value[1 : len(imp.Path.Value)-1]
if importPath == old || strings.HasPrefix(importPath, old+"/") {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil
}
i := int(imp.Path.Pos())
s := string(b[:i]) + new + string(b[i+len(old):])
ioutil.WriteFile(path, []byte(s), f.Mode())
}
}
}
return nil
}); err != nil {
return err
}
}
return os.Rename(oldFullPath, newFullPath)
}
func ReportShadowedPackages() {
for _, srcDir := range build.Default.SrcDirs() {
filepath.Walk(srcDir, func(path string, f os.FileInfo, err error) error {
if p, err := build.ImportDir(path, build.AllowBinary); err == nil && p.ConflictDir != "" {
fmt.Println("WARNING: package at " + p.Dir)
fmt.Println(" is shadowed by " + p.ConflictDir)
}
return nil
})
}
}