preparer.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. downloadSources bool
  38. makeDeps []string
  39. }
  40. func NewPreparerWithoutHooks(dbExecutor db.Executor, cmdBuilder exe.ICmdBuilder,
  41. cfg *settings.Configuration, downloadSources bool,
  42. ) *Preparer {
  43. return &Preparer{
  44. dbExecutor: dbExecutor,
  45. cmdBuilder: cmdBuilder,
  46. cfg: cfg,
  47. hooks: []Hook{},
  48. downloadSources: downloadSources,
  49. }
  50. }
  51. func NewPreparer(dbExecutor db.Executor, cmdBuilder exe.ICmdBuilder,
  52. cfg *settings.Configuration,
  53. ) *Preparer {
  54. preper := NewPreparerWithoutHooks(dbExecutor, cmdBuilder, cfg, true)
  55. if cfg.CleanMenu {
  56. preper.hooks = append(preper.hooks, Hook{
  57. Name: "clean",
  58. Hookfn: menus.CleanFn,
  59. Type: PreDownloadSourcesHook,
  60. })
  61. }
  62. if cfg.DiffMenu {
  63. preper.hooks = append(preper.hooks, Hook{
  64. Name: "diff",
  65. Hookfn: menus.DiffFn,
  66. Type: PreDownloadSourcesHook,
  67. })
  68. }
  69. if cfg.EditMenu {
  70. preper.hooks = append(preper.hooks, Hook{
  71. Name: "edit",
  72. Hookfn: menus.EditFn,
  73. Type: PreDownloadSourcesHook,
  74. })
  75. }
  76. return preper
  77. }
  78. func (preper *Preparer) ShouldCleanAURDirs(pkgBuildDirs map[string]string) PostInstallHookFunc {
  79. if !preper.cfg.CleanAfter || len(pkgBuildDirs) == 0 {
  80. return nil
  81. }
  82. text.Debugln("added post install hook to clean up AUR dirs", pkgBuildDirs)
  83. return func(ctx context.Context) error {
  84. cleanAfter(ctx, preper.cfg, preper.cfg.Runtime.CmdBuilder, pkgBuildDirs)
  85. return nil
  86. }
  87. }
  88. func (preper *Preparer) ShouldCleanMakeDeps(cmdArgs *parser.Arguments) PostInstallHookFunc {
  89. if len(preper.makeDeps) == 0 {
  90. return nil
  91. }
  92. switch preper.cfg.RemoveMake {
  93. case "yes":
  94. break
  95. case "no":
  96. return nil
  97. default:
  98. if !text.ContinueTask(os.Stdin, gotext.Get("Remove make dependencies after install?"), false, settings.NoConfirm) {
  99. return nil
  100. }
  101. }
  102. text.Debugln("added post install hook to clean up AUR makedeps", preper.makeDeps)
  103. return func(ctx context.Context) error {
  104. return removeMake(ctx, preper.cfg, preper.cfg.Runtime.CmdBuilder, preper.makeDeps, cmdArgs)
  105. }
  106. }
  107. func (preper *Preparer) Run(ctx context.Context,
  108. w io.Writer, targets []map[string]*dep.InstallInfo,
  109. ) (pkgbuildDirsByBase map[string]string, err error) {
  110. preper.Present(w, targets)
  111. pkgBuildDirs, err := preper.PrepareWorkspace(ctx, targets)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return pkgBuildDirs, nil
  116. }
  117. func (preper *Preparer) Present(w io.Writer, targets []map[string]*dep.InstallInfo) {
  118. pkgsBySourceAndReason := map[string]map[string][]string{}
  119. for _, layer := range targets {
  120. for pkgName, info := range layer {
  121. source := dep.SourceNames[info.Source]
  122. reason := dep.ReasonNames[info.Reason]
  123. var pkgStr string
  124. if info.Version != "" {
  125. pkgStr = text.Cyan(fmt.Sprintf("%s-%s", pkgName, info.Version))
  126. } else {
  127. pkgStr = text.Cyan(pkgName)
  128. }
  129. if _, ok := pkgsBySourceAndReason[source]; !ok {
  130. pkgsBySourceAndReason[source] = map[string][]string{}
  131. }
  132. pkgsBySourceAndReason[source][reason] = append(pkgsBySourceAndReason[source][reason], pkgStr)
  133. if info.Reason == dep.MakeDep {
  134. preper.makeDeps = append(preper.makeDeps, pkgName)
  135. }
  136. }
  137. }
  138. for source, pkgsByReason := range pkgsBySourceAndReason {
  139. for reason, pkgs := range pkgsByReason {
  140. fmt.Fprintf(w, text.Bold("%s %s (%d):")+" %s\n",
  141. source,
  142. reason,
  143. len(pkgs),
  144. strings.Join(pkgs, ", "))
  145. }
  146. }
  147. }
  148. func (preper *Preparer) PrepareWorkspace(ctx context.Context, targets []map[string]*dep.InstallInfo) (map[string]string, error) {
  149. aurBasesToClone := mapset.NewThreadUnsafeSet[string]()
  150. pkgBuildDirsByBase := make(map[string]string, len(targets))
  151. for _, layer := range targets {
  152. for _, info := range layer {
  153. if info.Source == dep.AUR {
  154. pkgBase := *info.AURBase
  155. pkgBuildDir := filepath.Join(preper.cfg.BuildDir, pkgBase)
  156. if preper.needToCloneAURBase(info, pkgBuildDir) {
  157. aurBasesToClone.Add(pkgBase)
  158. }
  159. pkgBuildDirsByBase[pkgBase] = pkgBuildDir
  160. } else if info.Source == dep.SrcInfo {
  161. pkgBase := *info.AURBase
  162. pkgBuildDirsByBase[pkgBase] = *info.SrcinfoPath
  163. }
  164. }
  165. }
  166. if _, errA := download.AURPKGBUILDRepos(ctx,
  167. preper.cmdBuilder, aurBasesToClone.ToSlice(),
  168. preper.cfg.AURURL, preper.cfg.BuildDir, false); errA != nil {
  169. return nil, errA
  170. }
  171. if !preper.downloadSources {
  172. return pkgBuildDirsByBase, nil
  173. }
  174. if err := mergePkgbuilds(ctx, preper.cmdBuilder, pkgBuildDirsByBase); err != nil {
  175. return nil, err
  176. }
  177. for _, hookFn := range preper.hooks {
  178. if hookFn.Type == PreDownloadSourcesHook {
  179. if err := hookFn.Hookfn(ctx, preper.cfg, os.Stdout, pkgBuildDirsByBase); err != nil {
  180. return nil, err
  181. }
  182. }
  183. }
  184. if errP := downloadPKGBUILDSourceFanout(ctx, preper.cmdBuilder,
  185. pkgBuildDirsByBase, false, preper.cfg.MaxConcurrentDownloads); errP != nil {
  186. text.Errorln(errP)
  187. }
  188. return pkgBuildDirsByBase, nil
  189. }
  190. func (preper *Preparer) needToCloneAURBase(installInfo *dep.InstallInfo, pkgbuildDir string) bool {
  191. if preper.cfg.ReDownload == "all" {
  192. return true
  193. }
  194. srcinfoFile := filepath.Join(pkgbuildDir, ".SRCINFO")
  195. if pkgbuild, err := gosrc.ParseFile(srcinfoFile); err == nil {
  196. if db.VerCmp(pkgbuild.Version(), installInfo.Version) >= 0 {
  197. text.OperationInfoln(
  198. gotext.Get("PKGBUILD up to date, skipping download: %s",
  199. text.Cyan(*installInfo.AURBase)))
  200. return false
  201. }
  202. }
  203. return true
  204. }