simplify src download

This commit is contained in:
jguer 2022-11-14 01:14:13 +01:00
parent fd46fa0f33
commit 3f7f55f260
No known key found for this signature in database
GPG Key ID: 6D6CC9BEA8556B35
7 changed files with 71 additions and 34 deletions

View File

@ -6,6 +6,7 @@ import (
"runtime" "runtime"
"sync" "sync"
mapset "github.com/deckarep/golang-set/v2"
"github.com/leonelquinteros/gotext" "github.com/leonelquinteros/gotext"
"github.com/Jguer/yay/v11/pkg/multierror" "github.com/Jguer/yay/v11/pkg/multierror"
@ -62,7 +63,7 @@ func downloadPKGBUILDSourceWorker(ctx context.Context, wg *sync.WaitGroup,
wg.Done() 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, incompatible bool, maxConcurrentDownloads int,
) error { ) error {
if len(pkgBuildDirs) == 0 { if len(pkgBuildDirs) == 0 {
@ -70,7 +71,9 @@ func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilde
} }
if len(pkgBuildDirs) == 1 { if len(pkgBuildDirs) == 1 {
return downloadPKGBUILDSource(ctx, cmdBuilder, pkgBuildDirs[0], incompatible) for _, pkgBuildDir := range pkgBuildDirs {
return downloadPKGBUILDSource(ctx, cmdBuilder, pkgBuildDir, incompatible)
}
} }
var ( var (
@ -85,9 +88,14 @@ func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilde
numOfWorkers = maxConcurrentDownloads numOfWorkers = maxConcurrentDownloads
} }
dedupSet := mapset.NewThreadUnsafeSet[string]()
go func() { go func() {
for _, pkgbuildDir := range pkgBuildDirs { for _, pkgbuildDir := range pkgBuildDirs {
c <- pkgbuildDir if !dedupSet.Contains(pkgbuildDir) {
c <- pkgbuildDir
dedupSet.Add(pkgbuildDir)
}
} }
close(c) close(c)

View File

@ -82,7 +82,13 @@ func Test_downloadPKGBUILDSourceError(t *testing.T) {
func Test_downloadPKGBUILDSourceFanout(t *testing.T) { func Test_downloadPKGBUILDSourceFanout(t *testing.T) {
t.Parallel() 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} { for _, maxConcurrentDownloads := range []int{0, 3} {
t.Run(fmt.Sprintf("maxconcurrentdownloads set to %d", maxConcurrentDownloads), func(t *testing.T) { t.Run(fmt.Sprintf("maxconcurrentdownloads set to %d", maxConcurrentDownloads), func(t *testing.T) {
cmdBuilder := &TestMakepkgBuilder{ cmdBuilder := &TestMakepkgBuilder{
@ -113,7 +119,7 @@ func Test_downloadPKGBUILDSourceFanoutNoCC(t *testing.T) {
test: t, test: t,
} }
pkgBuildDirs := []string{"/tmp/yay"} pkgBuildDirs := map[string]string{"yay": "/tmp/yay"}
err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, pkgBuildDirs, false, 0) err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, pkgBuildDirs, false, 0)
assert.NoError(t, err) assert.NoError(t, err)
@ -134,12 +140,12 @@ func Test_downloadPKGBUILDSourceFanoutError(t *testing.T) {
showError: &exec.ExitError{}, showError: &exec.ExitError{},
} }
pkgBuildDirs := []string{ pkgBuildDirs := map[string]string{
"/tmp/yay", "yay": "/tmp/yay",
"/tmp/yay-bin", "yay-bin": "/tmp/yay-bin",
"/tmp/yay-git", "yay-git": "/tmp/yay-git",
"/tmp/yay-v11", "yay-v11": "/tmp/yay-v11",
"/tmp/yay-v12", "yay-v12": "/tmp/yay-v12",
} }
err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, pkgBuildDirs, false, 0) err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, pkgBuildDirs, false, 0)

View File

@ -9,9 +9,9 @@ import (
"github.com/leonelquinteros/gotext" "github.com/leonelquinteros/gotext"
"github.com/Jguer/yay/v11/pkg/db" "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/query"
"github.com/Jguer/yay/v11/pkg/settings" "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/settings/parser"
"github.com/Jguer/yay/v11/pkg/stringset" "github.com/Jguer/yay/v11/pkg/stringset"
"github.com/Jguer/yay/v11/pkg/text" "github.com/Jguer/yay/v11/pkg/text"
@ -194,22 +194,17 @@ func isGitRepository(dir string) bool {
return !os.IsNotExist(err) 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...")) fmt.Println(gotext.Get("removing untracked AUR files from cache..."))
for i, base := range bases { for i, dir := range pkgbuildDirs {
dir := filepath.Join(config.BuildDir, base.Pkgbase()) text.OperationInfoln(gotext.Get("Cleaning (%d/%d): %s", i+1, len(pkgbuildDirs), text.Cyan(dir)))
if !isGitRepository(dir) {
continue
}
text.OperationInfoln(gotext.Get("Cleaning (%d/%d): %s", i+1, len(bases), text.Cyan(dir))) _, stderr, err := cmdBuilder.Capture(
cmdBuilder.BuildGitCmd(
_, stderr, err := config.Runtime.CmdBuilder.Capture(
config.Runtime.CmdBuilder.BuildGitCmd(
ctx, dir, "reset", "--hard", "HEAD")) ctx, dir, "reset", "--hard", "HEAD"))
if err != nil { 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( if err := config.Runtime.CmdBuilder.Show(

View File

@ -196,7 +196,16 @@ func install(ctx context.Context, cmdArgs *parser.Arguments, dbExecutor db.Execu
fmt.Println() fmt.Println()
if config.CleanAfter { 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() { 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) config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, false)
}() }()
bases := make([]string, 0, len(do.Aur)) pkgBuildDirs := make(map[string]string, len(do.Aur))
pkgBuildDirs := make([]string, 0, len(do.Aur))
for _, base := range do.Aur { for _, base := range do.Aur {
bases = append(bases, base.Pkgbase()) pkgBuildDirs[base.Pkgbase()] = filepath.Join(config.BuildDir, base.Pkgbase())
pkgBuildDirs = append(pkgBuildDirs, filepath.Join(config.BuildDir, base.Pkgbase()))
} }
if errP := downloadPKGBUILDSourceFanout(ctx, if errP := downloadPKGBUILDSourceFanout(ctx,

View File

@ -69,8 +69,7 @@ func installLocalPKGBUILD(
return err return err
} }
cleanFunc := preparer.ShouldCleanMakeDeps(ctx) if cleanFunc := preparer.ShouldCleanMakeDeps(ctx); cleanFunc != nil {
if cleanFunc != nil {
installer.AddPostInstallHook(cleanFunc) installer.AddPostInstallHook(cleanFunc)
} }
@ -79,6 +78,10 @@ func installLocalPKGBUILD(
return err return err
} }
if cleanAURDirsFunc := preparer.ShouldCleanAURDirs(ctx, pkgBuildDirs); cleanAURDirsFunc != nil {
installer.AddPostInstallHook(cleanAURDirsFunc)
}
if err = installer.Install(ctx, cmdArgs, topoSorted, pkgBuildDirs); err != nil { if err = installer.Install(ctx, cmdArgs, topoSorted, pkgBuildDirs); err != nil {
if errHook := installer.RunPostInstallHooks(ctx); errHook != nil { if errHook := installer.RunPostInstallHooks(ctx); errHook != nil {
text.Errorln(errHook) text.Errorln(errHook)

View File

@ -182,6 +182,7 @@ func (g *Grapher) GraphFromSrcInfo(ctx context.Context, graph *topo.Graph[string
for _, pkg := range aurPkgs { for _, pkg := range aurPkgs {
pkg := pkg pkg := pkg
graph.AddNode(pkg.Name)
g.ValidateAndSetNodeInfo(graph, pkg.Name, &topo.NodeInfo[*InstallInfo]{ g.ValidateAndSetNodeInfo(graph, pkg.Name, &topo.NodeInfo[*InstallInfo]{
Color: colorMap[Explicit], Color: colorMap[Explicit],
Background: bgColorMap[AUR], Background: bgColorMap[AUR],
@ -232,6 +233,7 @@ func (g *Grapher) GraphFromAURCache(ctx context.Context,
pkg := provideMenu(g.w, target, aurPkgs, g.noConfirm) pkg := provideMenu(g.w, target, aurPkgs, g.noConfirm)
graph.AddNode(pkg.Name)
g.ValidateAndSetNodeInfo(graph, pkg.Name, &topo.NodeInfo[*InstallInfo]{ g.ValidateAndSetNodeInfo(graph, pkg.Name, &topo.NodeInfo[*InstallInfo]{
Color: colorMap[Explicit], Color: colorMap[Explicit],
Background: bgColorMap[AUR], Background: bgColorMap[AUR],
@ -243,7 +245,6 @@ func (g *Grapher) GraphFromAURCache(ctx context.Context,
}, },
}) })
graph.AddNode(pkg.Name)
g.addDepNodes(ctx, pkg, graph) g.addDepNodes(ctx, pkg, graph)
} }

View File

@ -23,8 +23,24 @@ type Preparer struct {
cmdBuilder exe.ICmdBuilder cmdBuilder exe.ICmdBuilder
config *settings.Configuration 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 { 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 func(ctx context.Context) error {
return removeMake(ctx, preper.config.Runtime.CmdBuilder, preper.makeDeps) 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, if errP := downloadPKGBUILDSourceFanout(ctx, config.Runtime.CmdBuilder,
preper.pkgBuildDirs, false, config.MaxConcurrentDownloads); errP != nil { pkgBuildDirs, false, config.MaxConcurrentDownloads); errP != nil {
text.Errorln(errP) text.Errorln(errP)
} }
return pkgBuildDirs, nil return pkgBuildDirs, nil