depGraph.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package dep
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "strconv"
  7. "github.com/Jguer/yay/v11/pkg/db"
  8. "github.com/Jguer/yay/v11/pkg/metadata"
  9. aur "github.com/Jguer/yay/v11/pkg/query"
  10. "github.com/Jguer/yay/v11/pkg/text"
  11. "github.com/Jguer/yay/v11/pkg/topo"
  12. "github.com/leonelquinteros/gotext"
  13. )
  14. type Grapher struct {
  15. dbExecutor db.Executor
  16. aurCache *metadata.AURCache
  17. fullGraph bool // If true, the graph will include all dependencies including already installed ones or repo
  18. noConfirm bool
  19. w io.Writer // output writer
  20. }
  21. func NewGrapher(dbExecutor db.Executor, aurCache *metadata.AURCache, fullGraph, noConfirm bool, output io.Writer) *Grapher {
  22. return &Grapher{
  23. dbExecutor: dbExecutor,
  24. aurCache: aurCache,
  25. fullGraph: fullGraph,
  26. noConfirm: noConfirm,
  27. w: output,
  28. }
  29. }
  30. func (g *Grapher) GraphFromAURCache(targets []string) (*topo.Graph[string], error) {
  31. graph := topo.New[string]()
  32. for _, target := range targets {
  33. aurPkgs, _ := g.aurCache.FindPackage(target)
  34. pkg := provideMenu(g.w, target, aurPkgs, g.noConfirm)
  35. depSlice := ComputeCombinedDepList(pkg, false, false)
  36. g.addNodes(graph, pkg.Name, depSlice)
  37. }
  38. return graph, nil
  39. }
  40. func (g *Grapher) addNodes(
  41. graph *topo.Graph[string],
  42. parentPkgName string,
  43. deps []string,
  44. ) {
  45. for _, depString := range deps {
  46. depName, _, _ := splitDep(depString)
  47. if g.dbExecutor.LocalSatisfierExists(depString) {
  48. if g.fullGraph {
  49. graph.SetNodeInfo(depName, &topo.NodeInfo{Color: "green"})
  50. if err := graph.DependOn(depName, parentPkgName); err != nil {
  51. text.Warnln(depName, parentPkgName, err)
  52. }
  53. }
  54. continue
  55. }
  56. if graph.Exists(depName) {
  57. if err := graph.DependOn(depName, parentPkgName); err != nil {
  58. text.Warnln(depName, parentPkgName, err)
  59. }
  60. continue
  61. }
  62. // Check ALPM
  63. if alpmPkg := g.dbExecutor.SyncSatisfier(depString); alpmPkg != nil {
  64. if err := graph.DependOn(alpmPkg.Name(), parentPkgName); err != nil {
  65. text.Warnln("repo dep warn:", depName, parentPkgName, err)
  66. }
  67. graph.SetNodeInfo(alpmPkg.Name(), &topo.NodeInfo{Color: "blue"})
  68. if newDeps := alpmPkg.Depends().Slice(); len(newDeps) != 0 && g.fullGraph {
  69. newDepsSlice := make([]string, 0, len(newDeps))
  70. for _, newDep := range newDeps {
  71. newDepsSlice = append(newDepsSlice, newDep.Name)
  72. }
  73. g.addNodes(graph, alpmPkg.Name(), newDepsSlice)
  74. }
  75. continue
  76. }
  77. if aurPkgs, _ := g.aurCache.FindPackage(depName); len(aurPkgs) != 0 { // Check AUR
  78. pkg := aurPkgs[0]
  79. if len(aurPkgs) > 1 {
  80. pkg := provideMenu(g.w, depName, aurPkgs, g.noConfirm)
  81. g.aurCache.SetProvideCache(depName, []*aur.Pkg{pkg})
  82. }
  83. if err := graph.Alias(pkg.PackageBase, pkg.Name); err != nil {
  84. text.Warnln("aur alias warn:", pkg.PackageBase, pkg.Name, err)
  85. }
  86. if err := graph.DependOn(pkg.PackageBase, parentPkgName); err != nil {
  87. text.Warnln("aur dep warn:", pkg.PackageBase, parentPkgName, err)
  88. }
  89. graph.SetNodeInfo(pkg.PackageBase, &topo.NodeInfo{Color: "lightgreen"})
  90. if newDeps := ComputeCombinedDepList(pkg, false, false); len(newDeps) != 0 {
  91. g.addNodes(graph, pkg.Name, newDeps)
  92. }
  93. continue
  94. }
  95. }
  96. }
  97. func provideMenu(w io.Writer, dep string, options []*aur.Pkg, noConfirm bool) *aur.Pkg {
  98. size := len(options)
  99. if size == 1 {
  100. return options[0]
  101. }
  102. str := text.Bold(gotext.Get("There are %d providers available for %s:", size, dep))
  103. str += "\n"
  104. size = 1
  105. str += text.SprintOperationInfo(gotext.Get("Repository AUR"), "\n ")
  106. for _, pkg := range options {
  107. str += fmt.Sprintf("%d) %s ", size, pkg.Name)
  108. size++
  109. }
  110. text.OperationInfoln(str)
  111. for {
  112. fmt.Fprintln(w, gotext.Get("\nEnter a number (default=1): "))
  113. if noConfirm {
  114. fmt.Fprintln(w, "1")
  115. return options[0]
  116. }
  117. numberBuf, err := text.GetInput("", false)
  118. if err != nil {
  119. fmt.Fprintln(os.Stderr, err)
  120. break
  121. }
  122. if numberBuf == "" {
  123. return options[0]
  124. }
  125. num, err := strconv.Atoi(numberBuf)
  126. if err != nil {
  127. text.Errorln(gotext.Get("invalid number: %s", numberBuf))
  128. continue
  129. }
  130. if num < 1 || num >= size {
  131. text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size-1))
  132. continue
  133. }
  134. return options[num-1]
  135. }
  136. return nil
  137. }