forked from encoredev/encore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirs.go
148 lines (131 loc) · 3.92 KB
/
dirs.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
package parser
import (
"fmt"
"go/ast"
"go/build"
goparser "go/parser"
"go/scanner"
"go/token"
"io/fs"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"encr.dev/parser/est"
"encr.dev/pkg/watcher"
)
// walkFunc is the callback called by walkDirs to process a directory.
// dir is the path to the current directory; relPath is the (slash-separated)
// relative path from the original root dir.
type walkFunc func(dir, relPath string, files []fs.DirEntry) error
// walkDirs is like filepath.Walk but it calls walkFn once for each directory and not for individual files.
// It also reports both the full path and the path relative to the given root dir.
// It does not allow skipping directories in any way; any error returned from walkFn aborts the walk.
func walkDirs(root string, walkFn walkFunc) error {
return walkDir(root, ".", walkFn)
}
// walkDir processes a single directory and recurses.
// dir is the current directory path, and rel is the relative path from the original root.
// rel is always in slash form, while dir uses the OS-native filepath separator.
func walkDir(dir, rel string, walkFn walkFunc) error {
if watcher.IgnoreFolder(dir) {
return nil
}
entries, err := os.ReadDir(dir)
if err != nil {
return err
}
// Split the files and dirs
files := make([]fs.DirEntry, 0, len(entries))
var dirs []fs.DirEntry
for _, entry := range entries {
if entry.IsDir() {
dirs = append(dirs, entry)
} else {
files = append(files, entry)
}
}
if err := walkFn(dir, rel, files); err != nil {
return err
}
for _, d := range dirs {
dir2 := filepath.Join(dir, d.Name())
rel2 := path.Join(rel, d.Name())
if err := walkDir(dir2, rel2, walkFn); err != nil {
return err
}
}
return nil
}
// parseDir is like go/parser.ParseDir but it constructs *est.File objects instead.
func parseDir(buildContext build.Context, fset *token.FileSet, dir string, list []fs.DirEntry, filter func(entry fs.DirEntry) bool, mode goparser.Mode) (pkgs map[string]*ast.Package, files []*est.File, err error) {
// Sort the slice so that we have a stable order to ensure deterministic metadata.
sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
var errors scanner.ErrorList
pkgs = make(map[string]*ast.Package)
for _, d := range list {
if strings.HasSuffix(d.Name(), ".go") && (filter == nil || filter(d)) {
filename := filepath.Join(dir, d.Name())
contents, err := ioutil.ReadFile(filename)
if err != nil {
return nil, nil, err
}
// Check if this file should be part of the build
matched, err := buildContext.MatchFile(dir, d.Name())
if err != nil {
errors.Add(token.Position{Filename: filename}, err.Error())
continue
}
if !matched {
continue
}
src, err := goparser.ParseFile(fset, filename, contents, mode)
if err != nil || !src.Pos().IsValid() {
// Parse error or invalid file
if err == nil {
err = fmt.Errorf("could not parse file %s", d.Name())
}
if el, ok := err.(scanner.ErrorList); ok {
errors = append(errors, el...)
} else {
errors.Add(token.Position{Filename: filename}, err.Error())
}
continue
}
name := src.Name.Name
pkg, found := pkgs[name]
if !found {
pkg = &ast.Package{
Name: name,
Files: make(map[string]*ast.File),
}
pkgs[name] = pkg
}
pkg.Files[filename] = src
tokFile := fset.File(src.Package)
files = append(files, &est.File{
Name: d.Name(),
AST: src,
Token: tokFile,
Imports: getFileImports(src),
Contents: contents,
Path: filename,
References: make(map[ast.Node]*est.Node),
Pkg: nil, // will be set later
})
}
}
return pkgs, files, errors.Err()
}
func getFileImports(f *ast.File) map[string]bool {
imports := make(map[string]bool)
for _, s := range f.Imports {
if path, err := strconv.Unquote(s.Path.Value); err == nil {
imports[path] = true
}
}
return imports
}