-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
246 lines (197 loc) · 4.71 KB
/
manager.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package depmanager
import (
"bufio"
"crypto/sha1"
"io"
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
)
var workDir string
// GlobalDirMap is a map of all possible directory paths in the entire project scope
var GlobalDirMap map[string]bool = make(map[string]bool)
// GlobalPkgMap is the map of all imported packages in the project files
// to the dir file paths
var GlobalPkgMap map[string]string = make(map[string]string)
// GlobalFileHashMap is a map of files and the hashes of their imports.
// It is used to determine when to rebuild the dependency array(i.e when
// the imports change)
var GlobalFileHashMap map[string][]byte = make(map[string][]byte)
// BuildGlobalDirMap is a function that constructs a map that contains all
// the files and directories in the currDir argument passed to it
func BuildGlobalDirMap(currDir string) error {
err := filepath.WalkDir(currDir, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
GlobalDirMap[path] = true
}
return nil
})
if err != nil {
return err
}
return nil
}
// BuildDeps takes a file and returns a list of the dependencies of the
// file along with their dependencies
func BuildDeps(dir string) {
mainFile := dir + "/main.go"
workDir = dir
err := buildDepPackages(mainFile)
if err != nil {
panic(err)
}
}
func buildDepPackages(workFile string) error {
dirCtx := workDir
tempDepArr := []string{}
file, err := os.Open(workFile)
if err != nil {
panic(err)
}
defer file.Close()
fileDeps, err := extractImports(file)
if err != nil {
return err
}
GlobalFileHashMap[workFile] = hashFileDeps(fileDeps)
for _, dep := range fileDeps {
if !isValidDep(dep, dirCtx) {
continue
}
depPath := findPath(dep, dirCtx)
_, ok := GlobalPkgMap[dep]
if !ok && depPath != "" {
GlobalPkgMap[dep] = depPath
tempDepArr = append(tempDepArr, depPath)
} else {
continue
}
}
for _, pkg := range tempDepArr {
files, err := FetchFiles(pkg)
if err != nil {
return err
}
for _, file := range files {
buildDepPackages(file)
}
}
return nil
}
func findPath(dep, dirCtx string) string {
cmd := exec.Command("go", "list", "-m")
cmd.Dir = dirCtx
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
dep = strings.ReplaceAll(dep, strings.TrimSpace(string(out)), "")
dep = strings.TrimSpace(dep)
for k := range GlobalDirMap {
if strings.HasSuffix(k, dep) {
return k
}
}
return ""
}
// FetchFiles is a function that scans a directory and returns an array of the path of all
// the files in the dir
func FetchFiles(pkg string) ([]string, error) {
files := []string{}
err := filepath.WalkDir(pkg, func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
return files, err
}
return files, nil
}
func hashFileDeps(fileDeps []string) []byte {
hasher := sha1.New()
for _, fileDep := range fileDeps {
io.WriteString(hasher, fileDep)
}
return hasher.Sum(nil)
}
func isValidDep(dep, dirCtx string) bool {
dep = strings.TrimSpace(dep)
cmd := exec.Command("go", "list", "-m")
cmd.Dir = dirCtx
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
modName := strings.TrimSpace(string(out))
return strings.HasPrefix(dep, modName)
}
/*
* another approach to validation that i might take in future
func isValidDep() {
// construct recursive map of folders in main dir
// match dep with the map
}
*/
func extractImports(file *os.File) ([]string, error) {
imports := []string{}
fileReader := bufio.NewReader(file)
for {
line, err := fileReader.ReadBytes('\n')
if err != nil && err != io.EOF {
return imports, err
}
if err == io.EOF {
break
}
trimmedLine := strings.TrimSpace(string(line))
if strings.HasPrefix(trimmedLine, "package") {
continue
}
if strings.HasPrefix(trimmedLine, "import") {
if strings.HasSuffix(trimmedLine, "\"") {
imports = append(imports, removeQuote(trimmedLine))
break
} else if strings.HasSuffix(trimmedLine, "(") {
for strings.TrimSpace(string(line)) != ")" {
line, err = fileReader.ReadBytes('\n')
if err != nil && err != io.EOF {
return imports, err
}
pkgImport := removeQuote(strings.TrimSpace(string(line)))
if pkgImport != "" {
imports = append(imports, pkgImport)
}
}
break
}
}
}
sort.Strings(imports)
return imports, nil
}
func removeQuote(line string) string {
chars := []rune(line)
quote := []rune{}
inQuote := false
for i := 0; i < len(chars); i++ {
if chars[i] == '"' {
inQuote = !inQuote
continue
}
if inQuote {
quote = append(quote, chars[i])
} else {
continue
}
}
return string(quote)
}
func EmptyPkgMap() {
GlobalPkgMap = make(map[string]string)
}