forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummary.go
More file actions
175 lines (143 loc) · 3.78 KB
/
summary.go
File metadata and controls
175 lines (143 loc) · 3.78 KB
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
package main
import (
"context"
"flag"
"fmt"
"sort"
"strings"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/sourcegraph/run"
"github.com/sourcegraph/sourcegraph/dev/depgraph/internal/graph"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
var (
summaryFlagSet = flag.NewFlagSet("depgraph summary", flag.ExitOnError)
summaryDepsSum = summaryFlagSet.Bool("deps.sum", false, "generate md5sum of each dependency")
summaryDepsOnly = summaryFlagSet.Bool("deps.only", false, "only display dependencies")
)
var summaryCommand = &ffcli.Command{
Name: "summary",
ShortUsage: "depgraph summary {package}",
ShortHelp: "Outputs a text summary of the given package dependency and dependents",
FlagSet: summaryFlagSet,
Exec: summary,
}
func summary(ctx context.Context, args []string) error {
if len(args) != 1 {
return errors.Errorf("expected exactly one package")
}
pkg := args[0]
root, err := findRoot()
if err != nil {
return err
}
graph, err := graph.Load(root)
if err != nil {
return err
}
if _, ok := graph.PackageNames[pkg]; !ok {
return errors.Newf("pkg %q not found", pkg)
}
dependencyMap := summaryTraverse(pkg, graph.Dependencies)
dependencies := make([]string, 0, len(dependencyMap))
for dependency := range dependencyMap {
dependencies = append(dependencies, dependency)
}
sort.Strings(dependencies)
dependentMap := summaryTraverse(pkg, graph.Dependents)
dependents := make([]string, 0, len(dependentMap))
for dependent := range dependentMap {
dependents = append(dependents, dependent)
}
sort.Strings(dependents)
fmt.Printf("Target package:\n")
printPkg(ctx, root, pkg)
fmt.Printf("\n")
fmt.Printf("Direct dependencies:\n")
for _, dependency := range dependencies {
if dependencyMap[dependency] {
printPkg(ctx, root, dependency)
}
}
fmt.Printf("\n")
fmt.Printf("Transitive dependencies:\n")
for _, dependency := range dependencies {
if !dependencyMap[dependency] {
printPkg(ctx, root, dependency)
}
}
if *summaryDepsOnly {
return nil
}
fmt.Printf("\n")
fmt.Printf("Dependent commands:\n")
for _, dependent := range dependents {
if isMain(graph, dependent) {
fmt.Printf("\t> %s\n", dependent)
}
}
fmt.Printf("\n")
fmt.Printf("Direct dependents:\n")
for _, dependent := range dependents {
if !isMain(graph, dependent) && dependentMap[dependent] {
fmt.Printf("\t> %s\n", dependent)
}
}
fmt.Printf("\n")
fmt.Printf("Transitive dependents:\n")
for _, dependent := range dependents {
if !isMain(graph, dependent) && !dependentMap[dependent] {
fmt.Printf("\t> %s\n", dependent)
}
}
return nil
}
// summaryTraverse returns a set of packages related to the given package via the given
// relation. Each package is returned with a boolean value indicating whether or not the
// relation is direct (true) or transitive (false).k
func summaryTraverse(pkg string, relation map[string][]string) map[string]bool {
m := make(map[string]bool, len(relation[pkg]))
for _, v := range relation[pkg] {
m[v] = true
}
outer:
for {
for k := range m {
for _, v := range relation[k] {
if _, ok := m[v]; ok {
continue
}
m[v] = false
continue outer
}
}
break
}
return m
}
// isMain returns true if the given package declares "main" in the given package name map.
func isMain(graph *graph.DependencyGraph, pkg string) bool {
for _, name := range graph.PackageNames[pkg] {
if name == "main" {
return true
}
}
return false
}
func printPkg(ctx context.Context, root string, pkg string) error {
fmt.Printf("\t> %s", pkg)
if *summaryDepsSum {
dir := "./" + pkg
lines, err := run.Bash(ctx, "tar c", dir, "| md5sum").
Dir(root).
Run().
Lines()
if err != nil {
return err
}
sum := strings.Split(lines[0], " ")[0]
fmt.Printf("\t%s", sum)
}
fmt.Println()
return nil
}