/
rewriter.go
executable file
·207 lines (173 loc) · 3.9 KB
/
rewriter.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
package rewrite
import (
"bytes"
"fmt"
"go/ast"
"go/token"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"github.com/0x00b/gobbq/cmd/bbq/proto/com/gorewriter/code"
"golang.org/x/tools/go/packages"
)
// Rewriter TODO
type Rewriter struct {
pkg *packages.Package
files map[string]string
copied map[ast.Decl]bool
}
// New TODO
func New(dir string) (*Rewriter, error) {
importPath := code.ImportPathForDir(dir)
if importPath == "" {
return nil, fmt.Errorf("import path not found for directory: %q", dir)
}
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedSyntax | packages.NeedTypes,
}, importPath)
if err != nil {
return nil, err
}
if len(pkgs) == 0 {
return nil, fmt.Errorf("package not found for importPath: %s", importPath)
}
return &Rewriter{
pkg: pkgs[0],
files: map[string]string{},
copied: map[ast.Decl]bool{},
}, nil
}
func (r *Rewriter) getSource(start, end token.Pos) string {
startPos := r.pkg.Fset.Position(start)
endPos := r.pkg.Fset.Position(end)
if startPos.Filename != endPos.Filename {
panic("cant get source spanning multiple files")
}
file := r.getFile(startPos.Filename)
return file[startPos.Offset:endPos.Offset]
}
func (r *Rewriter) getFile(filename string) string {
if _, ok := r.files[filename]; !ok {
b, err := ioutil.ReadFile(filename)
if err != nil {
panic(fmt.Errorf("unable to load file, already exists: %s", err.Error()))
}
r.files[filename] = string(b)
}
return r.files[filename]
}
// GetMethodBody TODO
func (r *Rewriter) GetMethodBody(structname string, methodname string) string {
for _, f := range r.pkg.Syntax {
for _, d := range f.Decls {
d, isFunc := d.(*ast.FuncDecl)
if !isFunc {
continue
}
if d.Name.Name != methodname {
continue
}
if structname == "" && (d.Recv == nil || len(d.Recv.List) == 0) {
return r.getSource(d.Body.Pos()+1, d.Body.End()-1)
}
if d.Recv == nil || len(d.Recv.List) == 0 {
continue
}
recv := d.Recv.List[0].Type
if star, isStar := recv.(*ast.StarExpr); isStar {
recv = star.X
}
ident, ok := recv.(*ast.Ident)
if !ok {
continue
}
if ident.Name != structname {
continue
}
r.copied[d] = true
return r.getSource(d.Body.Pos()+1, d.Body.End()-1)
}
}
return ""
}
// MarkStructCopied TODO
func (r *Rewriter) MarkStructCopied(name string) {
for _, f := range r.pkg.Syntax {
for _, d := range f.Decls {
d, isGen := d.(*ast.GenDecl)
if !isGen {
continue
}
if d.Tok != token.TYPE || len(d.Specs) == 0 {
continue
}
spec, isTypeSpec := d.Specs[0].(*ast.TypeSpec)
if !isTypeSpec {
continue
}
if spec.Name.Name != name {
continue
}
r.copied[d] = true
}
}
}
// ExistingImports TODO
func (r *Rewriter) ExistingImports(filename string) []Import {
filename, err := filepath.Abs(filename)
if err != nil {
panic(err)
}
for _, f := range r.pkg.Syntax {
pos := r.pkg.Fset.Position(f.Pos())
if filename != pos.Filename {
continue
}
var imps []Import
for _, i := range f.Imports {
name := ""
if i.Name != nil {
name = i.Name.Name
}
path, err := strconv.Unquote(i.Path.Value)
if err != nil {
panic(err)
}
imps = append(imps, Import{name, path})
}
return imps
}
return nil
}
// RemainingSource TODO
func (r *Rewriter) RemainingSource(filename string) string {
filename, err := filepath.Abs(filename)
if err != nil {
panic(err)
}
for _, f := range r.pkg.Syntax {
pos := r.pkg.Fset.Position(f.Pos())
if filename != pos.Filename {
continue
}
var buf bytes.Buffer
for _, d := range f.Decls {
if r.copied[d] {
continue
}
if d, isGen := d.(*ast.GenDecl); isGen && d.Tok == token.IMPORT {
continue
}
buf.WriteString(r.getSource(d.Pos(), d.End()))
buf.WriteString("\n")
}
return strings.TrimSpace(buf.String())
}
return ""
}
// Import TODO
type Import struct {
Alias string
ImportPath string
}