2021-08-13 23:37:12 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
|
2022-11-14 01:14:13 +01:00
|
|
|
mapset "github.com/deckarep/golang-set/v2"
|
2021-08-13 23:37:12 +02:00
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
|
2021-09-08 22:28:08 +02:00
|
|
|
"github.com/Jguer/yay/v11/pkg/multierror"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/settings/exe"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/text"
|
2021-08-13 23:37:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type ErrDownloadSource struct {
|
|
|
|
inner error
|
|
|
|
pkgName string
|
|
|
|
errOut string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrDownloadSource) Error() string {
|
|
|
|
return fmt.Sprintln(gotext.Get("error downloading sources: %s", text.Cyan(e.pkgName)),
|
|
|
|
"\n\t context:", e.inner.Error(), "\n\t", e.errOut)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrDownloadSource) Unwrap() error {
|
|
|
|
return e.inner
|
|
|
|
}
|
|
|
|
|
2022-09-11 23:15:31 +02:00
|
|
|
func downloadPKGBUILDSource(ctx context.Context,
|
|
|
|
cmdBuilder exe.ICmdBuilder, pkgBuildDir string, installIncompatible bool,
|
2022-09-09 20:57:18 +02:00
|
|
|
) error {
|
2021-08-13 23:37:12 +02:00
|
|
|
args := []string{"--verifysource", "-Ccf"}
|
|
|
|
|
2022-09-11 23:15:31 +02:00
|
|
|
if installIncompatible {
|
2021-08-13 23:37:12 +02:00
|
|
|
args = append(args, "--ignorearch")
|
|
|
|
}
|
|
|
|
|
2022-09-09 20:57:18 +02:00
|
|
|
err := cmdBuilder.Show(
|
2022-09-11 23:15:31 +02:00
|
|
|
cmdBuilder.BuildMakepkgCmd(ctx, pkgBuildDir, args...))
|
2021-08-13 23:37:12 +02:00
|
|
|
if err != nil {
|
2022-11-14 00:25:14 +01:00
|
|
|
return ErrDownloadSource{inner: err, pkgName: pkgBuildDir}
|
2021-08-13 23:37:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-11 23:15:31 +02:00
|
|
|
func downloadPKGBUILDSourceWorker(ctx context.Context, wg *sync.WaitGroup,
|
|
|
|
dirChannel <-chan string, valOut chan<- string, errOut chan<- error,
|
|
|
|
cmdBuilder exe.ICmdBuilder, incompatible bool,
|
2022-09-09 20:57:18 +02:00
|
|
|
) {
|
2022-09-11 23:15:31 +02:00
|
|
|
for pkgBuildDir := range dirChannel {
|
|
|
|
err := downloadPKGBUILDSource(ctx, cmdBuilder, pkgBuildDir, incompatible)
|
2021-08-13 23:37:12 +02:00
|
|
|
if err != nil {
|
2022-09-11 23:15:31 +02:00
|
|
|
errOut <- ErrDownloadSource{inner: err, pkgName: pkgBuildDir, errOut: ""}
|
2021-08-13 23:37:12 +02:00
|
|
|
} else {
|
2022-09-11 23:15:31 +02:00
|
|
|
valOut <- pkgBuildDir
|
2021-08-13 23:37:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
2022-11-14 01:14:13 +01:00
|
|
|
func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilder, pkgBuildDirs map[string]string,
|
2022-09-11 23:15:31 +02:00
|
|
|
incompatible bool, maxConcurrentDownloads int,
|
2022-09-09 20:57:18 +02:00
|
|
|
) error {
|
2022-09-11 23:15:31 +02:00
|
|
|
if len(pkgBuildDirs) == 0 {
|
2022-09-09 20:57:18 +02:00
|
|
|
return nil // no work to do
|
|
|
|
}
|
|
|
|
|
2022-09-11 23:15:31 +02:00
|
|
|
if len(pkgBuildDirs) == 1 {
|
2022-11-14 01:14:13 +01:00
|
|
|
for _, pkgBuildDir := range pkgBuildDirs {
|
|
|
|
return downloadPKGBUILDSource(ctx, cmdBuilder, pkgBuildDir, incompatible)
|
|
|
|
}
|
2021-08-13 23:37:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
numOfWorkers = runtime.NumCPU()
|
|
|
|
wg = &sync.WaitGroup{}
|
|
|
|
c = make(chan string)
|
|
|
|
fanInChanValues = make(chan string)
|
|
|
|
fanInChanErrors = make(chan error)
|
|
|
|
)
|
|
|
|
|
2022-06-28 12:05:29 +02:00
|
|
|
if maxConcurrentDownloads != 0 {
|
|
|
|
numOfWorkers = maxConcurrentDownloads
|
|
|
|
}
|
|
|
|
|
2022-11-14 01:14:13 +01:00
|
|
|
dedupSet := mapset.NewThreadUnsafeSet[string]()
|
|
|
|
|
2021-08-13 23:37:12 +02:00
|
|
|
go func() {
|
2022-09-11 23:15:31 +02:00
|
|
|
for _, pkgbuildDir := range pkgBuildDirs {
|
2022-11-14 01:14:13 +01:00
|
|
|
if !dedupSet.Contains(pkgbuildDir) {
|
|
|
|
c <- pkgbuildDir
|
|
|
|
dedupSet.Add(pkgbuildDir)
|
|
|
|
}
|
2021-08-13 23:37:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(c)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Launch Workers
|
|
|
|
wg.Add(numOfWorkers)
|
|
|
|
|
|
|
|
for s := 0; s < numOfWorkers; s++ {
|
2022-09-11 23:15:31 +02:00
|
|
|
go downloadPKGBUILDSourceWorker(ctx, wg, c,
|
2021-08-13 23:37:12 +02:00
|
|
|
fanInChanValues, fanInChanErrors, cmdBuilder, incompatible)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(fanInChanValues)
|
|
|
|
close(fanInChanErrors)
|
|
|
|
}()
|
|
|
|
|
|
|
|
returnErr := multierror.MultiError{}
|
|
|
|
|
|
|
|
receiver:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case _, ok := <-fanInChanValues:
|
|
|
|
if !ok {
|
|
|
|
break receiver
|
|
|
|
}
|
|
|
|
case err, ok := <-fanInChanErrors:
|
|
|
|
if !ok {
|
|
|
|
break receiver
|
|
|
|
}
|
|
|
|
returnErr.Add(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnErr.Return()
|
|
|
|
}
|