yay/preparer.go

214 lines
5.6 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/menus"
2022-09-20 00:44:06 +02:00
"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
gosrc "github.com/Morganamilo/go-srcinfo"
2022-09-20 00:44:06 +02:00
mapset "github.com/deckarep/golang-set/v2"
"github.com/leonelquinteros/gotext"
)
type PreparerHookFunc func(ctx context.Context, config *settings.Configuration, w io.Writer, pkgbuildDirsByBase map[string]string) error
2022-09-20 00:44:06 +02:00
type Preparer struct {
dbExecutor db.Executor
cmdBuilder exe.ICmdBuilder
config *settings.Configuration
postDownloadHooks []PreparerHookFunc
postMergeHooks []PreparerHookFunc
2022-09-20 00:44:06 +02:00
2022-11-14 01:14:13 +01:00
makeDeps []string
}
func NewPreparer(dbExecutor db.Executor, cmdBuilder exe.ICmdBuilder, config *settings.Configuration) *Preparer {
preper := &Preparer{
dbExecutor: dbExecutor,
cmdBuilder: cmdBuilder,
config: config,
postDownloadHooks: []PreparerHookFunc{},
postMergeHooks: []PreparerHookFunc{},
}
if config.CleanMenu {
preper.postDownloadHooks = append(preper.postDownloadHooks, menus.CleanFn)
}
if config.DiffMenu {
preper.postMergeHooks = append(preper.postMergeHooks, menus.DiffFn)
}
if config.EditMenu {
preper.postMergeHooks = append(preper.postMergeHooks, menus.EditFn)
}
return preper
}
2022-11-15 15:44:50 +01:00
func (preper *Preparer) ShouldCleanAURDirs(pkgBuildDirs map[string]string) PostInstallHookFunc {
if !preper.config.CleanAfter || len(pkgBuildDirs) == 0 {
2022-11-14 01:14:13 +01:00
return nil
}
text.Debugln("added post install hook to clean up AUR dirs", pkgBuildDirs)
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, pkgBuildDirs)
2022-11-14 01:14:13 +01:00
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) Run(ctx context.Context,
w io.Writer, targets []map[string]*dep.InstallInfo,
) (pkgbuildDirsByBase map[string]string, err error) {
preper.Present(w, targets)
pkgBuildDirs, err := preper.PrepareWorkspace(ctx, targets)
if err != nil {
return nil, err
}
return pkgBuildDirs, nil
}
func (preper *Preparer) Present(w io.Writer, targets []map[string]*dep.InstallInfo) {
2022-09-20 00:44:06 +02:00
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, ", "))
}
}
}
func (preper *Preparer) PrepareWorkspace(ctx context.Context, targets []map[string]*dep.InstallInfo) (map[string]string, error) {
aurBasesToClone := mapset.NewThreadUnsafeSet[string]()
pkgBuildDirsByBase := make(map[string]string, len(targets))
2022-09-20 00:44:06 +02:00
for _, layer := range targets {
for _, info := range layer {
2022-09-20 00:44:06 +02:00
if info.Source == dep.AUR {
pkgBase := *info.AURBase
pkgBuildDir := filepath.Join(preper.config.BuildDir, pkgBase)
if preper.needToCloneAURBase(info, pkgBuildDir) {
aurBasesToClone.Add(pkgBase)
}
pkgBuildDirsByBase[pkgBase] = pkgBuildDir
2022-09-20 00:44:06 +02:00
} else if info.Source == dep.SrcInfo {
pkgBase := *info.AURBase
pkgBuildDirsByBase[pkgBase] = *info.SrcinfoPath
2022-09-20 00:44:06 +02:00
}
}
}
if _, errA := download.AURPKGBUILDRepos(ctx,
preper.cmdBuilder, aurBasesToClone.ToSlice(),
config.AURURL, config.BuildDir, false); errA != nil {
2022-09-20 00:44:06 +02:00
return nil, errA
}
if errP := downloadPKGBUILDSourceFanout(ctx, config.Runtime.CmdBuilder,
pkgBuildDirsByBase, false, config.MaxConcurrentDownloads); errP != nil {
2022-09-20 00:44:06 +02:00
text.Errorln(errP)
}
2022-11-15 15:44:50 +01:00
for _, hookFn := range preper.postDownloadHooks {
if err := hookFn(ctx, preper.config, os.Stdout, pkgBuildDirsByBase); err != nil {
return nil, err
}
}
if err := mergePkgbuilds(ctx, pkgBuildDirsByBase); err != nil {
return nil, err
}
for _, hookFn := range preper.postMergeHooks {
if err := hookFn(ctx, preper.config, os.Stdout, pkgBuildDirsByBase); err != nil {
return nil, err
}
}
return pkgBuildDirsByBase, nil
}
func (preper *Preparer) needToCloneAURBase(installInfo *dep.InstallInfo, pkgbuildDir string) bool {
if preper.config.ReDownload == "all" {
return true
}
srcinfoFile := filepath.Join(pkgbuildDir, ".SRCINFO")
if pkgbuild, err := gosrc.ParseFile(srcinfoFile); err == nil {
if db.VerCmp(pkgbuild.Version(), installInfo.Version) >= 0 {
text.OperationInfoln(
gotext.Get("PKGBUILD up to date, skipping download: %s",
text.Cyan(*installInfo.AURBase)))
return false
}
}
return true
2022-09-20 00:44:06 +02:00
}