preparer.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. makeDeps []string
  23. }
  24. func (preper *Preparer) ShouldCleanAURDirs(ctx context.Context, pkgBuildDirs map[string]string) PostInstallHookFunc {
  25. if !preper.config.CleanAfter {
  26. return nil
  27. }
  28. dirs := make([]string, 0, len(pkgBuildDirs))
  29. for _, dir := range pkgBuildDirs {
  30. dirs = append(dirs, dir)
  31. }
  32. text.Debugln("added post install hook to clean up AUR dirs", dirs)
  33. return func(ctx context.Context) error {
  34. cleanAfter(ctx, preper.config.Runtime.CmdBuilder, dirs)
  35. return nil
  36. }
  37. }
  38. func (preper *Preparer) ShouldCleanMakeDeps(ctx context.Context) PostInstallHookFunc {
  39. if len(preper.makeDeps) == 0 {
  40. return nil
  41. }
  42. switch preper.config.RemoveMake {
  43. case "yes":
  44. break
  45. case "no":
  46. return nil
  47. default:
  48. if !text.ContinueTask(os.Stdin, gotext.Get("Remove make dependencies after install?"), false, settings.NoConfirm) {
  49. return nil
  50. }
  51. }
  52. text.Debugln("added post install hook to clean up AUR makedeps", preper.makeDeps)
  53. return func(ctx context.Context) error {
  54. return removeMake(ctx, preper.config.Runtime.CmdBuilder, preper.makeDeps)
  55. }
  56. }
  57. func (preper *Preparer) Present(w io.Writer, targets []map[string]*dep.InstallInfo) error {
  58. pkgsBySourceAndReason := map[string]map[string][]string{}
  59. for _, layer := range targets {
  60. for pkgName, info := range layer {
  61. source := dep.SourceNames[info.Source]
  62. reason := dep.ReasonNames[info.Reason]
  63. var pkgStr string
  64. if info.Version != "" {
  65. pkgStr = text.Cyan(fmt.Sprintf("%s-%s", pkgName, info.Version))
  66. } else {
  67. pkgStr = text.Cyan(pkgName)
  68. }
  69. if _, ok := pkgsBySourceAndReason[source]; !ok {
  70. pkgsBySourceAndReason[source] = map[string][]string{}
  71. }
  72. pkgsBySourceAndReason[source][reason] = append(pkgsBySourceAndReason[source][reason], pkgStr)
  73. if info.Reason == dep.MakeDep {
  74. preper.makeDeps = append(preper.makeDeps, pkgName)
  75. }
  76. }
  77. }
  78. for source, pkgsByReason := range pkgsBySourceAndReason {
  79. for reason, pkgs := range pkgsByReason {
  80. fmt.Fprintf(w, text.Bold("%s %s (%d):")+" %s\n",
  81. source,
  82. reason,
  83. len(pkgs),
  84. strings.Join(pkgs, ", "))
  85. }
  86. }
  87. return nil
  88. }
  89. func (preper *Preparer) PrepareWorkspace(ctx context.Context, targets []map[string]*dep.InstallInfo) (map[string]string, error) {
  90. aurBases := mapset.NewThreadUnsafeSet[string]()
  91. pkgBuildDirs := make(map[string]string, 0)
  92. for _, layer := range targets {
  93. for pkgName, info := range layer {
  94. if info.Source == dep.AUR {
  95. pkgBase := *info.AURBase
  96. aurBases.Add(pkgBase)
  97. pkgBuildDirs[pkgName] = filepath.Join(config.BuildDir, pkgBase)
  98. } else if info.Source == dep.SrcInfo {
  99. pkgBuildDirs[pkgName] = *info.SrcinfoPath
  100. }
  101. }
  102. }
  103. if _, errA := download.AURPKGBUILDRepos(ctx,
  104. preper.cmdBuilder, aurBases.ToSlice(), config.AURURL, config.BuildDir, false); errA != nil {
  105. return nil, errA
  106. }
  107. if errP := downloadPKGBUILDSourceFanout(ctx, config.Runtime.CmdBuilder,
  108. pkgBuildDirs, false, config.MaxConcurrentDownloads); errP != nil {
  109. text.Errorln(errP)
  110. }
  111. return pkgBuildDirs, nil
  112. }