Skip to content

Commit

Permalink
basic aur install
Browse files Browse the repository at this point in the history
  • Loading branch information
Jguer committed Sep 11, 2022
1 parent 5bb46ac commit 351e352
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 75 deletions.
43 changes: 20 additions & 23 deletions aur_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package main
import (
"context"
"fmt"
"path/filepath"
"runtime"
"sync"

"github.com/leonelquinteros/gotext"

"github.com/Jguer/yay/v11/pkg/multierror"
"github.com/Jguer/yay/v11/pkg/settings/exe"
"github.com/Jguer/yay/v11/pkg/stringset"
"github.com/Jguer/yay/v11/pkg/text"
)

Expand All @@ -30,50 +28,49 @@ func (e *ErrDownloadSource) Unwrap() error {
return e.inner
}

func downloadPKGBUILDSource(ctx context.Context, cmdBuilder exe.ICmdBuilder, dest,
base string, incompatible stringset.StringSet,
func downloadPKGBUILDSource(ctx context.Context,
cmdBuilder exe.ICmdBuilder, pkgBuildDir string, installIncompatible bool,
) error {
dir := filepath.Join(dest, base)
args := []string{"--verifysource", "-Ccf"}

if incompatible.Get(base) {
if installIncompatible {
args = append(args, "--ignorearch")
}

err := cmdBuilder.Show(
cmdBuilder.BuildMakepkgCmd(ctx, dir, args...))
cmdBuilder.BuildMakepkgCmd(ctx, pkgBuildDir, args...))
if err != nil {
return ErrDownloadSource{inner: err, pkgName: base, errOut: ""}
return ErrDownloadSource{inner: err}
}

return nil
}

func downloadPKGBUILDSourceWorker(ctx context.Context, wg *sync.WaitGroup, dest string,
cBase <-chan string, valOut chan<- string, errOut chan<- error,
cmdBuilder exe.ICmdBuilder, incompatible stringset.StringSet,
func downloadPKGBUILDSourceWorker(ctx context.Context, wg *sync.WaitGroup,
dirChannel <-chan string, valOut chan<- string, errOut chan<- error,
cmdBuilder exe.ICmdBuilder, incompatible bool,
) {
for base := range cBase {
err := downloadPKGBUILDSource(ctx, cmdBuilder, dest, base, incompatible)
for pkgBuildDir := range dirChannel {
err := downloadPKGBUILDSource(ctx, cmdBuilder, pkgBuildDir, incompatible)
if err != nil {
errOut <- ErrDownloadSource{inner: err, pkgName: base, errOut: ""}
errOut <- ErrDownloadSource{inner: err, pkgName: pkgBuildDir, errOut: ""}
} else {
valOut <- base
valOut <- pkgBuildDir
}
}

wg.Done()
}

func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilder, dest string,
bases []string, incompatible stringset.StringSet, maxConcurrentDownloads int,
func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilder, pkgBuildDirs []string,
incompatible bool, maxConcurrentDownloads int,
) error {
if len(bases) == 0 {
if len(pkgBuildDirs) == 0 {
return nil // no work to do
}

if len(bases) == 1 {
return downloadPKGBUILDSource(ctx, cmdBuilder, dest, bases[0], incompatible)
if len(pkgBuildDirs) == 1 {
return downloadPKGBUILDSource(ctx, cmdBuilder, pkgBuildDirs[0], incompatible)
}

var (
Expand All @@ -89,8 +86,8 @@ func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilde
}

go func() {
for _, base := range bases {
c <- base
for _, pkgbuildDir := range pkgBuildDirs {
c <- pkgbuildDir
}

close(c)
Expand All @@ -100,7 +97,7 @@ func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilde
wg.Add(numOfWorkers)

for s := 0; s < numOfWorkers; s++ {
go downloadPKGBUILDSourceWorker(ctx, wg, dest, c,
go downloadPKGBUILDSourceWorker(ctx, wg, c,
fanInChanValues, fanInChanErrors, cmdBuilder, incompatible)
}

Expand Down
28 changes: 14 additions & 14 deletions aur_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"fmt"
"os/exec"
"path/filepath"
"sync/atomic"
"testing"

"github.com/stretchr/testify/assert"

"github.com/Jguer/yay/v11/pkg/multierror"
"github.com/Jguer/yay/v11/pkg/settings/exe"
"github.com/Jguer/yay/v11/pkg/stringset"
)

type TestMakepkgBuilder struct {
Expand Down Expand Up @@ -53,7 +53,7 @@ func Test_downloadPKGBUILDSource(t *testing.T) {
want: "makepkg --nocheck --config /etc/not.conf --verifysource -Ccf",
wantDir: "/tmp/yay-bin",
}
err := downloadPKGBUILDSource(context.TODO(), cmdBuilder, "/tmp", "yay-bin", stringset.Make())
err := downloadPKGBUILDSource(context.TODO(), cmdBuilder, filepath.Join("/tmp", "yay-bin"), false)
assert.NoError(t, err)
assert.Equal(t, 1, int(cmdBuilder.passes))
}
Expand All @@ -70,7 +70,7 @@ func Test_downloadPKGBUILDSourceError(t *testing.T) {
wantDir: "/tmp/yay-bin",
showError: &exec.ExitError{},
}
err := downloadPKGBUILDSource(context.TODO(), cmdBuilder, "/tmp", "yay-bin", stringset.Make())
err := downloadPKGBUILDSource(context.TODO(), cmdBuilder, filepath.Join("/tmp", "yay-bin"), false)
assert.Error(t, err)
assert.EqualError(t, err, "error downloading sources: \x1b[36myay-bin\x1b[0m \n\t context: <nil> \n\t \n")
}
Expand All @@ -81,7 +81,7 @@ func Test_downloadPKGBUILDSourceError(t *testing.T) {
func Test_downloadPKGBUILDSourceFanout(t *testing.T) {
t.Parallel()

bases := []string{"yay", "yay-bin", "yay-git", "yay-v11", "yay-v12"}
pkgBuildDirs := []string{"/tmp/yay", "/tmp/yay-bin", "/tmp/yay-git", "/tmp/yay-v11", "/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 All @@ -92,7 +92,7 @@ func Test_downloadPKGBUILDSourceFanout(t *testing.T) {
test: t,
}

err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, "/tmp", bases, stringset.Make(), maxConcurrentDownloads)
err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, pkgBuildDirs, false, maxConcurrentDownloads)
assert.NoError(t, err)
assert.Equal(t, 5, int(cmdBuilder.passes))
})
Expand All @@ -112,9 +112,9 @@ func Test_downloadPKGBUILDSourceFanoutNoCC(t *testing.T) {
test: t,
}

bases := []string{"yay"}
pkgBuildDirs := []string{"/tmp/yay"}

err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, "/tmp", bases, stringset.Make(), 0)
err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, pkgBuildDirs, false, 0)
assert.NoError(t, err)
assert.Equal(t, 1, int(cmdBuilder.passes))
}
Expand All @@ -133,15 +133,15 @@ func Test_downloadPKGBUILDSourceFanoutError(t *testing.T) {
showError: &exec.ExitError{},
}

bases := []string{
"yay",
"yay-bin",
"yay-git",
"yay-v11",
"yay-v12",
pkgBuildDirs := []string{
"/tmp/yay",
"/tmp/yay-bin",
"/tmp/yay-git",
"/tmp/yay-v11",
"/tmp/yay-v12",
}

err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, "/tmp", bases, stringset.Make(), 0)
err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, pkgBuildDirs, false, 0)
assert.Error(t, err)
assert.Equal(t, 5, int(cmdBuilder.passes))
assert.Len(t, err.(*multierror.MultiError).Errors, 5)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
require github.com/tidwall/gjson v1.14.3

require (
github.com/deckarep/golang-set/v2 v2.1.0
github.com/itchyny/gojq v0.12.8
github.com/itchyny/timefmt-go v0.1.3 // indirect
github.com/ohler55/ojg v1.14.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/bradleyjkemp/cupaloy v2.3.0+incompatible/go.mod h1:Au1Xw1sgaJ5iSFktEh
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set/v2 v2.1.0 h1:g47V4Or+DUdzbs8FxCCmgb6VYd+ptPAngjM6dtGktsI=
github.com/deckarep/golang-set/v2 v2.1.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
Expand Down
52 changes: 29 additions & 23 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,16 @@ func install(ctx context.Context, cmdArgs *parser.Arguments, dbExecutor db.Execu
}()

bases := make([]string, 0, len(do.Aur))
pkgBuildDirs := make([]string, 0, len(do.Aur))
for _, base := range do.Aur {
bases = append(bases, base.Pkgbase())
pkgBuildDirs = append(pkgBuildDirs, filepath.Join(config.BuildDir, base.Pkgbase()))
}

if errP := downloadPKGBUILDSourceFanout(ctx, config.Runtime.CmdBuilder, config.BuildDir,
bases, incompatible, config.MaxConcurrentDownloads); errP != nil {
if errP := downloadPKGBUILDSourceFanout(ctx,
config.Runtime.CmdBuilder,
pkgBuildDirs,
len(incompatible) > 0, config.MaxConcurrentDownloads); errP != nil {
text.Errorln(errP)
}

Expand Down Expand Up @@ -589,18 +593,6 @@ func buildInstallPkgbuilds(
incompatible stringset.StringSet,
conflicts stringset.MapStringSet, noDeps, noCheck bool,
) error {
arguments := cmdArgs.Copy()
arguments.ClearTargets()
arguments.Op = "U"
arguments.DelArg("confirm")
arguments.DelArg("noconfirm")
arguments.DelArg("c", "clean")
arguments.DelArg("q", "quiet")
arguments.DelArg("q", "quiet")
arguments.DelArg("y", "refresh")
arguments.DelArg("u", "sysupgrade")
arguments.DelArg("w", "downloadonly")

deps := make([]string, 0)
exp := make([]string, 0)
oldConfirm := settings.NoConfirm
Expand Down Expand Up @@ -636,8 +628,7 @@ func buildInstallPkgbuilds(
}

if !satisfied || !config.BatchInstall {
err = doInstall(ctx, arguments, cmdArgs, deps, exp)
arguments.ClearTargets()
err = doInstall(ctx, cmdArgs, deps, exp)

deps = make([]string, 0)
exp = make([]string, 0)
Expand Down Expand Up @@ -755,7 +746,7 @@ func buildInstallPkgbuilds(
for _, split := range base {
for suffix, optional := range map[string]bool{"": false, "-debug": true} {
deps, exp, errAdd = doAddTarget(dp, localNamesCache, remoteNamesCache,
arguments, cmdArgs, pkgdests, deps, exp, split.Name+suffix, optional)
cmdArgs, pkgdests, deps, exp, split.Name+suffix, optional)
if errAdd != nil {
return errAdd
}
Expand All @@ -776,7 +767,7 @@ func buildInstallPkgbuilds(
wg.Wait()
}

err = doInstall(ctx, arguments, cmdArgs, deps, exp)
err = doInstall(ctx, cmdArgs, deps, exp)
if err != nil {
go config.Runtime.VCSStore.RemovePackage([]string{do.Aur[len(do.Aur)-1].String()})
}
Expand All @@ -786,11 +777,26 @@ func buildInstallPkgbuilds(
return err
}

func doInstall(ctx context.Context, arguments, cmdArgs *parser.Arguments, pkgDeps, pkgExp []string) error {
if len(arguments.Targets) == 0 {
func doInstall(ctx context.Context, cmdArgs *parser.Arguments, pkgDeps, pkgExp []string) error {
arguments := cmdArgs.Copy()
arguments.ClearTargets()
arguments.Op = "U"
arguments.DelArg("confirm")
arguments.DelArg("noconfirm")
arguments.DelArg("c", "clean")
arguments.DelArg("q", "quiet")
arguments.DelArg("q", "quiet")
arguments.DelArg("y", "refresh")
arguments.DelArg("u", "sysupgrade")
arguments.DelArg("w", "downloadonly")

if len(pkgDeps)+len(pkgExp) == 0 {
return nil
}

arguments.AddTarget(pkgDeps...)
arguments.AddTarget(pkgExp...)

if errShow := config.Runtime.CmdBuilder.Show(config.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
arguments, config.Runtime.Mode, settings.NoConfirm)); errShow != nil {
return errShow
Expand All @@ -808,7 +814,7 @@ func doInstall(ctx context.Context, arguments, cmdArgs *parser.Arguments, pkgDep
}

func doAddTarget(dp *dep.Pool, localNamesCache, remoteNamesCache stringset.StringSet,
arguments, cmdArgs *parser.Arguments, pkgdests map[string]string,
cmdArgs *parser.Arguments, pkgdests map[string]string,
deps, exp []string, name string, optional bool,
) (newDeps, newExp []string, err error) {
pkgdest, ok := pkgdests[name]
Expand All @@ -831,15 +837,15 @@ func doAddTarget(dp *dep.Pool, localNamesCache, remoteNamesCache stringset.Strin
name, pkgdest))
}

arguments.AddTarget(pkgdest)

switch {
case cmdArgs.ExistsArg("asdeps", "asdep"):
deps = append(deps, name)
case cmdArgs.ExistsArg("asexplicit", "asexp"):
exp = append(exp, name)
case !dp.Explicit.Get(name) && !localNamesCache.Get(name) && !remoteNamesCache.Get(name):
deps = append(deps, name)
default:
exp = append(exp, name)
}

return deps, exp, nil
Expand Down
Loading

0 comments on commit 351e352

Please sign in to comment.