-
Notifications
You must be signed in to change notification settings - Fork 4
/
package.go
53 lines (45 loc) · 994 Bytes
/
package.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
package build
import (
"fmt"
"strings"
"github.com/samber/lo"
"golang.org/x/exp/slices"
)
type Pkg struct {
Path string `json:"path"`
Files []string `json:"files,omitempty"`
Deps []string `json:"deps,omitempty"`
}
func (p *Pkg) AddDep(s string) {
if lo.Contains(p.Deps, s) {
return
}
p.Deps = append(p.Deps, s)
}
type Pkgs []*Pkg
func (p Pkgs) Get(s string) *Pkg {
return lo.FindOrElse(p, nil, func(x *Pkg) bool {
return x.Path == s
})
}
func (p Pkgs) Sort() {
slices.SortFunc(p, func(l *Pkg, r *Pkg) bool {
return l.Path < r.Path
})
}
func (p Pkgs) ToGraph(prefix string) string {
if prefix != "" {
prefix += "/"
}
var ret []string
add := func(s string, args ...any) {
ret = append(ret, fmt.Sprintf(s, args...))
}
add("graph LR;")
lo.ForEach(p, func(pkg *Pkg, _ int) {
lo.ForEach(pkg.Deps, func(d string, _ int) {
add("\t%s --> %s", strings.TrimPrefix(pkg.Path, prefix), strings.TrimPrefix(d, prefix))
})
})
return strings.Join(ret, "\n")
}