-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.go
242 lines (220 loc) · 5.29 KB
/
package.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
package godeep
import (
"encoding/json"
"github.com/fatih/color"
"go/ast"
"golang.org/x/tools/go/packages"
"os"
"path/filepath"
"strings"
"sync"
)
/*
Creation Time: 2020 - Jan - 26
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2018
*/
//go:generate go get -u github.com/valyala/quicktemplate/qtc
//go:generate qtc -dir=.
func FindPackages(allPackages *Packages, rootPath string, onDone func(path string)) error {
allPackages.Reset()
waitGroup := sync.WaitGroup{}
rateLimit := make(chan struct{}, 50)
err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
waitGroup.Add(1)
rateLimit <- struct{}{}
go func(path string, info os.FileInfo, err error) {
defer waitGroup.Done()
defer func() {
<-rateLimit
}()
relPath, _ := filepath.Rel(rootPath, path)
if !info.IsDir() ||
strings.HasPrefix(relPath, ".") ||
strings.Contains(relPath, "vendor/",
) {
return
}
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedImports | packages.NeedDeps | packages.NeedTypes |
packages.NeedName | packages.NeedSyntax,
Dir: path,
}, "")
if err != nil {
return
}
for _, pkg := range pkgs {
if len(pkg.Errors) > 0 || !strings.Contains(pkg.PkgPath, ".") {
continue
}
allPackages.Fill(pkg)
if onDone != nil {
onDone(path)
}
}
return
}(path, info, err)
return nil
})
waitGroup.Wait()
for path, pkg := range allPackages.byPath {
for iPath := range allPackages.importedBy[path] {
pkg.importedByPackages = append(pkg.importedByPackages, iPath)
}
}
return err
}
type Packages struct {
byPath map[string]*Package
importedBy map[string]map[string]struct{}
mtx sync.RWMutex
}
func InitPackages() *Packages {
return &Packages{
byPath: make(map[string]*Package),
importedBy: make(map[string]map[string]struct{}),
mtx: sync.RWMutex{},
}
}
func (a *Packages) Reset() {
a.mtx.Lock()
for k := range a.byPath {
delete(a.byPath, k)
}
a.mtx.Unlock()
}
func (a *Packages) Unmarshal(data []byte) error {
return json.Unmarshal(data, &a.byPath)
}
func (a *Packages) Marshal() []byte {
d := jsonPackages{}
for _, p := range a.byPath {
d.Packages = append(d.Packages, jsonPackage{
Name: p.name,
Path: p.path,
Imported: p.imported,
ImportedBy: p.importedByPackages,
Funcs: p.exportedFunctions,
Types: p.exportedTypes,
})
}
// d, _ := json.Marshal(a.byPath)
return []byte(d.JSON())
}
func (a *Packages) Exist(pkg *packages.Package) bool {
a.mtx.RLock()
p := a.byPath[pkg.PkgPath]
a.mtx.RUnlock()
return p != nil
}
func (a *Packages) Insert(pkg *Package) {
a.mtx.Lock()
a.byPath[pkg.path] = pkg
a.mtx.Unlock()
}
func (a *Packages) GetByPath(pkgPath string) *Package {
a.mtx.RLock()
p := a.byPath[pkgPath]
a.mtx.RUnlock()
return p
}
func (a *Packages) Fill(pkg *packages.Package) {
if a.Exist(pkg) {
return
}
p := &Package{
name: pkg.Name,
path: pkg.PkgPath,
}
a.byPath[pkg.PkgPath] = p
for _, ipkg := range pkg.Imports {
p.imported = append(p.imported, ipkg.PkgPath)
if a.importedBy[ipkg.PkgPath] == nil {
a.importedBy[ipkg.PkgPath] = map[string]struct{}{}
}
a.importedBy[ipkg.PkgPath][pkg.PkgPath] = struct{}{}
}
for _, f := range pkg.Syntax {
for _, o := range f.Scope.Objects {
switch x := o.Decl.(type) {
case *ast.TypeSpec:
if x.Name.IsExported() {
p.exportedTypes = append(p.exportedTypes, x.Name.Name)
}
case *ast.FuncDecl:
if x.Name.IsExported() {
fn := strings.Builder{}
fn.WriteString(x.Name.Name)
fn.WriteString(astFuncType(x.Type))
p.exportedFunctions = append(p.exportedFunctions, fn.String())
}
case *ast.ValueSpec:
for _, n := range x.Names {
if n.IsExported() {
p.exportedVariables = append(p.exportedVariables, n.Name)
}
}
default:
}
}
}
}
func (a *Packages) ForEach(f func(pkgPath string, pkg *Package)) {
a.mtx.RLock()
for key, pkg := range a.byPath {
f(key, pkg)
}
a.mtx.RUnlock()
}
type Package struct {
mtx sync.Mutex
name string
path string
imported []string
importedByPackages []string
exportedTypes []string
exportedVariables []string
exportedFunctions []string
}
func (p *Package) Print() {
color.Green("========== %s (%s) ========", p.name, p.path)
printPackage(p)
printExportedItems(p)
}
func printPackage(pkg *Package) {
color.Red("Imports: (%d)", len(pkg.imported))
cnt := 0
for p := range pkg.imported {
cnt++
color.Red("\t %d. %s", cnt, p)
}
color.HiBlue("imported By: (%d)", len(pkg.importedByPackages))
cnt = 0
for p := range pkg.importedByPackages {
cnt++
color.HiBlue("\t %d. %s", cnt, p)
}
}
func printExportedItems(pkg *Package) {
color.HiMagenta("Exported Functions: (%d)", len(pkg.exportedFunctions))
cnt := 0
for p := range pkg.exportedFunctions {
cnt++
color.HiMagenta("\t %d. %s", cnt, p)
}
color.HiGreen("Exported Types: (%d)", len(pkg.exportedTypes))
cnt = 0
for p := range pkg.exportedTypes {
cnt++
color.HiGreen("\t %d. %s", cnt, p)
}
color.HiRed("Exported Variables: (%d)", len(pkg.exportedVariables))
cnt = 0
for p := range pkg.exportedVariables {
cnt++
color.HiRed("\t %d. %s", cnt, p)
}
}