preparer.go 6.4 KB

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