yay/pkg/dep/depOrder.go

202 lines
4.0 KiB
Go
Raw Normal View History

2020-07-10 02:36:45 +02:00
package dep
import (
2020-07-10 02:36:45 +02:00
"fmt"
2021-09-08 22:28:08 +02:00
"github.com/Jguer/yay/v11/pkg/db"
aur "github.com/Jguer/yay/v11/pkg/query"
"github.com/Jguer/yay/v11/pkg/stringset"
"github.com/Jguer/yay/v11/pkg/text"
)
2020-07-10 02:36:45 +02:00
type Order struct {
Aur []Base
Repo []db.IPackage
Runtime stringset.StringSet
}
2021-12-18 21:56:16 +01:00
func newOrder() *Order {
2020-07-10 02:36:45 +02:00
return &Order{
make([]Base, 0),
make([]db.IPackage, 0),
make(stringset.StringSet),
}
}
func GetOrder(dp *Pool, noDeps, noCheckDeps bool) *Order {
2021-12-18 21:56:16 +01:00
do := newOrder()
for _, target := range dp.Targets {
dep := target.DepString()
if aurPkg := dp.Aur[dep]; aurPkg != nil && pkgSatisfies(aurPkg.Name, aurPkg.Version, dep) {
do.orderPkgAur(aurPkg, dp, true, noDeps, noCheckDeps)
} else if aurPkg := dp.findSatisfierAur(dep); aurPkg != nil {
do.orderPkgAur(aurPkg, dp, true, noDeps, noCheckDeps)
} else if repoPkg := dp.findSatisfierRepo(dep); repoPkg != nil {
do.orderPkgRepo(repoPkg, dp, true)
}
}
return do
}
2021-05-13 07:27:24 +02:00
func (do *Order) orderPkgAur(pkg *aur.Pkg, dp *Pool, runtime, noDeps, noCheckDeps bool) {
if runtime {
do.Runtime.Set(pkg.Name)
}
2021-08-11 20:13:28 +02:00
delete(dp.Aur, pkg.Name)
for i, deps := range ComputeCombinedDepList(pkg, noDeps, noCheckDeps) {
for _, dep := range deps {
if aurPkg := dp.findSatisfierAur(dep); aurPkg != nil {
do.orderPkgAur(aurPkg, dp, runtime && i == 0, noDeps, noCheckDeps)
}
if repoPkg := dp.findSatisfierRepo(dep); repoPkg != nil {
do.orderPkgRepo(repoPkg, dp, runtime && i == 0)
}
}
}
for i, base := range do.Aur {
if base.Pkgbase() == pkg.PackageBase {
do.Aur[i] = append(base, pkg)
return
}
}
do.Aur = append(do.Aur, Base{pkg})
}
func (do *Order) orderPkgRepo(pkg db.IPackage, dp *Pool, runtime bool) {
if runtime {
do.Runtime.Set(pkg.Name())
}
2021-08-11 20:13:28 +02:00
delete(dp.Repo, pkg.Name())
2020-07-29 01:53:25 +02:00
for _, dep := range dp.AlpmExecutor.PackageDepends(pkg) {
if repoPkg := dp.findSatisfierRepo(dep.String()); repoPkg != nil {
do.orderPkgRepo(repoPkg, dp, runtime)
}
2020-07-29 01:53:25 +02:00
}
do.Repo = append(do.Repo, pkg)
}
2020-07-10 02:36:45 +02:00
func (do *Order) HasMake() bool {
lenAur := 0
for _, base := range do.Aur {
lenAur += len(base)
}
return len(do.Runtime) != lenAur+len(do.Repo)
}
2020-07-10 02:36:45 +02:00
func (do *Order) GetMake() []string {
makeOnly := []string{}
for _, base := range do.Aur {
for _, pkg := range base {
if !do.Runtime.Get(pkg.Name) {
makeOnly = append(makeOnly, pkg.Name)
}
}
}
for _, pkg := range do.Repo {
if !do.Runtime.Get(pkg.Name()) {
makeOnly = append(makeOnly, pkg.Name())
}
}
return makeOnly
}
2020-07-10 02:36:45 +02:00
2021-08-11 20:13:28 +02:00
// Print prints repository packages to be downloaded.
2020-07-10 02:36:45 +02:00
func (do *Order) Print() {
repo := ""
repoMake := ""
2021-05-13 07:27:24 +02:00
aurString := ""
2020-07-10 02:36:45 +02:00
aurMake := ""
repoLen := 0
repoMakeLen := 0
aurLen := 0
aurMakeLen := 0
for _, pkg := range do.Repo {
pkgStr := fmt.Sprintf(" %s-%s", pkg.Name(), pkg.Version())
if do.Runtime.Get(pkg.Name()) {
repo += pkgStr
repoLen++
} else {
repoMake += pkgStr
repoMakeLen++
}
}
for _, base := range do.Aur {
pkg := base.Pkgbase()
pkgStr := " " + pkg + "-" + base[0].Version
pkgStrMake := pkgStr
push := false
pushMake := false
switch {
case len(base) > 1, pkg != base[0].Name:
pkgStr += " ("
pkgStrMake += " ("
for _, split := range base {
if do.Runtime.Get(split.Name) {
pkgStr += split.Name + " "
aurLen++
2021-08-11 20:13:28 +02:00
2020-07-10 02:36:45 +02:00
push = true
} else {
pkgStrMake += split.Name + " "
aurMakeLen++
pushMake = true
}
}
pkgStr = pkgStr[:len(pkgStr)-1] + ")"
pkgStrMake = pkgStrMake[:len(pkgStrMake)-1] + ")"
case do.Runtime.Get(base[0].Name):
aurLen++
2021-08-11 20:13:28 +02:00
2020-07-10 02:36:45 +02:00
push = true
default:
aurMakeLen++
2021-08-11 20:13:28 +02:00
2020-07-10 02:36:45 +02:00
pushMake = true
}
if push {
2021-05-13 07:27:24 +02:00
aurString += pkgStr
2020-07-10 02:36:45 +02:00
}
2021-08-11 20:13:28 +02:00
2020-07-10 02:36:45 +02:00
if pushMake {
aurMake += pkgStrMake
}
}
printDownloads("Repo", repoLen, repo)
printDownloads("Repo Make", repoMakeLen, repoMake)
2021-05-13 07:27:24 +02:00
printDownloads("Aur", aurLen, aurString)
2020-07-10 02:36:45 +02:00
printDownloads("Aur Make", aurMakeLen, aurMake)
}
func printDownloads(repoName string, length int, packages string) {
if length < 1 {
return
}
repoInfo := fmt.Sprintf(text.Bold(text.Blue("[%s:%d]")), repoName, length)
fmt.Println(repoInfo + text.Cyan(packages))
}