dep.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. alpm "github.com/Jguer/go-alpm"
  6. "github.com/Jguer/yay/v9/pkg/types"
  7. rpc "github.com/mikkeloscar/aur"
  8. )
  9. type providers struct {
  10. lookfor string
  11. Pkgs []*rpc.Pkg
  12. }
  13. func makeProviders(name string) providers {
  14. return providers{
  15. name,
  16. make([]*rpc.Pkg, 0),
  17. }
  18. }
  19. func (q providers) Len() int {
  20. return len(q.Pkgs)
  21. }
  22. func (q providers) Less(i, j int) bool {
  23. if q.lookfor == q.Pkgs[i].Name {
  24. return true
  25. }
  26. if q.lookfor == q.Pkgs[j].Name {
  27. return false
  28. }
  29. return types.LessRunes([]rune(q.Pkgs[i].Name), []rune(q.Pkgs[j].Name))
  30. }
  31. func (q providers) Swap(i, j int) {
  32. q.Pkgs[i], q.Pkgs[j] = q.Pkgs[j], q.Pkgs[i]
  33. }
  34. func splitDep(dep string) (string, string, string) {
  35. mod := ""
  36. split := strings.FieldsFunc(dep, func(c rune) bool {
  37. match := c == '>' || c == '<' || c == '='
  38. if match {
  39. mod += string(c)
  40. }
  41. return match
  42. })
  43. if len(split) == 0 {
  44. return "", "", ""
  45. }
  46. if len(split) == 1 {
  47. return split[0], "", ""
  48. }
  49. return split[0], mod, split[1]
  50. }
  51. func pkgSatisfies(name, version, dep string) bool {
  52. depName, depMod, depVersion := splitDep(dep)
  53. if depName != name {
  54. return false
  55. }
  56. return verSatisfies(version, depMod, depVersion)
  57. }
  58. func provideSatisfies(provide, dep string) bool {
  59. depName, depMod, depVersion := splitDep(dep)
  60. provideName, provideMod, provideVersion := splitDep(provide)
  61. if provideName != depName {
  62. return false
  63. }
  64. // Unversioned provieds can not satisfy a versioned dep
  65. if provideMod == "" && depMod != "" {
  66. return false
  67. }
  68. return verSatisfies(provideVersion, depMod, depVersion)
  69. }
  70. func verSatisfies(ver1, mod, ver2 string) bool {
  71. switch mod {
  72. case "=":
  73. return alpm.VerCmp(ver1, ver2) == 0
  74. case "<":
  75. return alpm.VerCmp(ver1, ver2) < 0
  76. case "<=":
  77. return alpm.VerCmp(ver1, ver2) <= 0
  78. case ">":
  79. return alpm.VerCmp(ver1, ver2) > 0
  80. case ">=":
  81. return alpm.VerCmp(ver1, ver2) >= 0
  82. }
  83. return true
  84. }
  85. func satisfiesAur(dep string, pkg *rpc.Pkg) bool {
  86. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  87. return true
  88. }
  89. for _, provide := range pkg.Provides {
  90. if provideSatisfies(provide, dep) {
  91. return true
  92. }
  93. }
  94. return false
  95. }
  96. func satisfiesRepo(dep string, pkg *alpm.Package) bool {
  97. if pkgSatisfies(pkg.Name(), pkg.Version(), dep) {
  98. return true
  99. }
  100. if pkg.Provides().ForEach(func(provide alpm.Depend) error {
  101. if provideSatisfies(provide.String(), dep) {
  102. return fmt.Errorf("")
  103. }
  104. return nil
  105. }) != nil {
  106. return true
  107. }
  108. return false
  109. }
  110. //split apart db/package to db and package
  111. func splitDBFromName(pkg string) (string, string) {
  112. split := strings.SplitN(pkg, "/", 2)
  113. if len(split) == 2 {
  114. return split[0], split[1]
  115. }
  116. return "", split[0]
  117. }
  118. func getBases(pkgs []*rpc.Pkg) []Base {
  119. basesMap := make(map[string]Base)
  120. for _, pkg := range pkgs {
  121. basesMap[pkg.PackageBase] = append(basesMap[pkg.PackageBase], pkg)
  122. }
  123. bases := make([]Base, 0, len(basesMap))
  124. for _, base := range basesMap {
  125. bases = append(bases, base)
  126. }
  127. return bases
  128. }
  129. func isDevelName(name string) bool {
  130. for _, suffix := range []string{"git", "svn", "hg", "bzr", "nightly"} {
  131. if strings.HasSuffix(name, "-"+suffix) {
  132. return true
  133. }
  134. }
  135. return strings.Contains(name, "-always-")
  136. }