-
Notifications
You must be signed in to change notification settings - Fork 4
/
package.go
64 lines (54 loc) · 1.15 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
package build
import (
"cmp"
"runtime"
"slices"
"strings"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/util"
)
var ScriptExtension = func() string {
if runtime.GOOS == OSWindows {
return "bat"
}
return "sh"
}()
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() Pkgs {
slices.SortFunc(p, func(l *Pkg, r *Pkg) int {
return cmp.Compare(strings.ToLower(l.Path), strings.ToLower(r.Path))
})
return p
}
func (p Pkgs) ToGraph(prefix string) string {
if prefix != "" {
prefix += "/"
}
ret := &util.StringSlice{}
add := func(s string, args ...any) {
ret.Pushf(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 ret.Join("\n")
}