Przeglądaj źródła

simplify src download

jguer 2 lat temu
rodzic
commit
3f7f55f260
7 zmienionych plików z 71 dodań i 34 usunięć
  1. 11 3
      aur_source.go
  2. 14 8
      aur_source_test.go
  3. 7 12
      clean.go
  4. 12 5
      install.go
  5. 5 2
      local_install.go
  6. 2 1
      pkg/dep/depGraph.go
  7. 20 3
      preparer.go

+ 11 - 3
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)

+ 14 - 8
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)

+ 7 - 12
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(

+ 12 - 5
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,

+ 5 - 2
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)

+ 2 - 1
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)
 	}
 

+ 20 - 3
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