yay/sync.go

152 lines
3.8 KiB
Go
Raw Normal View History

2022-09-20 00:01:19 +02:00
package main
import (
"context"
"errors"
2022-11-02 00:37:27 +01:00
"fmt"
2022-09-20 00:01:19 +02:00
"os"
"strings"
2022-09-20 00:01:19 +02:00
"github.com/Jguer/yay/v11/pkg/completion"
2022-09-20 00:01:19 +02:00
"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/dep"
"github.com/Jguer/yay/v11/pkg/multierror"
2022-09-20 00:01:19 +02:00
"github.com/Jguer/yay/v11/pkg/settings"
"github.com/Jguer/yay/v11/pkg/settings/parser"
"github.com/Jguer/yay/v11/pkg/text"
2022-11-15 15:44:50 +01:00
2022-11-02 00:37:27 +01:00
"github.com/leonelquinteros/gotext"
2022-09-20 00:01:19 +02:00
)
func syncInstall(ctx context.Context,
config *settings.Configuration,
2022-10-28 23:58:15 +02:00
cmdArgs *parser.Arguments,
dbExecutor db.Executor,
2022-09-20 00:01:19 +02:00
) error {
2022-10-28 23:58:15 +02:00
aurCache := config.Runtime.AURCache
2022-11-02 00:37:27 +01: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-02 00:37:27 +01: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 23:58:15 +02:00
grapher := dep.NewGrapher(dbExecutor, aurCache, false, settings.NoConfirm, os.Stdout, noDeps, noCheck)
2022-09-20 00:01:19 +02:00
2022-11-08 01:32:21 +01:00
graph, err := grapher.GraphFromTargets(ctx, nil, cmdArgs.Targets)
2022-09-20 00:01:19 +02:00
if err != nil {
return err
}
2022-11-01 23:48:35 +01:00
if cmdArgs.ExistsArg("u", "sysupgrade") {
var errSysUp error
2022-11-15 15:44:50 +01:00
2022-11-08 01:32:21 +01:00
graph, _, errSysUp = sysupgradeTargetsV2(ctx, aurCache, dbExecutor, graph, cmdArgs.ExistsDouble("u", "sysupgrade"))
2022-11-01 23:48:35 +01: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-20 00:01:19 +02:00
func NewOperationService(ctx context.Context, cfg *settings.Configuration, dbExecutor db.Executor) *OperationService {
return &OperationService{
ctx: ctx,
cfg: cfg,
dbExecutor: dbExecutor,
}
}
2022-09-20 00:01:19 +02: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, err := preparer.Run(ctx, os.Stdout, targets)
if err != nil {
return err
2022-09-20 00:01:19 +02:00
}
2022-11-15 15:44:50 +01:00
cleanFunc := preparer.ShouldCleanMakeDeps()
2022-09-20 00:01:19 +02:00
if cleanFunc != nil {
installer.AddPostInstallHook(cleanFunc)
}
if cleanAURDirsFunc := preparer.ShouldCleanAURDirs(pkgBuildDirs); cleanAURDirsFunc != nil {
installer.AddPostInstallHook(cleanAURDirsFunc)
}
srcinfoOp := srcinfoOperator{
dbExecutor: o.dbExecutor,
cfg: o.cfg,
cmdBuilder: installer.exeCmd,
}
srcinfos, err := srcinfoOp.Run(ctx, pkgBuildDirs)
2022-09-20 00:01:19 +02:00
if err != nil {
return err
}
go func() {
_ = completion.Update(ctx, o.cfg.Runtime.HTTPClient, o.dbExecutor,
o.cfg.AURURL, o.cfg.Runtime.CompletionPath, o.cfg.CompletionInterval, false)
}()
err = installer.Install(ctx, cmdArgs, targets, pkgBuildDirs, srcinfos)
2022-09-20 00:01:19 +02:00
if err != nil {
if errHook := installer.RunPostInstallHooks(ctx); errHook != nil {
text.Errorln(errHook)
}
return err
}
var multiErr multierror.MultiError
if err := installer.CompileFailedAndIgnored(); err != nil {
multiErr.Add(err)
}
if err := installer.RunPostInstallHooks(ctx); err != nil {
multiErr.Add(err)
}
return multiErr.Return()
2022-09-20 00:01:19 +02:00
}