-
Notifications
You must be signed in to change notification settings - Fork 2
/
sync.go
144 lines (117 loc) · 2.82 KB
/
sync.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
package goast
import (
"encoding/json"
"go/ast"
"go/parser"
"go/token"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/m-mizutani/goerr"
)
func (x *Goast) Sync(src string) error {
dump := func(dst string, data *Node) error {
dir := filepath.Dir(dst)
if err := x.mkdir(dir, 0755); err != nil {
return goerr.Wrap(err, "failed to create dump dir").With("dir", dir)
}
fd, err := x.create(dst)
if err != nil {
return goerr.Wrap(err, "failed to create dump file").With("path", dst)
}
defer func() {
if err := fd.Close(); err != nil {
logger.With("path", dst).Warn(err.Error())
}
}()
encoder := json.NewEncoder(fd)
if !x.dumpCompact {
encoder.SetIndent("", " ")
}
if err := encoder.Encode(data); err != nil {
return goerr.Wrap(err, "failed to encode goast.Node")
}
return nil
}
return x.walk(src, func(fpath string, r io.Reader) error {
fSet := token.NewFileSet()
f, err := parser.ParseFile(fSet, fpath, r, parser.ParseComments)
if err != nil {
return goerr.Wrap(err)
}
comments := map[int]string{}
sync := func(data *Node) error {
if file, ok := data.Node.(*ast.File); ok {
comments = toCommentMap(file, fSet)
// If goast.sync comment exists at head of file, dump *ast.File
if dst, ok := comments[1]; ok {
if err := dump(dst, data); err != nil {
return err
}
}
delete(comments, 1)
return nil
}
pos := fSet.Position(data.Node.Pos())
if dst, ok := comments[pos.Line]; ok {
if err := dump(dst, data); err != nil {
return err
}
delete(comments, pos.Line)
}
return nil
}
if err := Inspect(f, fSet, sync, x.inspectOpt...); err != nil {
return err
}
return nil
})
}
func toCommentMap(f *ast.File, fSet *token.FileSet) map[int]string {
const commentPrefix = "goast.sync:"
comments := map[int]string{}
for _, c := range f.Comments {
pos := fSet.Position(c.Pos())
for _, l := range c.List {
idx := strings.Index(l.Text, commentPrefix)
if idx < 0 {
continue
}
comments[pos.Line] = strings.TrimSpace(l.Text[idx+len(commentPrefix):])
}
}
return comments
}
func walkGoCode(src string, cb func(fpath string, r io.Reader) error) error {
if err := filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return goerr.Wrap(err)
}
if d.IsDir() {
return nil
}
fpath := filepath.Clean(path)
if filepath.Ext(fpath) != ".go" {
return nil
}
logger.With("file", fpath).Debug("loading file")
fd, err := os.Open(fpath)
if err != nil {
return goerr.Wrap(err)
}
defer func() {
if err := fd.Close(); err != nil {
logger.Err(err).With("file", fpath).Warn("failed to close file")
}
}()
if err := cb(fpath, fd); err != nil {
return err
}
return nil
}); err != nil {
return goerr.Wrap(err)
}
return nil
}