Skip to content

Commit

Permalink
add support for target install
Browse files Browse the repository at this point in the history
  • Loading branch information
Jguer committed Oct 27, 2022
1 parent f496dba commit ba935cc
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 65 deletions.
40 changes: 25 additions & 15 deletions aur_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ func (installer *Installer) installAURPackages(ctx context.Context,
nameToBase, pkgBuildDirsByBase map[string]string,
installIncompatible bool,
) error {
deps, exp := make([]string, 0, aurDepNames.Cardinality()), make([]string, 0, aurExpNames.Cardinality())
deps, exps := make([]string, 0, aurDepNames.Cardinality()), make([]string, 0, aurExpNames.Cardinality())
pkgArchives := make([]string, 0, len(exps)+len(deps))

for _, name := range aurDepNames.Union(aurExpNames).ToSlice() {
base := nameToBase[name]
Expand Down Expand Up @@ -147,22 +148,30 @@ func (installer *Installer) installAURPackages(ctx context.Context,
return fmt.Errorf("%s - %w", gotext.Get("error making: %s", base), errMake)
}

names, err := installer.getNewTargets(pkgdests, name)
newPKGArchives, hasDebug, err := installer.getNewTargets(pkgdests, name)
if err != nil {
return err
}

isDep := installer.isDep(cmdArgs, aurExpNames, name)
pkgArchives = append(pkgArchives, newPKGArchives...)

if isDep {
deps = append(deps, names...)
if isDep := installer.isDep(cmdArgs, aurExpNames, name); isDep {
deps = append(deps, name)
} else {
exp = append(exp, names...)
exps = append(exps, name)
}

if hasDebug {
deps = append(deps, name+"-debug")
}
}

if err := doInstall(ctx, cmdArgs, deps, exp); err != nil {
return fmt.Errorf("%s - %w", fmt.Sprintf(gotext.Get("error installing:")+" %v %v", deps, exp), err)
if err := installPkgArchive(ctx, cmdArgs, pkgArchives); err != nil {
return fmt.Errorf("%s - %w", fmt.Sprintf(gotext.Get("error installing:")+" %v", pkgArchives), err)
}

if err := setInstallReason(ctx, cmdArgs, deps, exps); err != nil {
return fmt.Errorf("%s - %w", fmt.Sprintf(gotext.Get("error installing:")+" %v", pkgArchives), err)
}

return nil
Expand All @@ -182,28 +191,29 @@ func (*Installer) isDep(cmdArgs *parser.Arguments, aurExpNames mapset.Set[string
}

func (installer *Installer) getNewTargets(pkgdests map[string]string, name string,
) ([]string, error) {
) ([]string, bool, error) {
pkgdest, ok := pkgdests[name]
names := make([]string, 0, 2)
if !ok {
return nil, &PkgDestNotInListError{name: name}
return nil, false, &PkgDestNotInListError{name: name}
}

pkgFiles := make([]string, 0, 2)

if _, errStat := os.Stat(pkgdest); os.IsNotExist(errStat) {
return nil, &UnableToFindPkgDestError{name: name, pkgDest: pkgdest}
return nil, false, &UnableToFindPkgDestError{name: name, pkgDest: pkgdest}
}

names = append(names, name)
pkgFiles = append(pkgFiles, pkgdest)

debugName := pkgdest + "-debug"
pkgdestDebug, ok := pkgdests[debugName]
if ok {
if _, errStat := os.Stat(pkgdestDebug); errStat == nil {
names = append(names, debugName)
pkgFiles = append(pkgFiles, debugName)
}
}

return names, nil
return pkgFiles, ok, nil
}

func (*Installer) installSyncPackages(ctx context.Context, cmdArgs *parser.Arguments,
Expand Down
2 changes: 1 addition & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func displayNumberMenu(ctx context.Context, pkgS []string, dbExecutor db.Executo
}

if config.NewInstallEngine {
return syncInstall(ctx, config, cmdArgs, dbExecutor)
return syncInstall(ctx, config, arguments, dbExecutor)
}

return install(ctx, arguments, dbExecutor, true)
Expand Down
25 changes: 17 additions & 8 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ func buildInstallPkgbuilds(
}

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

deps = make([]string, 0)
exp = make([]string, 0)
Expand Down Expand Up @@ -768,7 +768,7 @@ func buildInstallPkgbuilds(
wg.Wait()
}

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

func doInstall(ctx context.Context, cmdArgs *parser.Arguments, pkgDeps, pkgExp []string) error {
func installPkgArchive(ctx context.Context, cmdArgs *parser.Arguments, pkgArchives []string) error {
arguments := cmdArgs.Copy()
arguments.ClearTargets()
arguments.Op = "U"
Expand All @@ -790,13 +790,14 @@ func doInstall(ctx context.Context, cmdArgs *parser.Arguments, pkgDeps, pkgExp [
arguments.DelArg("y", "refresh")
arguments.DelArg("u", "sysupgrade")
arguments.DelArg("w", "downloadonly")
arguments.DelArg("asdeps", "asdep")
arguments.DelArg("asexplicit", "asexp")

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

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

if errShow := config.Runtime.CmdBuilder.Show(config.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
arguments, config.Runtime.Mode, settings.NoConfirm)); errShow != nil {
Expand All @@ -807,11 +808,19 @@ func doInstall(ctx context.Context, cmdArgs *parser.Arguments, pkgDeps, pkgExp [
fmt.Fprintln(os.Stderr, errStore)
}

if errDeps := asdeps(ctx, cmdArgs, pkgDeps); errDeps != nil {
return nil
}

func setInstallReason(ctx context.Context, cmdArgs *parser.Arguments, deps, exps []string) error {
if len(deps)+len(exps) == 0 {
return nil
}

if errDeps := asdeps(ctx, cmdArgs, deps); errDeps != nil {
return errDeps
}

return asexp(ctx, cmdArgs, pkgExp)
return asexp(ctx, cmdArgs, exps)
}

func doAddTarget(dp *dep.Pool, localNamesCache, remoteNamesCache stringset.StringSet,
Expand Down
2 changes: 1 addition & 1 deletion local_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func installLocalPKGBUILD(

grapher := dep.NewGrapher(dbExecutor, aurCache, false, settings.NoConfirm, os.Stdout)

graph, err := grapher.GraphFromSrcInfo(wd, pkgbuild)
graph, err := grapher.GraphFromSrcInfo(nil, wd, pkgbuild)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/graph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func graphPackage(
return errors.New(gotext.Get("only one target is allowed"))
}

graph, err := grapher.GraphFromAURCache([]string{targets[0]})
graph, err := grapher.GraphFromAURCache(nil, []string{targets[0]})
if err != nil {
return err
}
Expand Down
58 changes: 54 additions & 4 deletions pkg/dep/depGraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ type Grapher struct {
w io.Writer // output writer
}

func NewGrapher(dbExecutor db.Executor, aurCache *metadata.AURCache, fullGraph, noConfirm bool, output io.Writer) *Grapher {
func NewGrapher(dbExecutor db.Executor, aurCache *metadata.AURCache,
fullGraph, noConfirm bool, output io.Writer,
) *Grapher {
return &Grapher{
dbExecutor: dbExecutor,
aurCache: aurCache,
Expand All @@ -104,9 +106,48 @@ func NewGrapher(dbExecutor db.Executor, aurCache *metadata.AURCache, fullGraph,
}
}

func (g *Grapher) GraphFromSrcInfo(pkgBuildDir string, pkgbuild *gosrc.Srcinfo) (*topo.Graph[string, *InstallInfo], error) {
func (g *Grapher) GraphFromTargets(targets []string) (*topo.Graph[string, *InstallInfo], error) {
graph := topo.New[string, *InstallInfo]()

for _, targetString := range targets {
var (
err error
target = ToTarget(targetString)
)

switch target.DB {
case "aur":
graph, err = g.GraphFromAURCache(graph, []string{target.Name})
default:
graph.AddNode(target.Name)
g.ValidateAndSetNodeInfo(graph, target.Name, &topo.NodeInfo[*InstallInfo]{
Color: colorMap[Explicit],
Background: bgColorMap[AUR],
Value: &InstallInfo{
Source: Sync,
Reason: Explicit,
Version: target.Version,
SyncDBName: &target.DB,
},
})
}

if err != nil {
return nil, err
}
}

fmt.Println(graph)
return graph, nil
}

func (g *Grapher) GraphFromSrcInfo(graph *topo.Graph[string, *InstallInfo], pkgBuildDir string,
pkgbuild *gosrc.Srcinfo,
) (*topo.Graph[string, *InstallInfo], error) {
if graph == nil {
graph = topo.New[string, *InstallInfo]()
}

aurPkgs, err := makeAURPKGFromSrcinfo(g.dbExecutor, pkgbuild)
if err != nil {
return nil, err
Expand Down Expand Up @@ -147,11 +188,19 @@ func (g *Grapher) addDepNodes(pkg *aur.Pkg, graph *topo.Graph[string, *InstallIn
}
}

func (g *Grapher) GraphFromAURCache(targets []string) (*topo.Graph[string, *InstallInfo], error) {
graph := topo.New[string, *InstallInfo]()
func (g *Grapher) GraphFromAURCache(graph *topo.Graph[string, *InstallInfo], targets []string) (*topo.Graph[string, *InstallInfo], error) {
if graph == nil {
graph = topo.New[string, *InstallInfo]()
}

for _, target := range targets {
aurPkgs, _ := g.aurCache.FindPackage(target)
if len(aurPkgs) == 0 {
text.Errorln("No AUR package found for", target)

continue
}

pkg := provideMenu(g.w, target, aurPkgs, g.noConfirm)

g.ValidateAndSetNodeInfo(graph, pkg.Name, &topo.NodeInfo[*InstallInfo]{
Expand All @@ -165,6 +214,7 @@ func (g *Grapher) GraphFromAURCache(targets []string) (*topo.Graph[string, *Inst
},
})

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

Expand Down
31 changes: 0 additions & 31 deletions pkg/dep/depPool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,6 @@ import (
"github.com/Jguer/yay/v11/pkg/text"
)

type Target struct {
DB string
Name string
Mod string
Version string
}

func ToTarget(pkg string) Target {
dbName, depString := text.SplitDBFromName(pkg)
name, mod, depVersion := splitDep(depString)

return Target{
DB: dbName,
Name: name,
Mod: mod,
Version: depVersion,
}
}

func (t Target) DepString() string {
return t.Name + t.Mod + t.Version
}

func (t Target) String() string {
if t.DB != "" {
return t.DB + "/" + t.DepString()
}

return t.DepString()
}

type Pool struct {
Targets []Target
Explicit stringset.StringSet
Expand Down
34 changes: 34 additions & 0 deletions pkg/dep/target_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dep

import "github.com/Jguer/yay/v11/pkg/text"

type Target struct {
DB string
Name string
Mod string
Version string
}

func ToTarget(pkg string) Target {
dbName, depString := text.SplitDBFromName(pkg)
name, mod, depVersion := splitDep(depString)

return Target{
DB: dbName,
Name: name,
Mod: mod,
Version: depVersion,
}
}

func (t Target) DepString() string {
return t.Name + t.Mod + t.Version
}

func (t Target) String() string {
if t.DB != "" {
return t.DB + "/" + t.DepString()
}

return t.DepString()
}
4 changes: 2 additions & 2 deletions pkg/text/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func Debugln(a ...interface{}) {
return
}

fmt.Fprint(os.Stdout, append([]interface{}{Bold(yellow("[DEBUG] "))}, a...)...)
fmt.Fprintln(os.Stdout, ResetCode)
fmt.Fprintln(os.Stdout, append([]interface{}{Bold(yellow("[DEBUG] "))}, a...)...)
fmt.Fprint(os.Stdout, ResetCode)
}

func OperationInfoln(a ...interface{}) {
Expand Down
9 changes: 8 additions & 1 deletion preparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ func (preper *Preparer) Present(w io.Writer, targets []map[string]*dep.InstallIn
for pkgName, info := range layer {
source := dep.SourceNames[info.Source]
reason := dep.ReasonNames[info.Reason]
pkgStr := text.Cyan(fmt.Sprintf("%s-%s", pkgName, info.Version))

var pkgStr string
if info.Version != "" {
pkgStr = text.Cyan(fmt.Sprintf("%s-%s", pkgName, info.Version))
} else {
pkgStr = text.Cyan(pkgName)
}

if _, ok := pkgsBySourceAndReason[source]; !ok {
pkgsBySourceAndReason[source] = map[string][]string{}
}
Expand Down
2 changes: 1 addition & 1 deletion sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func syncInstall(ctx context.Context,

grapher := dep.NewGrapher(dbExecutor, aurCache, false, settings.NoConfirm, os.Stdout)

graph, err := grapher.GraphFromAURCache(cmdArgs.Targets)
graph, err := grapher.GraphFromTargets(cmdArgs.Targets)
if err != nil {
return err
}
Expand Down

0 comments on commit ba935cc

Please sign in to comment.