yay/preparer.go

139 lines
3.4 KiB
Go
Raw Normal View History

2022-09-20 00:44:06 +02:00
package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/dep"
"github.com/Jguer/yay/v11/pkg/download"
"github.com/Jguer/yay/v11/pkg/settings"
"github.com/Jguer/yay/v11/pkg/settings/exe"
"github.com/Jguer/yay/v11/pkg/text"
2022-11-15 15:44:50 +01:00
2022-09-20 00:44:06 +02:00
mapset "github.com/deckarep/golang-set/v2"
"github.com/leonelquinteros/gotext"
)
type Preparer struct {
dbExecutor db.Executor
cmdBuilder exe.ICmdBuilder
config *settings.Configuration
2022-11-14 01:14:13 +01:00
makeDeps []string
}
2022-11-15 15:44:50 +01:00
func (preper *Preparer) ShouldCleanAURDirs(pkgBuildDirs map[string]string) PostInstallHookFunc {
2022-11-14 01:14:13 +01:00
if !preper.config.CleanAfter {
return nil
}
dirs := make([]string, 0, len(pkgBuildDirs))
for _, dir := range pkgBuildDirs {
dirs = append(dirs, dir)
}
text.Debugln("added post install hook to clean up AUR dirs", dirs)
2022-11-15 15:44:50 +01:00
2022-11-14 01:14:13 +01:00
return func(ctx context.Context) error {
cleanAfter(ctx, preper.config.Runtime.CmdBuilder, dirs)
return nil
}
2022-09-20 00:44:06 +02:00
}
2022-11-15 15:44:50 +01:00
func (preper *Preparer) ShouldCleanMakeDeps() PostInstallHookFunc {
2022-09-20 00:44:06 +02:00
if len(preper.makeDeps) == 0 {
return nil
}
switch preper.config.RemoveMake {
case "yes":
break
case "no":
return nil
default:
if !text.ContinueTask(os.Stdin, gotext.Get("Remove make dependencies after install?"), false, settings.NoConfirm) {
return nil
}
}
2022-11-14 01:14:13 +01:00
text.Debugln("added post install hook to clean up AUR makedeps", preper.makeDeps)
2022-11-15 15:44:50 +01:00
2022-09-20 00:44:06 +02:00
return func(ctx context.Context) error {
return removeMake(ctx, preper.config.Runtime.CmdBuilder, preper.makeDeps)
}
}
func (preper *Preparer) Present(w io.Writer, targets []map[string]*dep.InstallInfo) error {
pkgsBySourceAndReason := map[string]map[string][]string{}
for _, layer := range targets {
for pkgName, info := range layer {
source := dep.SourceNames[info.Source]
reason := dep.ReasonNames[info.Reason]
2022-10-28 00:38:11 +02:00
var pkgStr string
if info.Version != "" {
pkgStr = text.Cyan(fmt.Sprintf("%s-%s", pkgName, info.Version))
} else {
pkgStr = text.Cyan(pkgName)
}
2022-09-20 00:44:06 +02:00
if _, ok := pkgsBySourceAndReason[source]; !ok {
pkgsBySourceAndReason[source] = map[string][]string{}
}
pkgsBySourceAndReason[source][reason] = append(pkgsBySourceAndReason[source][reason], pkgStr)
2022-11-15 15:44:50 +01:00
2022-09-20 00:44:06 +02:00
if info.Reason == dep.MakeDep {
preper.makeDeps = append(preper.makeDeps, pkgName)
}
}
}
for source, pkgsByReason := range pkgsBySourceAndReason {
for reason, pkgs := range pkgsByReason {
fmt.Fprintf(w, text.Bold("%s %s (%d):")+" %s\n",
source,
reason,
len(pkgs),
strings.Join(pkgs, ", "))
}
}
return nil
}
func (preper *Preparer) PrepareWorkspace(ctx context.Context, targets []map[string]*dep.InstallInfo) (map[string]string, error) {
aurBases := mapset.NewThreadUnsafeSet[string]()
pkgBuildDirs := make(map[string]string, 0)
for _, layer := range targets {
for pkgName, info := range layer {
if info.Source == dep.AUR {
pkgBase := *info.AURBase
aurBases.Add(pkgBase)
pkgBuildDirs[pkgName] = filepath.Join(config.BuildDir, pkgBase)
} else if info.Source == dep.SrcInfo {
pkgBuildDirs[pkgName] = *info.SrcinfoPath
}
}
}
if _, errA := download.AURPKGBUILDRepos(ctx,
preper.cmdBuilder, aurBases.ToSlice(), config.AURURL, config.BuildDir, false); errA != nil {
return nil, errA
}
if errP := downloadPKGBUILDSourceFanout(ctx, config.Runtime.CmdBuilder,
2022-11-14 01:14:13 +01:00
pkgBuildDirs, false, config.MaxConcurrentDownloads); errP != nil {
2022-09-20 00:44:06 +02:00
text.Errorln(errP)
}
2022-11-15 15:44:50 +01:00
2022-09-20 00:44:06 +02:00
return pkgBuildDirs, nil
}