yay/pkg/dep/base.go

69 lines
1.2 KiB
Go
Raw Normal View History

2020-07-10 02:36:45 +02:00
package dep
import (
aur "github.com/Jguer/yay/v11/pkg/query"
"github.com/Jguer/yay/v11/pkg/stringset"
)
2020-07-10 02:36:45 +02:00
2021-08-11 20:13:28 +02:00
// Base is an AUR base package.
2021-05-13 07:27:24 +02:00
type Base []*aur.Pkg
2020-07-10 02:36:45 +02:00
// Pkgbase returns the first base package.
func (b Base) Pkgbase() string {
return b[0].PackageBase
}
// Version returns the first base package version.
func (b Base) Version() string {
return b[0].Version
}
// URLPath returns the first base package URL.
func (b Base) URLPath() string {
return b[0].URLPath
}
func (b Base) AnyIsInSet(set stringset.StringSet) bool {
for _, pkg := range b {
if set.Get(pkg.Name) {
return true
}
}
return false
}
2020-07-10 02:36:45 +02:00
// Packages foo and bar from a pkgbase named base would print like so:
2021-08-11 20:13:28 +02:00
// base (foo bar).
2020-07-10 02:36:45 +02:00
func (b Base) String() string {
pkg := b[0]
str := pkg.PackageBase
2021-08-11 20:13:28 +02:00
2020-07-10 02:36:45 +02:00
if len(b) > 1 || pkg.PackageBase != pkg.Name {
str2 := " ("
for _, split := range b {
str2 += split.Name + " "
}
2021-08-11 20:13:28 +02:00
2020-07-10 02:36:45 +02:00
str2 = str2[:len(str2)-1] + ")"
str += str2
}
return str
}
2021-05-13 07:27:24 +02:00
func GetBases(pkgs []*aur.Pkg) []Base {
2020-07-10 02:36:45 +02:00
basesMap := make(map[string]Base)
for _, pkg := range pkgs {
basesMap[pkg.PackageBase] = append(basesMap[pkg.PackageBase], pkg)
}
bases := make([]Base, 0, len(basesMap))
for _, base := range basesMap {
bases = append(bases, base)
}
return bases
}