/
cmd.go
201 lines (165 loc) · 4.31 KB
/
cmd.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
package cut
import (
"context"
"flag"
"fmt"
"io"
"os"
"sort"
"strings"
"text/tabwriter"
"github.com/google/subcommands"
"golang.org/x/tools/go/packages"
"github.com/loov/goda/internal/pkggraph"
"github.com/loov/goda/internal/pkgset"
"github.com/loov/goda/internal/stat"
"github.com/loov/goda/internal/templates"
)
type Command struct {
printStandard bool
noAlign bool
format string
exclude string
}
func (*Command) Name() string { return "cut" }
func (*Command) Synopsis() string { return "Analyse indirect-dependencies." }
func (*Command) Usage() string {
return `cut <expr>:
Print information about indirect-dependencies.
It shows packages whose removal would remove the most indirect dependencies.
See "help expr" for further information about expressions.
See "help format" for further information about formatting.
`
}
func (cmd *Command) SetFlags(f *flag.FlagSet) {
f.BoolVar(&cmd.printStandard, "std", false, "print std packages")
f.BoolVar(&cmd.noAlign, "noalign", false, "disable aligning tabs")
f.StringVar(&cmd.format, "f", "{{.ID}}\tin:{{.InDegree}}\tpkgs:{{.Cut.PackageCount}}\tsize:{{.Cut.AllFiles.Size}}\tloc:{{.Cut.Go.Lines}}", "info formatting")
f.StringVar(&cmd.exclude, "exclude", "", "package expr to exclude from output")
}
func (cmd *Command) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
t, err := templates.Parse(cmd.format)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid label string: %v\n", err)
return subcommands.ExitFailure
}
if !cmd.printStandard {
go pkgset.LoadStd()
}
result, err := pkgset.Calc(ctx, f.Args())
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return subcommands.ExitFailure
}
excluded := pkgset.New()
if cmd.exclude != "" {
excluded, err = pkgset.Calc(ctx, strings.Fields(cmd.exclude))
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return subcommands.ExitFailure
}
}
if !cmd.printStandard {
result = pkgset.Subtract(result, pkgset.Std())
}
graph := pkggraph.From(result)
nodes := map[string]*Node{}
nodelist := []*Node{}
var include func(parent *Node, n *pkggraph.Node)
include = func(parent *Node, n *pkggraph.Node) {
if n, ok := nodes[n.ID]; ok {
parent.Import(n)
return
}
node := &Node{
Node: n,
}
nodes[n.ID] = node
if _, analyse := graph.Packages[n.ID]; analyse {
nodelist = append(nodelist, node)
}
parent.Import(node)
for _, child := range n.ImportsNodes {
include(node, child)
}
}
for _, n := range graph.Sorted {
include(nil, n)
}
for _, p := range nodes {
if !cmd.printStandard && pkgset.IsStd(p.Package) {
continue
}
}
for _, node := range nodelist {
Reset(nodes)
node.Cut = Erase(node)
}
sort.Slice(nodelist, func(i, k int) bool {
if nodelist[i].InDegree() == nodelist[k].InDegree() {
return nodelist[i].Cut.PackageCount > nodelist[k].Cut.PackageCount
}
return nodelist[i].InDegree() < nodelist[k].InDegree()
})
var w io.Writer = os.Stdout
if !cmd.noAlign {
w = tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
}
for _, node := range nodelist {
if _, exclude := excluded[node.ID]; exclude {
continue
}
err := t.Execute(w, node)
fmt.Fprintln(w)
if err != nil {
fmt.Fprintf(os.Stderr, "template error: %v\n", err)
}
}
if w, ok := w.(interface{ Flush() error }); ok {
w.Flush()
}
return subcommands.ExitSuccess
}
func Reset(stats map[string]*Node) {
for _, stat := range stats {
stat.indegree = len(stat.ImportedBy)
}
}
func Erase(stat *Node) stat.Stat {
cut := stat.Stat
for _, imp := range stat.Imports {
imp.indegree--
if imp.indegree == 0 {
cut.Add(Erase(imp))
}
}
return cut
}
type Node struct {
*pkggraph.Node
Cut stat.Stat
Imports []*Node
ImportedBy []*Node
indegree int
}
func (parent *Node) Pkg() *packages.Package { return parent.Package }
func (parent *Node) InDegree() int { return len(parent.ImportedBy) }
func (parent *Node) OutDegree() int { return len(parent.Imports) }
func (parent *Node) Import(child *Node) {
if parent == nil {
return
}
if !hasPackage(parent.Imports, child) {
child.indegree++
child.ImportedBy = append(child.ImportedBy, parent)
parent.Imports = append(parent.Imports, child)
}
}
func hasPackage(xs []*Node, p *Node) bool {
for _, x := range xs {
if x == p {
return true
}
}
return false
}