Skip to content

Commit

Permalink
simplify src download
Browse files Browse the repository at this point in the history
  • Loading branch information
Jguer committed Nov 14, 2022
1 parent fd46fa0 commit 3f7f55f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 34 deletions.
14 changes: 11 additions & 3 deletions aur_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -62,15 +63,17 @@ 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 {
return nil // no work to do
}

if len(pkgBuildDirs) == 1 {
return downloadPKGBUILDSource(ctx, cmdBuilder, pkgBuildDirs[0], incompatible)
for _, pkgBuildDir := range pkgBuildDirs {
return downloadPKGBUILDSource(ctx, cmdBuilder, pkgBuildDir, incompatible)
}
}

var (
Expand All @@ -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)
Expand Down
22 changes: 14 additions & 8 deletions aur_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
19 changes: 7 additions & 12 deletions clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down
17 changes: 12 additions & 5 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions local_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/dep/depGraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand All @@ -243,7 +245,6 @@ func (g *Grapher) GraphFromAURCache(ctx context.Context,
},
})

graph.AddNode(pkg.Name)
g.addDepNodes(ctx, pkg, graph)
}

Expand Down
23 changes: 20 additions & 3 deletions preparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3f7f55f

Please sign in to comment.