-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_tracker.go
92 lines (81 loc) · 2.39 KB
/
import_tracker.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
package meta
import (
"fmt"
"go/token"
"sort"
"strings"
)
type ImportTracker interface {
Import(pkgPath string) string
LocalNameOf(packagePath string) string
PathOf(localName string) (string, bool)
ImportLines() []string
}
type DefaultImportTracker struct {
pkgPathToName map[string]string
nameToPkgPath map[string]string
localPkgPath string
}
func NewDefaultImportTracker(localPkgPath string) *DefaultImportTracker {
return &DefaultImportTracker{
pkgPathToName: map[string]string{},
nameToPkgPath: map[string]string{},
localPkgPath: localPkgPath,
}
}
func (tracker *DefaultImportTracker) Import(pkgPath string) string {
if tracker.localPkgPath == pkgPath {
return ""
}
name, ok := tracker.pkgPathToName[pkgPath]
if ok {
return name
}
name = PkgNameOrAlias(tracker, pkgPath)
tracker.pkgPathToName[pkgPath] = name
tracker.nameToPkgPath[name] = pkgPath
return name
}
func (tracker *DefaultImportTracker) LocalNameOf(pkgPath string) string {
return tracker.pkgPathToName[pkgPath]
}
func (tracker *DefaultImportTracker) PathOf(localName string) (string, bool) {
name, ok := tracker.nameToPkgPath[localName]
return name, ok
}
func (tracker *DefaultImportTracker) ImportLines() []string {
var importPaths []string
for name, pkgPath := range tracker.nameToPkgPath {
var importPath string
if name == pkgPath || strings.HasSuffix(pkgPath, "/"+name) {
importPath = fmt.Sprintf("\"%s\"", pkgPath)
} else {
importPath = fmt.Sprintf("%s \"%s\"", name, pkgPath)
}
importPaths = append(importPaths, importPath)
}
sort.Sort(sort.StringSlice(importPaths))
return importPaths
}
func PkgNameOrAlias(tracker ImportTracker, pkgPath string) string {
dirs := strings.Split(pkgPath, "/")
for n := len(dirs) - 1; n >= 0; n-- {
// follow kube convention of not having anything between directory names
name := strings.Join(dirs[n:], "")
name = strings.Replace(name, "_", "", -1)
// These characters commonly appear in import paths for go
// packages, but aren't legal go names. So we'll sanitize.
name = strings.Replace(name, ".", "", -1)
name = strings.Replace(name, "-", "", -1)
if _, found := tracker.PathOf(name); found {
// This name collides with some other package
continue
}
// If the import name is a Go keyword, prefix with an underscore.
if token.Lookup(name).IsKeyword() {
name = "_" + name
}
return name
}
panic("can't find import for " + pkgPath)
}