/
walk.go
179 lines (139 loc) · 4.12 KB
/
walk.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
package fs
import (
"errors"
"fmt"
"go/parser"
"go/token"
"os"
"strings"
"github.com/ilya2049/gocomponent/internal/component"
"github.com/ilya2049/gocomponent/internal/pkg/fs"
)
type Walk struct {
fileReader fs.FileReader
filePathWalker filePathWalker
astFileParser astFileParser
projectDir string
project *project
}
func NewWalk(
projectDir string,
fileReader fs.FileReader,
filePathWalker filePathWalker,
astFileParser astFileParser,
) *Walk {
if !strings.HasSuffix(projectDir, component.Slash) {
projectDir += component.Slash
}
return &Walk{
projectDir: projectDir,
project: newProject(),
fileReader: fileReader,
filePathWalker: filePathWalker,
astFileParser: astFileParser,
}
}
func (w *Walk) ReadComponentGraph() (*component.Graph, error) {
moduleName, err := w.readModuleName(w.projectDir)
if err != nil {
return nil, err
}
err = w.filePathWalker.Walk(w.projectDir, func(path string, _ os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("walk err: %w", err)
}
if isHidden(path) {
return nil
}
if !isGoSourceFile(path) {
return nil
}
namespace, ok := findNamespaceInPath(path)
if !ok {
return nil
}
if w.isRootNamespace(namespace) {
namespace = w.createRootNamespace()
} else {
namespace = w.createRootBasedNamespace(namespace)
}
aComponent := w.project.getOrAddComponent(namespace)
packageImports, err := w.parseImportsAndCountBytesOfGoFile(aComponent, moduleName, path)
if err != nil {
return err
}
aPackage := newProjectPackage(aComponent, packageImports)
w.addPackageInProject(namespace, aPackage)
return nil
})
if err != nil {
return nil, err
}
return w.project.createComponentGraph(), nil
}
func (w *Walk) isRootNamespace(namespace component.Namespace) bool {
return namespace+component.Slash == component.Namespace(w.projectDir)
}
func (*Walk) createRootNamespace() component.Namespace {
return component.NewNamespace(component.Slash)
}
func (w *Walk) createRootBasedNamespace(namespace component.Namespace) component.Namespace {
return component.Slash + namespace.TrimPrefix(w.projectDir)
}
func (w *Walk) addPackageInProject(namespace component.Namespace, newPackage *projectPackage) {
existingPackage, ok := w.project.findPackage(namespace)
if ok {
existingPackage.join(newPackage)
return
}
w.project.addPackage(namespace, newPackage)
}
func (w *Walk) parseImportsAndCountBytesOfGoFile(
currentComponent *component.Component,
moduleName string,
goFileName string,
) (map[component.Namespace]*component.Component, error) {
file, err := w.astFileParser.ParseFile(token.NewFileSet(), goFileName, nil, parser.Mode(0))
if err != nil {
return nil, fmt.Errorf("parse a .go file: %w", err)
}
if !strings.HasSuffix(goFileName, "_test.go") {
currentComponent.AddBytesInSize(int(file.FileEnd) - int(file.FileStart))
}
imports := make(map[component.Namespace]*component.Component)
for _, fileImport := range file.Imports {
namespace := component.NewNamespace(fileImport.Path.Value[1 : len(fileImport.Path.Value)-1])
moduleNameWithSlash := moduleName + component.Slash
if namespace.HasPrefix(moduleNameWithSlash) {
namespace = namespace.TrimPrefix(moduleName)
}
if namespace == currentComponent.Namespace() {
continue
}
component := w.project.getOrAddComponent(namespace)
imports[namespace] = component
}
return imports, nil
}
var ErrFirstLineOfGoModShouldIncludeExactlyTwoPArts = errors.New(
"the first line of go mod file parts should includes exactly two parts",
)
func (w *Walk) readModuleName(projectDir string) (string, error) {
goModFileBytes, err := w.fileReader.ReadFile(projectDir + "go.mod")
if err != nil {
return "", fmt.Errorf("read go.mod: %w", err)
}
var firstLineOfGoModFile = []byte{}
for _, b := range goModFileBytes {
if b == '\n' {
break
} else {
firstLineOfGoModFile = append(firstLineOfGoModFile, b)
}
}
firstLineOfGoModFileParts := strings.Split(string(firstLineOfGoModFile), " ")
if len(firstLineOfGoModFileParts) != 2 {
return "", ErrFirstLineOfGoModShouldIncludeExactlyTwoPArts
}
return firstLineOfGoModFileParts[1], nil
}