preparer.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/Jguer/yay/v12/pkg/db"
  10. "github.com/Jguer/yay/v12/pkg/dep"
  11. "github.com/Jguer/yay/v12/pkg/download"
  12. "github.com/Jguer/yay/v12/pkg/menus"
  13. "github.com/Jguer/yay/v12/pkg/settings"
  14. "github.com/Jguer/yay/v12/pkg/settings/exe"
  15. "github.com/Jguer/yay/v12/pkg/settings/parser"
  16. "github.com/Jguer/yay/v12/pkg/text"
  17. gosrc "github.com/Morganamilo/go-srcinfo"
  18. mapset "github.com/deckarep/golang-set/v2"
  19. "github.com/leonelquinteros/gotext"
  20. )
  21. type HookType string
  22. const (
  23. // PreDownloadSourcesHook is called before sourcing a package
  24. PreDownloadSourcesHook HookType = "pre-download-sources"
  25. )
  26. type HookFn func(ctx context.Context, config *settings.Configuration, w io.Writer, pkgbuildDirsByBase map[string]string) error
  27. type Hook struct {
  28. Name string
  29. Hookfn HookFn
  30. Type HookType
  31. }
  32. type Preparer struct {
  33. dbExecutor db.Executor
  34. cmdBuilder exe.ICmdBuilder
  35. cfg *settings.Configuration
  36. hooks []Hook
  37. makeDeps []string
  38. }
  39. func NewPreparer(dbExecutor db.Executor, cmdBuilder exe.ICmdBuilder, cfg *settings.Configuration) *Preparer {
  40. preper := &Preparer{
  41. dbExecutor: dbExecutor,
  42. cmdBuilder: cmdBuilder,
  43. cfg: cfg,
  44. hooks: []Hook{},
  45. }
  46. if cfg.CleanMenu {
  47. preper.hooks = append(preper.hooks, Hook{
  48. Name: "clean",
  49. Hookfn: menus.CleanFn,
  50. Type: PreDownloadSourcesHook,
  51. })
  52. }
  53. if cfg.DiffMenu {
  54. preper.hooks = append(preper.hooks, Hook{
  55. Name: "diff",
  56. Hookfn: menus.DiffFn,
  57. Type: PreDownloadSourcesHook,
  58. })
  59. }
  60. if cfg.EditMenu {
  61. preper.hooks = append(preper.hooks, Hook{
  62. Name: "edit",
  63. Hookfn: menus.EditFn,
  64. Type: PreDownloadSourcesHook,
  65. })
  66. }
  67. return preper
  68. }
  69. func (preper *Preparer) ShouldCleanAURDirs(pkgBuildDirs map[string]string) PostInstallHookFunc {
  70. if !preper.cfg.CleanAfter || len(pkgBuildDirs) == 0 {
  71. return nil
  72. }
  73. text.Debugln("added post install hook to clean up AUR dirs", pkgBuildDirs)
  74. return func(ctx context.Context) error {
  75. cleanAfter(ctx, preper.cfg, preper.cfg.Runtime.CmdBuilder, pkgBuildDirs)
  76. return nil
  77. }
  78. }
  79. func (preper *Preparer) ShouldCleanMakeDeps(cmdArgs *parser.Arguments) PostInstallHookFunc {
  80. if len(preper.makeDeps) == 0 {
  81. return nil
  82. }
  83. switch preper.cfg.RemoveMake {
  84. case "yes":
  85. break
  86. case "no":
  87. return nil
  88. default:
  89. if !text.ContinueTask(os.Stdin, gotext.Get("Remove make dependencies after install?"), false, settings.NoConfirm) {
  90. return nil
  91. }
  92. }
  93. text.Debugln("added post install hook to clean up AUR makedeps", preper.makeDeps)
  94. return func(ctx context.Context) error {
  95. return removeMake(ctx, preper.cfg, preper.cfg.Runtime.CmdBuilder, preper.makeDeps, cmdArgs)
  96. }
  97. }
  98. func (preper *Preparer) Run(ctx context.Context,
  99. w io.Writer, targets []map[string]*dep.InstallInfo,
  100. ) (pkgbuildDirsByBase map[string]string, err error) {
  101. preper.Present(w, targets)
  102. pkgBuildDirs, err := preper.PrepareWorkspace(ctx, targets)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return pkgBuildDirs, nil
  107. }
  108. func (preper *Preparer) Present(w io.Writer, targets []map[string]*dep.InstallInfo) {
  109. pkgsBySourceAndReason := map[string]map[string][]string{}
  110. for _, layer := range targets {
  111. for pkgName, info := range layer {
  112. source := dep.SourceNames[info.Source]
  113. reason := dep.ReasonNames[info.Reason]
  114. var pkgStr string
  115. if info.Version != "" {
  116. pkgStr = text.Cyan(fmt.Sprintf("%s-%s", pkgName, info.Version))
  117. } else {
  118. pkgStr = text.Cyan(pkgName)
  119. }
  120. if _, ok := pkgsBySourceAndReason[source]; !ok {
  121. pkgsBySourceAndReason[source] = map[string][]string{}
  122. }
  123. pkgsBySourceAndReason[source][reason] = append(pkgsBySourceAndReason[source][reason], pkgStr)
  124. if info.Reason == dep.MakeDep {
  125. preper.makeDeps = append(preper.makeDeps, pkgName)
  126. }
  127. }
  128. }
  129. for source, pkgsByReason := range pkgsBySourceAndReason {
  130. for reason, pkgs := range pkgsByReason {
  131. fmt.Fprintf(w, text.Bold("%s %s (%d):")+" %s\n",
  132. source,
  133. reason,
  134. len(pkgs),
  135. strings.Join(pkgs, ", "))
  136. }
  137. }
  138. }
  139. func (preper *Preparer) PrepareWorkspace(ctx context.Context, targets []map[string]*dep.InstallInfo) (map[string]string, error) {
  140. aurBasesToClone := mapset.NewThreadUnsafeSet[string]()
  141. pkgBuildDirsByBase := make(map[string]string, len(targets))
  142. for _, layer := range targets {
  143. for _, info := range layer {
  144. if info.Source == dep.AUR {
  145. pkgBase := *info.AURBase
  146. pkgBuildDir := filepath.Join(preper.cfg.BuildDir, pkgBase)
  147. if preper.needToCloneAURBase(info, pkgBuildDir) {
  148. aurBasesToClone.Add(pkgBase)
  149. }
  150. pkgBuildDirsByBase[pkgBase] = pkgBuildDir
  151. } else if info.Source == dep.SrcInfo {
  152. pkgBase := *info.AURBase
  153. pkgBuildDirsByBase[pkgBase] = *info.SrcinfoPath
  154. }
  155. }
  156. }
  157. if _, errA := download.AURPKGBUILDRepos(ctx,
  158. preper.cmdBuilder, aurBasesToClone.ToSlice(),
  159. preper.cfg.AURURL, preper.cfg.BuildDir, false); errA != nil {
  160. return nil, errA
  161. }
  162. if err := mergePkgbuilds(ctx, preper.cmdBuilder, pkgBuildDirsByBase); err != nil {
  163. return nil, err
  164. }
  165. for _, hookFn := range preper.hooks {
  166. if hookFn.Type == PreDownloadSourcesHook {
  167. if err := hookFn.Hookfn(ctx, preper.cfg, os.Stdout, pkgBuildDirsByBase); err != nil {
  168. return nil, err
  169. }
  170. }
  171. }
  172. if errP := downloadPKGBUILDSourceFanout(ctx, preper.cmdBuilder,
  173. pkgBuildDirsByBase, false, preper.cfg.MaxConcurrentDownloads); errP != nil {
  174. text.Errorln(errP)
  175. }
  176. return pkgBuildDirsByBase, nil
  177. }
  178. func (preper *Preparer) needToCloneAURBase(installInfo *dep.InstallInfo, pkgbuildDir string) bool {
  179. if preper.cfg.ReDownload == "all" {
  180. return true
  181. }
  182. srcinfoFile := filepath.Join(pkgbuildDir, ".SRCINFO")
  183. if pkgbuild, err := gosrc.ParseFile(srcinfoFile); err == nil {
  184. if db.VerCmp(pkgbuild.Version(), installInfo.Version) >= 0 {
  185. text.OperationInfoln(
  186. gotext.Get("PKGBUILD up to date, skipping download: %s",
  187. text.Cyan(*installInfo.AURBase)))
  188. return false
  189. }
  190. }
  191. return true
  192. }