preparer.go 6.7 KB

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