preparer.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/Jguer/yay/v11/pkg/db"
  10. "github.com/Jguer/yay/v11/pkg/dep"
  11. "github.com/Jguer/yay/v11/pkg/download"
  12. "github.com/Jguer/yay/v11/pkg/settings"
  13. "github.com/Jguer/yay/v11/pkg/settings/exe"
  14. "github.com/Jguer/yay/v11/pkg/text"
  15. mapset "github.com/deckarep/golang-set/v2"
  16. "github.com/leonelquinteros/gotext"
  17. )
  18. type Preparer struct {
  19. dbExecutor db.Executor
  20. cmdBuilder exe.ICmdBuilder
  21. config *settings.Configuration
  22. pkgBuildDirs []string
  23. makeDeps []string
  24. }
  25. func (preper *Preparer) ShouldCleanMakeDeps(ctx context.Context) PostInstallHookFunc {
  26. if len(preper.makeDeps) == 0 {
  27. return nil
  28. }
  29. switch preper.config.RemoveMake {
  30. case "yes":
  31. break
  32. case "no":
  33. return nil
  34. default:
  35. if !text.ContinueTask(os.Stdin, gotext.Get("Remove make dependencies after install?"), false, settings.NoConfirm) {
  36. return nil
  37. }
  38. }
  39. return func(ctx context.Context) error {
  40. return removeMake(ctx, preper.config.Runtime.CmdBuilder, preper.makeDeps)
  41. }
  42. }
  43. func (preper *Preparer) Present(w io.Writer, targets []map[string]*dep.InstallInfo) error {
  44. pkgsBySourceAndReason := map[string]map[string][]string{}
  45. for _, layer := range targets {
  46. for pkgName, info := range layer {
  47. source := dep.SourceNames[info.Source]
  48. reason := dep.ReasonNames[info.Reason]
  49. var pkgStr string
  50. if info.Version != "" {
  51. pkgStr = text.Cyan(fmt.Sprintf("%s-%s", pkgName, info.Version))
  52. } else {
  53. pkgStr = text.Cyan(pkgName)
  54. }
  55. if _, ok := pkgsBySourceAndReason[source]; !ok {
  56. pkgsBySourceAndReason[source] = map[string][]string{}
  57. }
  58. pkgsBySourceAndReason[source][reason] = append(pkgsBySourceAndReason[source][reason], pkgStr)
  59. if info.Reason == dep.MakeDep {
  60. preper.makeDeps = append(preper.makeDeps, pkgName)
  61. }
  62. }
  63. }
  64. for source, pkgsByReason := range pkgsBySourceAndReason {
  65. for reason, pkgs := range pkgsByReason {
  66. fmt.Fprintf(w, text.Bold("%s %s (%d):")+" %s\n",
  67. source,
  68. reason,
  69. len(pkgs),
  70. strings.Join(pkgs, ", "))
  71. }
  72. }
  73. return nil
  74. }
  75. func (preper *Preparer) PrepareWorkspace(ctx context.Context, targets []map[string]*dep.InstallInfo) (map[string]string, error) {
  76. aurBases := mapset.NewThreadUnsafeSet[string]()
  77. pkgBuildDirs := make(map[string]string, 0)
  78. for _, layer := range targets {
  79. for pkgName, info := range layer {
  80. if info.Source == dep.AUR {
  81. pkgBase := *info.AURBase
  82. aurBases.Add(pkgBase)
  83. pkgBuildDirs[pkgName] = filepath.Join(config.BuildDir, pkgBase)
  84. } else if info.Source == dep.SrcInfo {
  85. pkgBuildDirs[pkgName] = *info.SrcinfoPath
  86. }
  87. }
  88. }
  89. if _, errA := download.AURPKGBUILDRepos(ctx,
  90. preper.cmdBuilder, aurBases.ToSlice(), config.AURURL, config.BuildDir, false); errA != nil {
  91. return nil, errA
  92. }
  93. if errP := downloadPKGBUILDSourceFanout(ctx, config.Runtime.CmdBuilder,
  94. preper.pkgBuildDirs, false, config.MaxConcurrentDownloads); errP != nil {
  95. text.Errorln(errP)
  96. }
  97. return pkgBuildDirs, nil
  98. }