result.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package aur
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. vcs "github.com/jguer/yay/aur/vcs"
  7. "github.com/jguer/yay/config"
  8. "github.com/jguer/yay/pacman"
  9. rpc "github.com/mikkeloscar/aur"
  10. gopkg "github.com/mikkeloscar/gopkgbuild"
  11. )
  12. // PkgDependencies returns package dependencies not installed belonging to AUR
  13. // 0 is Repo, 1 is Foreign.
  14. func PkgDependencies(a *rpc.Pkg) (runDeps [2][]string, makeDeps [2][]string, err error) {
  15. var q Query
  16. if len(a.Depends) == 0 && len(a.MakeDepends) == 0 {
  17. q, err = rpc.Info([]string{a.Name})
  18. if len(q) == 0 || err != nil {
  19. err = fmt.Errorf("Unable to search dependencies, %s", err)
  20. return
  21. }
  22. } else {
  23. q = append(q, *a)
  24. }
  25. depSearch := pacman.BuildDependencies(a.Depends)
  26. if len(a.Depends) != 0 {
  27. runDeps[0], runDeps[1] = depSearch(q[0].Depends, true, false)
  28. if len(runDeps[0]) != 0 || len(runDeps[1]) != 0 {
  29. fmt.Println("\x1b[1;32m=>\x1b[1;33m Run Dependencies: \x1b[0m")
  30. printDeps(runDeps[0], runDeps[1])
  31. }
  32. }
  33. if len(a.MakeDepends) != 0 {
  34. makeDeps[0], makeDeps[1] = depSearch(q[0].MakeDepends, false, false)
  35. if len(makeDeps[0]) != 0 || len(makeDeps[1]) != 0 {
  36. fmt.Println("\x1b[1;32m=>\x1b[1;33m Make Dependencies: \x1b[0m")
  37. printDeps(makeDeps[0], makeDeps[1])
  38. }
  39. }
  40. depSearch(a.MakeDepends, false, true)
  41. err = nil
  42. return
  43. }
  44. func printDeps(repoDeps []string, aurDeps []string) {
  45. if len(repoDeps) != 0 {
  46. fmt.Print("\x1b[1;32m==> Repository dependencies: \x1b[0m")
  47. for _, repoD := range repoDeps {
  48. fmt.Print("\x1b[33m", repoD, " \x1b[0m")
  49. }
  50. fmt.Print("\n")
  51. }
  52. if len(aurDeps) != 0 {
  53. fmt.Print("\x1b[1;32m==> AUR dependencies: \x1b[0m")
  54. for _, aurD := range aurDeps {
  55. fmt.Print("\x1b[33m", aurD, " \x1b[0m")
  56. }
  57. fmt.Print("\n")
  58. }
  59. }
  60. func setupPackageSpace(a *rpc.Pkg) (err error) {
  61. dir := config.YayConf.BuildDir + a.PackageBase + "/"
  62. if _, err = os.Stat(dir); !os.IsNotExist(err) {
  63. if !config.ContinueTask("Directory exists. Clean Build?", "yY") {
  64. _ = os.RemoveAll(config.YayConf.BuildDir + a.PackageBase)
  65. }
  66. }
  67. if err = config.DownloadAndUnpack(BaseURL+a.URLPath, config.YayConf.BuildDir, false); err != nil {
  68. return
  69. }
  70. if !config.ContinueTask("Edit PKGBUILD?", "yY") {
  71. editcmd := exec.Command(config.Editor(), dir+"PKGBUILD")
  72. editcmd.Stdin, editcmd.Stdout, editcmd.Stderr = os.Stdin, os.Stdout, os.Stderr
  73. editcmd.Run()
  74. }
  75. pkgb, err := gopkg.ParseSRCINFO(dir + ".SRCINFO")
  76. if err == nil {
  77. for _, pkgsource := range pkgb.Source {
  78. owner, repo := vcs.ParseSource(pkgsource)
  79. if owner != "" && repo != "" {
  80. err = vcs.BranchInfo(a.Name, owner, repo)
  81. if err != nil {
  82. fmt.Println(err)
  83. }
  84. }
  85. }
  86. }
  87. err = os.Chdir(dir)
  88. if err != nil {
  89. return
  90. }
  91. return
  92. }
  93. // PkgInstall handles install from Info Result.
  94. func PkgInstall(a *rpc.Pkg, flags []string) (finalmdeps []string, err error) {
  95. fmt.Printf("\x1b[1;32m==> Installing\x1b[33m %s\x1b[0m\n", a.Name)
  96. if a.Maintainer == "" {
  97. fmt.Println("\x1b[1;31;40m==> Warning:\x1b[0;;40m This package is orphaned.\x1b[0m")
  98. }
  99. if err = setupPackageSpace(a); err != nil {
  100. return
  101. }
  102. if specialDBsauce {
  103. return
  104. }
  105. runDeps, makeDeps, err := PkgDependencies(a)
  106. if err != nil {
  107. return
  108. }
  109. repoDeps := append(runDeps[0], makeDeps[0]...)
  110. aurDeps := append(runDeps[1], makeDeps[1]...)
  111. finalmdeps = append(finalmdeps, makeDeps[0]...)
  112. finalmdeps = append(finalmdeps, makeDeps[1]...)
  113. if len(aurDeps) != 0 || len(repoDeps) != 0 {
  114. if !config.ContinueTask("Continue?", "nN") {
  115. return finalmdeps, fmt.Errorf("user did not like the dependencies")
  116. }
  117. }
  118. aurQ, _ := rpc.Info(aurDeps)
  119. if len(aurQ) != len(aurDeps) {
  120. (Query)(aurQ).MissingPackage(aurDeps)
  121. if !config.ContinueTask("Continue?", "nN") {
  122. return finalmdeps, fmt.Errorf("unable to install dependencies")
  123. }
  124. }
  125. var depArgs []string
  126. if config.YayConf.NoConfirm {
  127. depArgs = []string{"--asdeps", "--noconfirm"}
  128. } else {
  129. depArgs = []string{"--asdeps"}
  130. }
  131. // Repo dependencies
  132. if len(repoDeps) != 0 {
  133. errR := config.PassToPacman("-S", repoDeps, depArgs)
  134. if errR != nil {
  135. return finalmdeps, errR
  136. }
  137. }
  138. // Handle AUR dependencies
  139. for _, dep := range aurQ {
  140. finalmdepsR, errA := PkgInstall(&dep, depArgs)
  141. finalmdeps = append(finalmdeps, finalmdepsR...)
  142. if errA != nil {
  143. pacman.CleanRemove(repoDeps)
  144. pacman.CleanRemove(aurDeps)
  145. return finalmdeps, errA
  146. }
  147. }
  148. args := []string{"-sri"}
  149. args = append(args, flags...)
  150. makepkgcmd := exec.Command(config.YayConf.MakepkgBin, args...)
  151. makepkgcmd.Stdin, makepkgcmd.Stdout, makepkgcmd.Stderr = os.Stdin, os.Stdout, os.Stderr
  152. err = makepkgcmd.Run()
  153. if err == nil {
  154. _ = vcs.SaveBranchInfo()
  155. }
  156. return
  157. }
  158. // PrintInfo prints package info like pacman -Si.
  159. func PrintInfo(a *rpc.Pkg) {
  160. fmt.Println("\x1b[1;37mRepository :\x1b[0m", "aur")
  161. fmt.Println("\x1b[1;37mName :\x1b[0m", a.Name)
  162. fmt.Println("\x1b[1;37mVersion :\x1b[0m", a.Version)
  163. fmt.Println("\x1b[1;37mDescription :\x1b[0m", a.Description)
  164. if a.URL != "" {
  165. fmt.Println("\x1b[1;37mURL :\x1b[0m", a.URL)
  166. } else {
  167. fmt.Println("\x1b[1;37mURL :\x1b[0m", "None")
  168. }
  169. fmt.Println("\x1b[1;37mLicenses :\x1b[0m", a.License)
  170. // if len(a.Provides) != 0 {
  171. // fmt.Println("\x1b[1;37mProvides :\x1b[0m", a.Provides)
  172. // } else {
  173. // fmt.Println("\x1b[1;37mProvides :\x1b[0m", "None")
  174. // }
  175. if len(a.Depends) != 0 {
  176. fmt.Println("\x1b[1;37mDepends On :\x1b[0m", a.Depends)
  177. } else {
  178. fmt.Println("\x1b[1;37mDepends On :\x1b[0m", "None")
  179. }
  180. if len(a.MakeDepends) != 0 {
  181. fmt.Println("\x1b[1;37mMake depends On :\x1b[0m", a.MakeDepends)
  182. } else {
  183. fmt.Println("\x1b[1;37mMake depends On :\x1b[0m", "None")
  184. }
  185. if len(a.OptDepends) != 0 {
  186. fmt.Println("\x1b[1;37mOptional Deps :\x1b[0m", a.OptDepends)
  187. } else {
  188. fmt.Println("\x1b[1;37mOptional Deps :\x1b[0m", "None")
  189. }
  190. if len(a.Conflicts) != 0 {
  191. fmt.Println("\x1b[1;37mConflicts With :\x1b[0m", a.Conflicts)
  192. } else {
  193. fmt.Println("\x1b[1;37mConflicts With :\x1b[0m", "None")
  194. }
  195. if a.Maintainer != "" {
  196. fmt.Println("\x1b[1;37mMaintainer :\x1b[0m", a.Maintainer)
  197. } else {
  198. fmt.Println("\x1b[1;37mMaintainer :\x1b[0m", "None")
  199. }
  200. fmt.Println("\x1b[1;37mVotes :\x1b[0m", a.NumVotes)
  201. fmt.Println("\x1b[1;37mPopularity :\x1b[0m", a.Popularity)
  202. if a.OutOfDate != 0 {
  203. fmt.Println("\x1b[1;37mOut-of-date :\x1b[0m", "Yes")
  204. }
  205. }
  206. // RemoveMakeDeps receives a make dependency list and removes those
  207. // that are no longer necessary.
  208. func RemoveMakeDeps(depS []string) (err error) {
  209. hanging := pacman.SliceHangingPackages(depS)
  210. if len(hanging) != 0 {
  211. if !config.ContinueTask("Confirm Removal?", "nN") {
  212. return nil
  213. }
  214. err = pacman.CleanRemove(hanging)
  215. }
  216. return
  217. }