-
Notifications
You must be signed in to change notification settings - Fork 16
/
stats.go
181 lines (158 loc) · 4.13 KB
/
stats.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
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"text/tabwriter"
)
type ProjectStats struct {
ImportStatsByPath map[string]*ImportStats
}
type ImportStats struct {
Path string
Remote bool
ReferencePositions []token.Position
}
type SummaryItem struct {
Origin int
Sum int
Path string
}
func (i SummaryItem) Legend() string {
var origin string
switch i.Origin {
case 1:
origin = "R"
case 0:
origin = "L"
case -1:
origin = "S"
}
return fmt.Sprintf("%s\t%s\t%d", origin, i.Path, i.Sum)
}
type Summary struct {
Items []SummaryItem
}
func (s *Summary) Append(i SummaryItem) { s.Items = append(s.Items, i) }
func (s *Summary) Get(i int) SummaryItem { return s.Items[i] }
func (s *Summary) Len() int { return len(s.Items) }
func (s *Summary) Swap(i, j int) { s.Items[i], s.Items[j] = s.Items[j], s.Items[i] }
func (s *Summary) Less(i, j int) bool {
i1 := s.Items[i]
i2 := s.Items[j]
return i1.Origin > i2.Origin || (i1.Origin == i2.Origin && i1.Sum > i2.Sum)
}
func NewProjectStats() *ProjectStats {
return &ProjectStats{
make(map[string]*ImportStats),
}
}
func AnalyzeSourceTree(dir string) (*ProjectStats, error) {
ps := NewProjectStats()
err := filepath.Walk(
dir,
func(path string, info os.FileInfo, err error) error {
fileDir := filepath.Dir(path)
baseName := filepath.Base(path)
if strings.HasSuffix(baseName, ".go") {
// Bail if not analyzing the gopack dir specifically
// and we hit that directory as part of this analysis.
// (should only ever be an issue with running gopack on itself and running tests)
// (use Contains rather than HasPrefix to handle absolute and relative paths)
if !strings.Contains(dir, GopackDir) &&
strings.Contains(fileDir, GopackDir) {
return nil
}
e := ps.analyzeSourceFile(path)
if e != nil {
return e
}
}
return nil
})
if err != nil {
return nil, err
}
return ps, nil
}
func (ps *ProjectStats) analyzeSourceFile(path string) error {
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, path, nil, parser.ParseComments)
if err != nil {
return err
}
for _, i := range f.Imports {
err = ps.foundImport(fs, i, path)
if err != nil {
return err
}
}
return nil
}
func (ps *ProjectStats) foundImport(fs *token.FileSet, i *ast.ImportSpec, path string) error {
importPath, err := strconv.Unquote(i.Path.Value)
if err != nil {
return err
}
ref := fs.Position(i.Pos())
_, found := ps.ImportStatsByPath[importPath]
if found {
ps.ImportStatsByPath[importPath].ReferencePositions = append(ps.ImportStatsByPath[importPath].ReferencePositions, ref)
} else {
ps.ImportStatsByPath[importPath] = NewImportStats(importPath, ref)
}
return nil
}
func (ps *ProjectStats) IsImportUsed(importPath string) bool {
_, used := ps.ImportStatsByPath[importPath]
return used
}
func (ps *ProjectStats) PrintSummary() {
writer := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0)
summary := ps.GetSummary()
fmt.Fprintln(writer, "Import stats summary:\n")
for _, item := range summary.Items {
fmt.Fprintln(writer, item.Legend())
}
fmt.Fprintln(writer, "\nR Remote, L Local, S Stdlib")
writer.Flush()
}
func (ps *ProjectStats) GetSummary() *Summary {
summary := &Summary{Items: []SummaryItem{}}
for k, v := range ps.ImportStatsByPath {
item := SummaryItem{Path: k, Sum: len(v.ReferencePositions)}
if v.Remote {
item.Origin = 1
} else if strings.HasPrefix(k, ".") {
item.Origin = 0
} else {
item.Origin = -1
}
summary.Append(item)
}
sort.Sort(summary)
return summary
}
func NewImportStats(importPath string, pos token.Position) *ImportStats {
parts := strings.Split(importPath, "/")
remote := false
if len(parts) > 0 && strings.Contains(parts[0], ".") && strings.Index(parts[0], ".") > 0 {
remote = true
}
return &ImportStats{
importPath, remote, []token.Position{pos},
}
}
func (i *ImportStats) ReferenceList() string {
lines := []string{}
for _, ref := range i.ReferencePositions {
lines = append(lines, fmt.Sprintf("%s:%d", ref.Filename, ref.Line))
}
return fmt.Sprintf("* %s", strings.Join(lines, "\n* "))
}