yay/sync.go

186 lines
4.9 KiB
Go
Raw Normal View History

2022-09-19 22:01:19 +00:00
package main
import (
"context"
"errors"
2022-11-01 23:37:27 +00:00
"fmt"
2022-09-19 22:01:19 +00:00
"os"
"strings"
2022-09-19 22:01:19 +00:00
"github.com/Jguer/yay/v11/pkg/completion"
2022-09-19 22:01:19 +00:00
"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/dep"
"github.com/Jguer/yay/v11/pkg/multierror"
2022-09-19 22:01:19 +00:00
"github.com/Jguer/yay/v11/pkg/settings"
"github.com/Jguer/yay/v11/pkg/settings/parser"
"github.com/Jguer/yay/v11/pkg/srcinfo"
2022-09-19 22:01:19 +00:00
"github.com/Jguer/yay/v11/pkg/text"
"github.com/Jguer/yay/v11/pkg/upgrade"
2022-11-15 14:44:50 +00:00
2022-11-01 23:37:27 +00:00
"github.com/leonelquinteros/gotext"
2022-09-19 22:01:19 +00:00
)
func syncInstall(ctx context.Context,
config *settings.Configuration,
2022-10-28 21:58:15 +00:00
cmdArgs *parser.Arguments,
dbExecutor db.Executor,
2022-09-19 22:01:19 +00:00
) error {
2022-10-28 21:58:15 +00:00
aurCache := config.Runtime.AURCache
2022-11-01 23:37:27 +00:00
refreshArg := cmdArgs.ExistsArg("y", "refresh")
noDeps := cmdArgs.ExistsArg("d", "nodeps")
noCheck := strings.Contains(config.MFlags, "--nocheck")
if noDeps {
config.Runtime.CmdBuilder.AddMakepkgFlag("-d")
}
2022-11-01 23:37:27 +00:00
if refreshArg && config.Runtime.Mode.AtLeastRepo() {
if errR := earlyRefresh(ctx, cmdArgs); errR != nil {
return fmt.Errorf("%s - %w", gotext.Get("error refreshing databases"), errR)
}
// we may have done -Sy, our handle now has an old
// database.
if errRefresh := dbExecutor.RefreshHandle(); errRefresh != nil {
return errRefresh
}
}
2022-10-28 21:58:15 +00:00
grapher := dep.NewGrapher(dbExecutor, aurCache, false, settings.NoConfirm, os.Stdout, noDeps, noCheck)
2022-09-19 22:01:19 +00:00
2022-11-08 00:32:21 +00:00
graph, err := grapher.GraphFromTargets(ctx, nil, cmdArgs.Targets)
2022-09-19 22:01:19 +00:00
if err != nil {
return err
}
2022-11-01 22:48:35 +00:00
if cmdArgs.ExistsArg("u", "sysupgrade") {
var errSysUp error
2022-11-15 14:44:50 +00:00
upService := upgrade.NewUpgradeService(
grapher, aurCache, dbExecutor, config.Runtime.VCSStore,
config.Runtime, config, settings.NoConfirm, config.Runtime.Logger.Child("upgrade"))
graph, errSysUp = upService.GraphUpgrades(ctx, graph, cmdArgs.ExistsDouble("u", "sysupgrade"))
2022-11-01 22:48:35 +00:00
if errSysUp != nil {
return errSysUp
}
}
opService := NewOperationService(ctx, config, dbExecutor)
multiErr := &multierror.MultiError{}
targets := graph.TopoSortedLayerMap(func(s string, ii *dep.InstallInfo) error {
if ii.Source == dep.Missing {
multiErr.Add(errors.New(gotext.Get("could not find %s%s", s, ii.Version)))
}
return nil
})
if err := multiErr.Return(); err != nil {
return err
}
return opService.Run(ctx, cmdArgs, targets)
}
type OperationService struct {
ctx context.Context
cfg *settings.Configuration
dbExecutor db.Executor
}
2022-09-19 22:01:19 +00:00
func NewOperationService(ctx context.Context, cfg *settings.Configuration, dbExecutor db.Executor) *OperationService {
return &OperationService{
ctx: ctx,
cfg: cfg,
dbExecutor: dbExecutor,
}
}
2022-09-19 22:01:19 +00:00
func (o *OperationService) Run(ctx context.Context,
cmdArgs *parser.Arguments,
targets []map[string]*dep.InstallInfo,
) error {
if len(targets) == 0 {
fmt.Fprintln(os.Stdout, "", gotext.Get("there is nothing to do"))
return nil
}
preparer := NewPreparer(o.dbExecutor, o.cfg.Runtime.CmdBuilder, o.cfg)
installer := NewInstaller(o.dbExecutor, o.cfg.Runtime.CmdBuilder, o.cfg.Runtime.VCSStore, o.cfg.Runtime.Mode)
pkgBuildDirs, errInstall := preparer.Run(ctx, os.Stdout, targets)
if errInstall != nil {
return errInstall
2022-09-19 22:01:19 +00:00
}
2022-11-15 14:44:50 +00:00
cleanFunc := preparer.ShouldCleanMakeDeps()
2022-09-19 22:01:19 +00:00
if cleanFunc != nil {
installer.AddPostInstallHook(cleanFunc)
}
if cleanAURDirsFunc := preparer.ShouldCleanAURDirs(pkgBuildDirs); cleanAURDirsFunc != nil {
installer.AddPostInstallHook(cleanAURDirsFunc)
}
go func() {
errComp := completion.Update(ctx, o.cfg.Runtime.HTTPClient, o.dbExecutor,
o.cfg.AURURL, o.cfg.Runtime.CompletionPath, o.cfg.CompletionInterval, false)
if errComp != nil {
text.Warnln(errComp)
}
}()
srcInfo, errInstall := srcinfo.NewService(o.dbExecutor, o.cfg, o.cfg.Runtime.CmdBuilder, o.cfg.Runtime.VCSStore, pkgBuildDirs)
if errInstall != nil {
return errInstall
}
2022-09-19 22:01:19 +00:00
incompatible, errInstall := srcInfo.IncompatiblePkgs(ctx)
if errInstall != nil {
return errInstall
}
if errIncompatible := confirmIncompatible(incompatible); errIncompatible != nil {
return errIncompatible
}
if errPGP := srcInfo.CheckPGPKeys(ctx); errPGP != nil {
return errPGP
}
if errInstall := installer.Install(ctx, cmdArgs, targets, pkgBuildDirs); errInstall != nil {
return errInstall
2022-09-19 22:01:19 +00:00
}
var multiErr multierror.MultiError
if err := installer.CompileFailedAndIgnored(); err != nil {
multiErr.Add(err)
}
if err := srcInfo.UpdateVCSStore(ctx, targets, installer.failedAndIgnored); err != nil {
text.Warnln(err)
}
if err := installer.RunPostInstallHooks(ctx); err != nil {
multiErr.Add(err)
}
return multiErr.Return()
2022-09-19 22:01:19 +00:00
}
func confirmIncompatible(incompatible []string) error {
if len(incompatible) > 0 {
text.Warnln(gotext.Get("The following packages are not compatible with your architecture:"))
for _, pkg := range incompatible {
fmt.Print(" " + text.Cyan(pkg))
}
fmt.Println()
if !text.ContinueTask(os.Stdin, gotext.Get("Try to build them anyway?"), true, settings.NoConfirm) {
return &settings.ErrUserAbort{}
}
}
return nil
}