-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdep.go
106 lines (83 loc) · 2.03 KB
/
dep.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
package main
import (
"go/build"
"go/parser"
"go/token"
"os"
"path/filepath"
)
type pkg struct {
Context *build.Context
Name string
Dir string
}
func newPackage(context *build.Context, name, dir string) *pkg {
return &pkg{context, name, dir}
}
// Import loads and return the build packagee
func (p *pkg) Import() (*build.Package, error) {
if p.Name != "" {
return p.Context.Import(p.Name, p.Dir, build.AllowBinary)
}
return p.Context.ImportDir(p.Dir, build.AllowBinary)
}
// computeDep returns the list of the target's dependency files
func computeDep(context *build.Context, target string) ([]string, error) {
var (
queue []*pkg
files []string
)
info, err := os.Stat(target)
if err != nil {
return files, err
}
visited := make(map[string]bool)
if info.IsDir() {
queue = append(queue, newPackage(context, "", target))
} else {
f, err := parser.ParseFile(token.NewFileSet(), target, nil, parser.ImportsOnly)
if err != nil {
return files, err
}
d := newPackage(context, "", filepath.Dir(target))
if f.Name != nil {
if n := f.Name.String(); n != "main" {
d.Name = n
visited[d.Name] = true
}
}
queue = append(queue, d)
}
var current *pkg
for len(queue) > 0 {
current, queue = queue[0], queue[1:]
// Ignore import errors. They should be caught at build time
p, _ := current.Import()
if !p.Goroot {
if len(p.PkgObj) > 0 && fileExists(p.PkgObj) {
files = append(files, p.PkgObj)
}
for _, i := range p.Imports {
if !visited[i] {
visited[i] = true
queue = append(queue, newPackage(context, i, ""))
}
}
f := addPrefix(p.Dir, p.CFiles, p.CgoFiles, p.GoFiles, p.HFiles, p.SFiles, p.SysoFiles)
files = append(files, f...)
}
}
return files, nil
}
// addPrefix adds prefix at beginning of each name in the list
func addPrefix(prefix string, names ...[]string) []string {
var files []string
for _, name := range names {
for _, n := range name {
if len(n) > 0 {
files = append(files, filepath.Join(prefix, n))
}
}
}
return files
}