From 3f7f55f2607ad7462a7cd191229709b4085ebaf7 Mon Sep 17 00:00:00 2001 From: jguer Date: Mon, 14 Nov 2022 01:14:13 +0100 Subject: [PATCH] simplify src download --- aur_source.go | 14 +++++++++++--- aur_source_test.go | 22 ++++++++++++++-------- clean.go | 19 +++++++------------ install.go | 17 ++++++++++++----- local_install.go | 7 +++++-- pkg/dep/depGraph.go | 3 ++- preparer.go | 23 ++++++++++++++++++++--- 7 files changed, 71 insertions(+), 34 deletions(-) diff --git a/aur_source.go b/aur_source.go index 4f5ce688..d880e3be 100644 --- a/aur_source.go +++ b/aur_source.go @@ -6,6 +6,7 @@ import ( "runtime" "sync" + mapset "github.com/deckarep/golang-set/v2" "github.com/leonelquinteros/gotext" "github.com/Jguer/yay/v11/pkg/multierror" @@ -62,7 +63,7 @@ func downloadPKGBUILDSourceWorker(ctx context.Context, wg *sync.WaitGroup, wg.Done() } -func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilder, pkgBuildDirs []string, +func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilder, pkgBuildDirs map[string]string, incompatible bool, maxConcurrentDownloads int, ) error { if len(pkgBuildDirs) == 0 { @@ -70,7 +71,9 @@ func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilde } if len(pkgBuildDirs) == 1 { - return downloadPKGBUILDSource(ctx, cmdBuilder, pkgBuildDirs[0], incompatible) + for _, pkgBuildDir := range pkgBuildDirs { + return downloadPKGBUILDSource(ctx, cmdBuilder, pkgBuildDir, incompatible) + } } var ( @@ -85,9 +88,14 @@ func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilde numOfWorkers = maxConcurrentDownloads } + dedupSet := mapset.NewThreadUnsafeSet[string]() + go func() { for _, pkgbuildDir := range pkgBuildDirs { - c <- pkgbuildDir + if !dedupSet.Contains(pkgbuildDir) { + c <- pkgbuildDir + dedupSet.Add(pkgbuildDir) + } } close(c) diff --git a/aur_source_test.go b/aur_source_test.go index c8860917..261fcfa7 100644 --- a/aur_source_test.go +++ b/aur_source_test.go @@ -82,7 +82,13 @@ func Test_downloadPKGBUILDSourceError(t *testing.T) { func Test_downloadPKGBUILDSourceFanout(t *testing.T) { t.Parallel() - pkgBuildDirs := []string{"/tmp/yay", "/tmp/yay-bin", "/tmp/yay-git", "/tmp/yay-v11", "/tmp/yay-v12"} + pkgBuildDirs := map[string]string{ + "yay": "/tmp/yay", + "yay-bin": "/tmp/yay-bin", + "yay-git": "/tmp/yay-git", + "yay-v11": "/tmp/yay-v11", + "yay-v12": "/tmp/yay-v12", + } for _, maxConcurrentDownloads := range []int{0, 3} { t.Run(fmt.Sprintf("maxconcurrentdownloads set to %d", maxConcurrentDownloads), func(t *testing.T) { cmdBuilder := &TestMakepkgBuilder{ @@ -113,7 +119,7 @@ func Test_downloadPKGBUILDSourceFanoutNoCC(t *testing.T) { test: t, } - pkgBuildDirs := []string{"/tmp/yay"} + pkgBuildDirs := map[string]string{"yay": "/tmp/yay"} err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, pkgBuildDirs, false, 0) assert.NoError(t, err) @@ -134,12 +140,12 @@ func Test_downloadPKGBUILDSourceFanoutError(t *testing.T) { showError: &exec.ExitError{}, } - pkgBuildDirs := []string{ - "/tmp/yay", - "/tmp/yay-bin", - "/tmp/yay-git", - "/tmp/yay-v11", - "/tmp/yay-v12", + pkgBuildDirs := map[string]string{ + "yay": "/tmp/yay", + "yay-bin": "/tmp/yay-bin", + "yay-git": "/tmp/yay-git", + "yay-v11": "/tmp/yay-v11", + "yay-v12": "/tmp/yay-v12", } err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, pkgBuildDirs, false, 0) diff --git a/clean.go b/clean.go index c5bb2776..1a9643ae 100644 --- a/clean.go +++ b/clean.go @@ -9,9 +9,9 @@ import ( "github.com/leonelquinteros/gotext" "github.com/Jguer/yay/v11/pkg/db" - "github.com/Jguer/yay/v11/pkg/dep" "github.com/Jguer/yay/v11/pkg/query" "github.com/Jguer/yay/v11/pkg/settings" + "github.com/Jguer/yay/v11/pkg/settings/exe" "github.com/Jguer/yay/v11/pkg/settings/parser" "github.com/Jguer/yay/v11/pkg/stringset" "github.com/Jguer/yay/v11/pkg/text" @@ -194,22 +194,17 @@ func isGitRepository(dir string) bool { return !os.IsNotExist(err) } -func cleanAfter(ctx context.Context, bases []dep.Base) { +func cleanAfter(ctx context.Context, cmdBuilder exe.ICmdBuilder, pkgbuildDirs []string) { fmt.Println(gotext.Get("removing untracked AUR files from cache...")) - for i, base := range bases { - dir := filepath.Join(config.BuildDir, base.Pkgbase()) - if !isGitRepository(dir) { - continue - } + for i, dir := range pkgbuildDirs { + text.OperationInfoln(gotext.Get("Cleaning (%d/%d): %s", i+1, len(pkgbuildDirs), text.Cyan(dir))) - text.OperationInfoln(gotext.Get("Cleaning (%d/%d): %s", i+1, len(bases), text.Cyan(dir))) - - _, stderr, err := config.Runtime.CmdBuilder.Capture( - config.Runtime.CmdBuilder.BuildGitCmd( + _, stderr, err := cmdBuilder.Capture( + cmdBuilder.BuildGitCmd( ctx, dir, "reset", "--hard", "HEAD")) if err != nil { - text.Errorln(gotext.Get("error resetting %s: %s", base.String(), stderr)) + text.Errorln(gotext.Get("error resetting %s: %s", dir, stderr)) } if err := config.Runtime.CmdBuilder.Show( diff --git a/install.go b/install.go index ada00402..d1b3da11 100644 --- a/install.go +++ b/install.go @@ -196,7 +196,16 @@ func install(ctx context.Context, cmdArgs *parser.Arguments, dbExecutor db.Execu fmt.Println() if config.CleanAfter { - defer cleanAfter(ctx, do.Aur) + defer func() { + pkgbuildDirs := make([]string, 0, len(do.Aur)) + for _, base := range do.Aur { + dir := filepath.Join(config.BuildDir, base.Pkgbase()) + if isGitRepository(dir) { + pkgbuildDirs = append(pkgbuildDirs, dir) + } + } + cleanAfter(ctx, config.Runtime.CmdBuilder, pkgbuildDirs) + }() } if do.HasMake() { @@ -329,11 +338,9 @@ func install(ctx context.Context, cmdArgs *parser.Arguments, dbExecutor db.Execu config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, false) }() - bases := make([]string, 0, len(do.Aur)) - pkgBuildDirs := make([]string, 0, len(do.Aur)) + pkgBuildDirs := make(map[string]string, len(do.Aur)) for _, base := range do.Aur { - bases = append(bases, base.Pkgbase()) - pkgBuildDirs = append(pkgBuildDirs, filepath.Join(config.BuildDir, base.Pkgbase())) + pkgBuildDirs[base.Pkgbase()] = filepath.Join(config.BuildDir, base.Pkgbase()) } if errP := downloadPKGBUILDSourceFanout(ctx, diff --git a/local_install.go b/local_install.go index 4672aabb..205320c6 100644 --- a/local_install.go +++ b/local_install.go @@ -69,8 +69,7 @@ func installLocalPKGBUILD( return err } - cleanFunc := preparer.ShouldCleanMakeDeps(ctx) - if cleanFunc != nil { + if cleanFunc := preparer.ShouldCleanMakeDeps(ctx); cleanFunc != nil { installer.AddPostInstallHook(cleanFunc) } @@ -79,6 +78,10 @@ func installLocalPKGBUILD( return err } + if cleanAURDirsFunc := preparer.ShouldCleanAURDirs(ctx, pkgBuildDirs); cleanAURDirsFunc != nil { + installer.AddPostInstallHook(cleanAURDirsFunc) + } + if err = installer.Install(ctx, cmdArgs, topoSorted, pkgBuildDirs); err != nil { if errHook := installer.RunPostInstallHooks(ctx); errHook != nil { text.Errorln(errHook) diff --git a/pkg/dep/depGraph.go b/pkg/dep/depGraph.go index f2bbf945..eb9855de 100644 --- a/pkg/dep/depGraph.go +++ b/pkg/dep/depGraph.go @@ -182,6 +182,7 @@ func (g *Grapher) GraphFromSrcInfo(ctx context.Context, graph *topo.Graph[string for _, pkg := range aurPkgs { pkg := pkg + graph.AddNode(pkg.Name) g.ValidateAndSetNodeInfo(graph, pkg.Name, &topo.NodeInfo[*InstallInfo]{ Color: colorMap[Explicit], Background: bgColorMap[AUR], @@ -232,6 +233,7 @@ func (g *Grapher) GraphFromAURCache(ctx context.Context, pkg := provideMenu(g.w, target, aurPkgs, g.noConfirm) + graph.AddNode(pkg.Name) g.ValidateAndSetNodeInfo(graph, pkg.Name, &topo.NodeInfo[*InstallInfo]{ Color: colorMap[Explicit], Background: bgColorMap[AUR], @@ -243,7 +245,6 @@ func (g *Grapher) GraphFromAURCache(ctx context.Context, }, }) - graph.AddNode(pkg.Name) g.addDepNodes(ctx, pkg, graph) } diff --git a/preparer.go b/preparer.go index 11a6db48..c5be9437 100644 --- a/preparer.go +++ b/preparer.go @@ -23,8 +23,24 @@ type Preparer struct { cmdBuilder exe.ICmdBuilder config *settings.Configuration - pkgBuildDirs []string - makeDeps []string + makeDeps []string +} + +func (preper *Preparer) ShouldCleanAURDirs(ctx context.Context, pkgBuildDirs map[string]string) PostInstallHookFunc { + 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) + return func(ctx context.Context) error { + cleanAfter(ctx, preper.config.Runtime.CmdBuilder, dirs) + return nil + } } func (preper *Preparer) ShouldCleanMakeDeps(ctx context.Context) PostInstallHookFunc { @@ -43,6 +59,7 @@ func (preper *Preparer) ShouldCleanMakeDeps(ctx context.Context) PostInstallHook } } + text.Debugln("added post install hook to clean up AUR makedeps", preper.makeDeps) return func(ctx context.Context) error { return removeMake(ctx, preper.config.Runtime.CmdBuilder, preper.makeDeps) } @@ -109,7 +126,7 @@ func (preper *Preparer) PrepareWorkspace(ctx context.Context, targets []map[stri } if errP := downloadPKGBUILDSourceFanout(ctx, config.Runtime.CmdBuilder, - preper.pkgBuildDirs, false, config.MaxConcurrentDownloads); errP != nil { + pkgBuildDirs, false, config.MaxConcurrentDownloads); errP != nil { text.Errorln(errP) } return pkgBuildDirs, nil