/
helpers.go
109 lines (94 loc) · 2.41 KB
/
helpers.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
package parser
import (
"fmt"
"github.com/thoas/go-funk"
"go/ast"
"golang.org/x/tools/go/packages"
"path"
"strings"
)
type PackageFiles struct {
PackagePath string
PackageName string
AstFiles []*ast.File
}
func filterFile(filepath string) bool {
if !strings.HasSuffix(filepath, goFileSuffix) ||
strings.HasSuffix(filepath, GenerateFileSuffix) || strings.HasSuffix(filepath, testFileSuffix) {
return false
}
return true
}
func getDependenciesFilenames(dir string) ([]string, error) {
goFiles := []string{}
pkgs, err := loadPackage(dir)
if err != nil {
return nil, err
}
for _, pack := range pkgs {
goFiles = append(goFiles, goFilesFromPackage(pack)...)
for _, childPack := range pack.Imports {
goFiles = append(goFiles, goFilesFromPackage(childPack)...)
}
}
return funk.UniqString(goFiles), nil
}
func GetDependenciesAstFiles(filename string) ([]PackageFiles, error) {
pkgs, err := loadPackageWithSyntax(path.Dir(filename))
if err != nil {
return nil, err
}
pfs := []PackageFiles{}
done := map[string]bool{}
for _, pkg := range pkgs {
if _, ok := done[pkg.PkgPath]; ok {
continue
}
pfs = append(pfs, PackageFiles{
PackagePath: pkg.PkgPath,
PackageName: pkg.Name,
AstFiles: pkg.Syntax,
})
done[pkg.PkgPath] = true
for _, childPack := range pkg.Imports {
if _, ok := done[childPack.PkgPath]; ok {
continue
}
pfs = append(pfs, PackageFiles{
PackagePath: childPack.PkgPath,
PackageName: childPack.Name,
AstFiles: childPack.Syntax,
})
done[childPack.PkgPath] = true
}
}
return pfs, nil
}
func goFilesFromPackage(pkg *packages.Package) []string {
files := []string{}
files = append(files, pkg.GoFiles...)
return funk.FilterString(files, filterFile)
}
func EntryPointPackageName(filename string) (string, string, error) {
pkgs, err := loadPackage(path.Dir(filename))
if err != nil {
return "", "", err
}
for _, pack := range pkgs {
return pack.Name, pack.PkgPath, nil
}
return "", "", fmt.Errorf("package not found for entry point")
}
func loadPackage(path string) ([]*packages.Package, error) {
return packages.Load(&packages.Config{
Mode: packages.NeedImports | packages.NeedFiles | packages.NeedName,
}, path)
}
func loadPackageWithSyntax(path string) ([]*packages.Package, error) {
return packages.Load(&packages.Config{
Mode: packages.NeedImports |
packages.NeedFiles |
packages.NeedName |
packages.NeedSyntax,
}, path)
}