-
Notifications
You must be signed in to change notification settings - Fork 1
/
modules.go
204 lines (173 loc) · 5.69 KB
/
modules.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
package modules
import (
"encoding/json"
"fmt"
"github.com/alexisvisco/gwd/pkg/utils"
"io/fs"
"path/filepath"
"strings"
"github.com/alexisvisco/gwd/pkg/diff/packages"
"github.com/alexisvisco/gwd/pkg/output"
"github.com/alexisvisco/gwd/pkg/parsing"
"github.com/alexisvisco/gwd/pkg/vars"
"github.com/samber/lo"
)
type Modules struct {
Modules []*Changes `json:"modules"`
}
type Changes struct {
ModuleName string `json:"module_name"`
ModulePath string `json:"module_path"`
PackagesModified packages.Modified `json:"packages_modified"`
Reason ModuleChangeReason `json:"reason"`
ModuleDependencyImportPath packages.ImportPath `json:"module_dependency_import_path,omitempty"`
}
type cache struct {
modulePathToChanges map[string]*Changes
modifiedImportPath map[packages.ImportPath]packages.Modified
}
func FromFilesChanged(fileChanged []string) (*Modules, error) {
modules := &Modules{Modules: make([]*Changes, 0, len(vars.ModuleNameToModulePath))}
cache := &cache{
modulePathToChanges: make(map[string]*Changes),
modifiedImportPath: make(map[packages.ImportPath]packages.Modified),
}
err := detectImportPathThatHaveChanged(fileChanged, modules, cache)
if err != nil {
return nil, err
}
if err := detectImportPathImported(modules, cache); err != nil {
return nil, err
}
return modules, nil
}
type ModuleChangeReason string
const (
ModuleChangeReasonFile = ModuleChangeReason("ModuleChangeReasonFile")
ModuleChangeReasonModuleDependency = ModuleChangeReason("ModuleChangeReasonModuleDependency")
)
func detectImportPathThatHaveChanged(filePathChange []string, modules *Modules, c *cache) error {
for _, filePath := range filePathChange {
moduleName, modulePath := getModuleFromFilePath(filePath)
if moduleName == "" {
// the filePathChange are not in a go module, so we need to ignore it
continue
}
changes, ok := c.modulePathToChanges[modulePath]
if !ok {
changes = &Changes{
ModuleName: moduleName,
ModulePath: modulePath,
PackagesModified: packages.NewChanges(),
Reason: ModuleChangeReasonFile,
}
c.modulePathToChanges[modulePath] = changes
}
packageName, err := packages.GetImportPathFromPath(moduleName, modulePath, filePath, utils.PathIsDir(filePath))
if err != nil {
return err
}
c.modifiedImportPath[packageName] = changes.PackagesModified
changes.PackagesModified.AddModifiedPackage(packageName, filePath)
}
for _, change := range c.modulePathToChanges {
modules.Modules = append(modules.Modules, change)
}
return nil
}
// detectImportPathImported loop over all modules and parse all go files to check whether they import a package
// that have been modified.
func detectImportPathImported(modules *Modules, cache *cache) error {
for modulePathWhichCanImportModifiedPackage, moduleNameWhichCanImportModifiedPackage := range vars.ModulePathToModuleName {
err := filepath.WalkDir(modulePathWhichCanImportModifiedPackage, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() || !strings.HasSuffix(info.Name(), ".go") {
return nil
}
importPathWhichImportModifiedPackage, err := packages.GetImportPathFromPath(
moduleNameWhichCanImportModifiedPackage,
modulePathWhichCanImportModifiedPackage,
path,
false,
)
if err != nil {
return err
}
for _, importPathWhichIsModifiedPackage := range parsing.GetImports(path) {
packageChange, ok := cache.modifiedImportPath[importPathWhichIsModifiedPackage]
if !ok {
continue
}
packageChange.AddImportPathWhichImportModifiedPackage(
moduleNameWhichCanImportModifiedPackage,
modulePathWhichCanImportModifiedPackage,
importPathWhichIsModifiedPackage,
importPathWhichImportModifiedPackage,
)
changes, ok := cache.modulePathToChanges[modulePathWhichCanImportModifiedPackage]
if !ok {
changes = &Changes{
ModuleName: moduleNameWhichCanImportModifiedPackage,
ModulePath: modulePathWhichCanImportModifiedPackage,
PackagesModified: packages.NewChanges(),
Reason: ModuleChangeReasonModuleDependency,
ModuleDependencyImportPath: importPathWhichIsModifiedPackage,
}
cache.modulePathToChanges[modulePathWhichCanImportModifiedPackage] = changes
modules.Modules = append(modules.Modules, changes)
}
}
return nil
})
if err != nil {
return err
}
}
return nil
}
func getModuleFromFilePath(filePath string) (moduleName, modulePath string) {
for modulePath, moduleName = range vars.ModulePathToModuleName {
if strings.Contains(filePath, modulePath) {
return
}
}
return "", ""
}
func (m Modules) Human() {
if len(m.Modules) == 0 {
return
}
fmt.Println(strings.Join(lo.Map(m.Modules, func(t *Changes, i int) string {
return t.ModulePath
}), " "))
}
func (m Modules) HumanVerbose() {
for _, mod := range m.Modules {
fmt.Printf(
"%q%s\n", mod.ModuleName,
lo.Ternary(mod.Reason == ModuleChangeReasonModuleDependency,
fmt.Sprintf(" no files changed but the package imports %q", mod.ModuleDependencyImportPath),
":"))
for _, ch := range mod.PackagesModified {
for s := range ch.Files {
fmt.Printf(" - %q\n", s)
if len(ch.ImportedImportPath) > 0 {
fmt.Printf(" imported by:\n")
for importPath, detail := range ch.ImportedImportPath {
fmt.Printf(" ∟ %q (%d times) module %q\n", importPath, detail.Counter, detail.ModuleName)
}
}
}
}
}
}
func (m Modules) Json() {
marshal, err := json.Marshal(m)
if err != nil {
fmt.Println(output.JsonError)
return
}
fmt.Println(string(marshal))
}