dep_graph.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. package dep
  2. import (
  3. "context"
  4. "fmt"
  5. "path/filepath"
  6. "strconv"
  7. aurc "github.com/Jguer/aur"
  8. alpm "github.com/Jguer/go-alpm/v2"
  9. gosrc "github.com/Morganamilo/go-srcinfo"
  10. mapset "github.com/deckarep/golang-set/v2"
  11. "github.com/leonelquinteros/gotext"
  12. "github.com/Jguer/yay/v12/pkg/db"
  13. "github.com/Jguer/yay/v12/pkg/dep/topo"
  14. "github.com/Jguer/yay/v12/pkg/intrange"
  15. aur "github.com/Jguer/yay/v12/pkg/query"
  16. "github.com/Jguer/yay/v12/pkg/settings"
  17. "github.com/Jguer/yay/v12/pkg/settings/exe"
  18. "github.com/Jguer/yay/v12/pkg/text"
  19. )
  20. const (
  21. sourceAUR = "aur"
  22. sourceCacheSRCINFO = "srcinfo"
  23. )
  24. type InstallInfo struct {
  25. Source Source
  26. Reason Reason
  27. Version string
  28. LocalVersion string
  29. SrcinfoPath *string
  30. AURBase *string
  31. SyncDBName *string
  32. IsGroup bool
  33. Upgrade bool
  34. Devel bool
  35. }
  36. func (i *InstallInfo) String() string {
  37. return fmt.Sprintf("InstallInfo{Source: %v, Reason: %v}", i.Source, i.Reason)
  38. }
  39. type (
  40. Reason int
  41. Source int
  42. )
  43. func (r Reason) String() string {
  44. return ReasonNames[r]
  45. }
  46. func (s Source) String() string {
  47. return SourceNames[s]
  48. }
  49. const (
  50. Explicit Reason = iota // 0
  51. Dep // 1
  52. MakeDep // 2
  53. CheckDep // 3
  54. )
  55. var ReasonNames = map[Reason]string{
  56. Explicit: gotext.Get("Explicit"),
  57. Dep: gotext.Get("Dependency"),
  58. MakeDep: gotext.Get("Make Dependency"),
  59. CheckDep: gotext.Get("Check Dependency"),
  60. }
  61. const (
  62. AUR Source = iota
  63. Sync
  64. Local
  65. SrcInfo
  66. Missing
  67. )
  68. var SourceNames = map[Source]string{
  69. AUR: gotext.Get("AUR"),
  70. Sync: gotext.Get("Sync"),
  71. Local: gotext.Get("Local"),
  72. SrcInfo: gotext.Get("SRCINFO"),
  73. Missing: gotext.Get("Missing"),
  74. }
  75. var bgColorMap = map[Source]string{
  76. AUR: "lightblue",
  77. Sync: "lemonchiffon",
  78. Local: "darkolivegreen1",
  79. Missing: "tomato",
  80. }
  81. var colorMap = map[Reason]string{
  82. Explicit: "black",
  83. Dep: "deeppink",
  84. MakeDep: "navyblue",
  85. CheckDep: "forestgreen",
  86. }
  87. type SourceHandler interface {
  88. Graph(ctx context.Context, graph *topo.Graph[string, *InstallInfo], targets []Target) (*topo.Graph[string, *InstallInfo], error)
  89. Test(target Target) bool
  90. }
  91. type Grapher struct {
  92. logger *text.Logger
  93. providerCache map[string][]aur.Pkg
  94. cfg *settings.Configuration
  95. dbExecutor db.Executor
  96. aurClient aurc.QueryClient
  97. cmdBuilder exe.ICmdBuilder
  98. fullGraph bool // If true, the graph will include all dependencies including already installed ones or repo
  99. noConfirm bool // If true, the graph will not prompt for confirmation
  100. noDeps bool // If true, the graph will not include dependencies
  101. noCheckDeps bool // If true, the graph will not include check dependencies
  102. needed bool // If true, the graph will only include packages that are not installed
  103. handlers map[string][]SourceHandler
  104. }
  105. func NewGrapher(dbExecutor db.Executor, cfg *settings.Configuration, aurCache aurc.QueryClient, cmdBuilder exe.ICmdBuilder,
  106. fullGraph, noConfirm, noDeps, noCheckDeps, needed bool,
  107. logger *text.Logger,
  108. ) *Grapher {
  109. return &Grapher{
  110. dbExecutor: dbExecutor,
  111. cfg: cfg,
  112. aurClient: aurCache,
  113. cmdBuilder: cmdBuilder,
  114. fullGraph: fullGraph,
  115. noConfirm: noConfirm,
  116. noDeps: noDeps,
  117. noCheckDeps: noCheckDeps,
  118. needed: needed,
  119. providerCache: make(map[string][]aurc.Pkg, 5),
  120. logger: logger,
  121. handlers: make(map[string][]SourceHandler),
  122. }
  123. }
  124. func NewGraph() *topo.Graph[string, *InstallInfo] {
  125. return topo.New[string, *InstallInfo]()
  126. }
  127. func (g *Grapher) RegisterSourceHandler(handler SourceHandler, source string) {
  128. g.handlers[source] = append(g.handlers[source], handler)
  129. }
  130. func (g *Grapher) GraphFromTargets(ctx context.Context,
  131. graph *topo.Graph[string, *InstallInfo], targets []string,
  132. ) (*topo.Graph[string, *InstallInfo], error) {
  133. if graph == nil {
  134. graph = NewGraph()
  135. }
  136. aurTargets := make([]string, 0, len(targets))
  137. srcinfoTargets := make([]string, 0)
  138. for _, targetString := range targets {
  139. target := ToTarget(targetString)
  140. switch target.DB {
  141. case "": // unspecified db
  142. if pkg := g.dbExecutor.SyncSatisfier(target.Name); pkg != nil {
  143. GraphSyncPkg(ctx, g.dbExecutor, graph, g.logger, pkg, nil)
  144. continue
  145. }
  146. groupPackages := g.dbExecutor.PackagesFromGroup(target.Name)
  147. if len(groupPackages) > 0 {
  148. dbName := groupPackages[0].DB().Name()
  149. g.GraphSyncGroup(ctx, graph, target.Name, dbName)
  150. continue
  151. }
  152. fallthrough
  153. case sourceAUR:
  154. aurTargets = append(aurTargets, target.Name)
  155. case sourceCacheSRCINFO:
  156. srcinfoTargets = append(srcinfoTargets, filepath.Join(g.cfg.BuildDir, target.Name))
  157. default:
  158. pkg, err := g.dbExecutor.SatisfierFromDB(target.Name, target.DB)
  159. if err != nil {
  160. return nil, err
  161. }
  162. if pkg != nil {
  163. GraphSyncPkg(ctx, g.dbExecutor, graph, g.logger, pkg, nil)
  164. continue
  165. }
  166. groupPackages, err := g.dbExecutor.PackagesFromGroupAndDB(target.Name, target.DB)
  167. if err != nil {
  168. return nil, err
  169. }
  170. if len(groupPackages) > 0 {
  171. g.GraphSyncGroup(ctx, graph, target.Name, target.DB)
  172. continue
  173. }
  174. g.logger.Errorln(gotext.Get("No package found for"), " ", target)
  175. }
  176. }
  177. var errA error
  178. graph, errA = g.GraphFromAUR(ctx, graph, aurTargets)
  179. if errA != nil {
  180. return nil, errA
  181. }
  182. graph, errS := g.GraphFromSrcInfoDirs(ctx, graph, srcinfoTargets)
  183. if errS != nil {
  184. return nil, errS
  185. }
  186. return graph, nil
  187. }
  188. func (g *Grapher) pickSrcInfoPkgs(pkgs []*aurc.Pkg) ([]*aurc.Pkg, error) {
  189. final := make([]*aurc.Pkg, 0, len(pkgs))
  190. for i := range pkgs {
  191. g.logger.Println(text.Magenta(strconv.Itoa(i+1)+" ") + text.Bold(pkgs[i].Name) +
  192. " " + text.Cyan(pkgs[i].Version))
  193. g.logger.Println(" " + pkgs[i].Description)
  194. }
  195. g.logger.Infoln(gotext.Get("Packages to exclude") + " (eg: \"1 2 3\", \"1-3\", \"^4\"):")
  196. numberBuf, err := g.logger.GetInput("", g.noConfirm)
  197. if err != nil {
  198. return nil, err
  199. }
  200. include, exclude, _, otherExclude := intrange.ParseNumberMenu(numberBuf)
  201. isInclude := len(exclude) == 0 && otherExclude.Cardinality() == 0
  202. for i := 1; i <= len(pkgs); i++ {
  203. target := i - 1
  204. if isInclude && !include.Get(i) {
  205. final = append(final, pkgs[target])
  206. }
  207. if !isInclude && (exclude.Get(i)) {
  208. final = append(final, pkgs[target])
  209. }
  210. }
  211. return final, nil
  212. }
  213. func (g *Grapher) addAurPkgProvides(pkg *aurc.Pkg, graph *topo.Graph[string, *InstallInfo]) {
  214. for i := range pkg.Provides {
  215. depName, mod, version := splitDep(pkg.Provides[i])
  216. g.logger.Debugln(pkg.String() + " provides: " + depName)
  217. graph.Provides(depName, &alpm.Depend{
  218. Name: depName,
  219. Version: version,
  220. Mod: aurDepModToAlpmDep(mod),
  221. }, pkg.Name)
  222. }
  223. }
  224. func (g *Grapher) AddDepsForPkgs(ctx context.Context, pkgs []*aur.Pkg, graph *topo.Graph[string, *InstallInfo]) {
  225. for _, pkg := range pkgs {
  226. g.addDepNodes(ctx, pkg, graph)
  227. }
  228. }
  229. func (g *Grapher) addDepNodes(ctx context.Context, pkg *aur.Pkg, graph *topo.Graph[string, *InstallInfo]) {
  230. if len(pkg.MakeDepends) > 0 {
  231. g.addNodes(ctx, graph, pkg.Name, pkg.MakeDepends, MakeDep)
  232. }
  233. if !g.noDeps && len(pkg.Depends) > 0 {
  234. g.addNodes(ctx, graph, pkg.Name, pkg.Depends, Dep)
  235. }
  236. if !g.noCheckDeps && !g.noDeps && len(pkg.CheckDepends) > 0 {
  237. g.addNodes(ctx, graph, pkg.Name, pkg.CheckDepends, CheckDep)
  238. }
  239. }
  240. func GraphSyncPkg(ctx context.Context, dbExecutor db.Executor,
  241. graph *topo.Graph[string, *InstallInfo], logger *text.Logger,
  242. pkg alpm.IPackage, upgradeInfo *db.SyncUpgrade,
  243. ) *topo.Graph[string, *InstallInfo] {
  244. if graph == nil {
  245. graph = NewGraph()
  246. }
  247. graph.AddNode(pkg.Name())
  248. _ = pkg.Provides().ForEach(func(p *alpm.Depend) error {
  249. logger.Debugln(pkg.Name() + " provides: " + p.String())
  250. graph.Provides(p.Name, p, pkg.Name())
  251. return nil
  252. })
  253. dbName := pkg.DB().Name()
  254. info := &InstallInfo{
  255. Source: Sync,
  256. Reason: Explicit,
  257. Version: pkg.Version(),
  258. SyncDBName: &dbName,
  259. }
  260. if upgradeInfo == nil {
  261. if localPkg := dbExecutor.LocalPackage(pkg.Name()); localPkg != nil {
  262. info.Reason = Reason(localPkg.Reason())
  263. }
  264. } else {
  265. info.Upgrade = true
  266. info.Reason = Reason(upgradeInfo.Reason)
  267. info.LocalVersion = upgradeInfo.LocalVersion
  268. }
  269. validateAndSetNodeInfo(graph, pkg.Name(), &topo.NodeInfo[*InstallInfo]{
  270. Color: colorMap[info.Reason],
  271. Background: bgColorMap[info.Source],
  272. Value: info,
  273. })
  274. return graph
  275. }
  276. func (g *Grapher) GraphSyncGroup(ctx context.Context,
  277. graph *topo.Graph[string, *InstallInfo],
  278. groupName, dbName string,
  279. ) *topo.Graph[string, *InstallInfo] {
  280. if graph == nil {
  281. graph = NewGraph()
  282. }
  283. graph.AddNode(groupName)
  284. validateAndSetNodeInfo(graph, groupName, &topo.NodeInfo[*InstallInfo]{
  285. Color: colorMap[Explicit],
  286. Background: bgColorMap[Sync],
  287. Value: &InstallInfo{
  288. Source: Sync,
  289. Reason: Explicit,
  290. Version: "",
  291. SyncDBName: &dbName,
  292. IsGroup: true,
  293. },
  294. })
  295. return graph
  296. }
  297. func (g *Grapher) GraphAURTarget(ctx context.Context,
  298. graph *topo.Graph[string, *InstallInfo],
  299. pkg *aurc.Pkg, instalInfo *InstallInfo,
  300. ) *topo.Graph[string, *InstallInfo] {
  301. if graph == nil {
  302. graph = NewGraph()
  303. }
  304. graph.AddNode(pkg.Name)
  305. g.addAurPkgProvides(pkg, graph)
  306. validateAndSetNodeInfo(graph, pkg.Name, &topo.NodeInfo[*InstallInfo]{
  307. Color: colorMap[instalInfo.Reason],
  308. Background: bgColorMap[AUR],
  309. Value: instalInfo,
  310. })
  311. return graph
  312. }
  313. func (g *Grapher) GraphFromAUR(ctx context.Context,
  314. graph *topo.Graph[string, *InstallInfo],
  315. targets []string,
  316. ) (*topo.Graph[string, *InstallInfo], error) {
  317. if graph == nil {
  318. graph = NewGraph()
  319. }
  320. if len(targets) == 0 {
  321. return graph, nil
  322. }
  323. aurPkgs, errCache := g.aurClient.Get(ctx, &aurc.Query{By: aurc.Name, Needles: targets})
  324. if errCache != nil {
  325. g.logger.Errorln(errCache)
  326. }
  327. for i := range aurPkgs {
  328. pkg := &aurPkgs[i]
  329. if _, ok := g.providerCache[pkg.Name]; !ok {
  330. g.providerCache[pkg.Name] = []aurc.Pkg{*pkg}
  331. }
  332. }
  333. aurPkgsAdded := []*aurc.Pkg{}
  334. for _, target := range targets {
  335. if cachedProvidePkg, ok := g.providerCache[target]; ok {
  336. aurPkgs = cachedProvidePkg
  337. } else {
  338. var errA error
  339. aurPkgs, errA = g.aurClient.Get(ctx, &aurc.Query{By: aurc.Provides, Needles: []string{target}, Contains: true})
  340. if errA != nil {
  341. g.logger.Errorln(gotext.Get("Failed to find AUR package for"), " ", target, ":", errA)
  342. }
  343. }
  344. if len(aurPkgs) == 0 {
  345. g.logger.Errorln(gotext.Get("No AUR package found for"), " ", target)
  346. continue
  347. }
  348. aurPkg := &aurPkgs[0]
  349. if len(aurPkgs) > 1 {
  350. chosen := g.provideMenu(target, aurPkgs)
  351. aurPkg = chosen
  352. g.providerCache[target] = []aurc.Pkg{*aurPkg}
  353. }
  354. reason := Explicit
  355. if pkg := g.dbExecutor.LocalPackage(aurPkg.Name); pkg != nil {
  356. reason = Reason(pkg.Reason())
  357. if g.needed {
  358. if db.VerCmp(pkg.Version(), aurPkg.Version) >= 0 {
  359. g.logger.Warnln(gotext.Get("%s is up to date -- skipping", text.Cyan(pkg.Name()+"-"+pkg.Version())))
  360. continue
  361. }
  362. }
  363. }
  364. graph = g.GraphAURTarget(ctx, graph, aurPkg, &InstallInfo{
  365. AURBase: &aurPkg.PackageBase,
  366. Reason: reason,
  367. Source: AUR,
  368. Version: aurPkg.Version,
  369. })
  370. aurPkgsAdded = append(aurPkgsAdded, aurPkg)
  371. }
  372. g.AddDepsForPkgs(ctx, aurPkgsAdded, graph)
  373. return graph, nil
  374. }
  375. // Removes found deps from the deps mapset and returns the found deps.
  376. func (g *Grapher) findDepsFromAUR(ctx context.Context,
  377. deps mapset.Set[string],
  378. ) []aurc.Pkg {
  379. pkgsToAdd := make([]aurc.Pkg, 0, deps.Cardinality())
  380. if deps.Cardinality() == 0 {
  381. return []aurc.Pkg{}
  382. }
  383. missingNeedles := make([]string, 0, deps.Cardinality())
  384. for _, depString := range deps.ToSlice() {
  385. if _, ok := g.providerCache[depString]; !ok {
  386. depName, _, _ := splitDep(depString)
  387. missingNeedles = append(missingNeedles, depName)
  388. }
  389. }
  390. if len(missingNeedles) != 0 {
  391. g.logger.Debugln("deps to find", missingNeedles)
  392. // provider search is more demanding than a simple search
  393. // try to find name match if possible and then try to find provides.
  394. aurPkgs, errCache := g.aurClient.Get(ctx, &aurc.Query{
  395. By: aurc.Name, Needles: missingNeedles, Contains: false,
  396. })
  397. if errCache != nil {
  398. g.logger.Errorln(errCache)
  399. }
  400. for i := range aurPkgs {
  401. pkg := &aurPkgs[i]
  402. if deps.Contains(pkg.Name) {
  403. g.providerCache[pkg.Name] = append(g.providerCache[pkg.Name], *pkg)
  404. }
  405. for _, val := range pkg.Provides {
  406. if val == pkg.Name {
  407. continue
  408. }
  409. if deps.Contains(val) {
  410. g.providerCache[val] = append(g.providerCache[val], *pkg)
  411. }
  412. }
  413. }
  414. }
  415. for _, depString := range deps.ToSlice() {
  416. var aurPkgs []aurc.Pkg
  417. depName, _, _ := splitDep(depString)
  418. if cachedProvidePkg, ok := g.providerCache[depString]; ok {
  419. aurPkgs = cachedProvidePkg
  420. } else {
  421. var errA error
  422. aurPkgs, errA = g.aurClient.Get(ctx, &aurc.Query{By: aurc.Provides, Needles: []string{depName}, Contains: true})
  423. if errA != nil {
  424. g.logger.Errorln(gotext.Get("Failed to find AUR package for"), depString, ":", errA)
  425. }
  426. }
  427. // remove packages that don't satisfy the dependency
  428. for i := 0; i < len(aurPkgs); i++ {
  429. if !satisfiesAur(depString, &aurPkgs[i]) {
  430. aurPkgs = append(aurPkgs[:i], aurPkgs[i+1:]...)
  431. i--
  432. }
  433. }
  434. if len(aurPkgs) == 0 {
  435. g.logger.Errorln(gotext.Get("No AUR package found for"), " ", depString)
  436. continue
  437. }
  438. pkg := aurPkgs[0]
  439. if len(aurPkgs) > 1 {
  440. chosen := g.provideMenu(depString, aurPkgs)
  441. pkg = *chosen
  442. }
  443. g.providerCache[depString] = []aurc.Pkg{pkg}
  444. deps.Remove(depString)
  445. pkgsToAdd = append(pkgsToAdd, pkg)
  446. }
  447. return pkgsToAdd
  448. }
  449. func validateAndSetNodeInfo(graph *topo.Graph[string, *InstallInfo],
  450. node string, nodeInfo *topo.NodeInfo[*InstallInfo],
  451. ) {
  452. info := graph.GetNodeInfo(node)
  453. if info != nil && info.Value != nil {
  454. if info.Value.Reason < nodeInfo.Value.Reason {
  455. return // refuse to downgrade reason
  456. }
  457. if info.Value.Upgrade {
  458. return // refuse to overwrite an upgrade
  459. }
  460. }
  461. graph.SetNodeInfo(node, nodeInfo)
  462. }
  463. func (g *Grapher) addNodes(
  464. ctx context.Context,
  465. graph *topo.Graph[string, *InstallInfo],
  466. parentPkgName string,
  467. deps []string,
  468. depType Reason,
  469. ) {
  470. targetsToFind := mapset.NewThreadUnsafeSet(deps...)
  471. // Check if in graph already
  472. for _, depString := range targetsToFind.ToSlice() {
  473. depName, _, _ := splitDep(depString)
  474. if !graph.Exists(depName) && !graph.ProvidesExists(depName) {
  475. continue
  476. }
  477. if graph.Exists(depName) {
  478. if err := graph.DependOn(depName, parentPkgName); err != nil {
  479. g.logger.Warnln(depString, parentPkgName, err)
  480. }
  481. targetsToFind.Remove(depString)
  482. }
  483. if p := graph.GetProviderNode(depName); p != nil {
  484. if provideSatisfies(p.String(), depString, p.Version) {
  485. if err := graph.DependOn(p.Provider, parentPkgName); err != nil {
  486. g.logger.Warnln(p.Provider, parentPkgName, err)
  487. }
  488. targetsToFind.Remove(depString)
  489. }
  490. }
  491. }
  492. // Check installed
  493. for _, depString := range targetsToFind.ToSlice() {
  494. depName, _, _ := splitDep(depString)
  495. if !g.dbExecutor.LocalSatisfierExists(depString) {
  496. continue
  497. }
  498. if g.fullGraph {
  499. validateAndSetNodeInfo(
  500. graph,
  501. depName,
  502. &topo.NodeInfo[*InstallInfo]{Color: colorMap[depType], Background: bgColorMap[Local]})
  503. if err := graph.DependOn(depName, parentPkgName); err != nil {
  504. g.logger.Warnln(depName, parentPkgName, err)
  505. }
  506. }
  507. targetsToFind.Remove(depString)
  508. }
  509. // Check Sync
  510. for _, depString := range targetsToFind.ToSlice() {
  511. alpmPkg := g.dbExecutor.SyncSatisfier(depString)
  512. if alpmPkg == nil {
  513. continue
  514. }
  515. if err := graph.DependOn(alpmPkg.Name(), parentPkgName); err != nil {
  516. g.logger.Warnln("repo dep warn:", depString, parentPkgName, err)
  517. }
  518. dbName := alpmPkg.DB().Name()
  519. validateAndSetNodeInfo(
  520. graph,
  521. alpmPkg.Name(),
  522. &topo.NodeInfo[*InstallInfo]{
  523. Color: colorMap[depType],
  524. Background: bgColorMap[Sync],
  525. Value: &InstallInfo{
  526. Source: Sync,
  527. Reason: depType,
  528. Version: alpmPkg.Version(),
  529. SyncDBName: &dbName,
  530. },
  531. })
  532. if newDeps := alpmPkg.Depends().Slice(); len(newDeps) != 0 && g.fullGraph {
  533. newDepsSlice := make([]string, 0, len(newDeps))
  534. for _, newDep := range newDeps {
  535. newDepsSlice = append(newDepsSlice, newDep.Name)
  536. }
  537. g.addNodes(ctx, graph, alpmPkg.Name(), newDepsSlice, Dep)
  538. }
  539. targetsToFind.Remove(depString)
  540. }
  541. // Check AUR
  542. pkgsToAdd := g.findDepsFromAUR(ctx, targetsToFind)
  543. for i := range pkgsToAdd {
  544. aurPkg := &pkgsToAdd[i]
  545. if err := graph.DependOn(aurPkg.Name, parentPkgName); err != nil {
  546. g.logger.Warnln("aur dep warn:", aurPkg.Name, parentPkgName, err)
  547. }
  548. graph.SetNodeInfo(
  549. aurPkg.Name,
  550. &topo.NodeInfo[*InstallInfo]{
  551. Color: colorMap[depType],
  552. Background: bgColorMap[AUR],
  553. Value: &InstallInfo{
  554. Source: AUR,
  555. Reason: depType,
  556. AURBase: &aurPkg.PackageBase,
  557. Version: aurPkg.Version,
  558. },
  559. })
  560. g.addDepNodes(ctx, aurPkg, graph)
  561. }
  562. // Add missing to graph
  563. for _, depString := range targetsToFind.ToSlice() {
  564. depName, mod, ver := splitDep(depString)
  565. // no dep found. add as missing
  566. if err := graph.DependOn(depName, parentPkgName); err != nil {
  567. g.logger.Warnln("missing dep warn:", depString, parentPkgName, err)
  568. }
  569. graph.SetNodeInfo(depName, &topo.NodeInfo[*InstallInfo]{
  570. Color: colorMap[depType],
  571. Background: bgColorMap[Missing],
  572. Value: &InstallInfo{
  573. Source: Missing,
  574. Reason: depType,
  575. Version: fmt.Sprintf("%s%s", mod, ver),
  576. },
  577. })
  578. }
  579. }
  580. func (g *Grapher) provideMenu(dep string, options []aur.Pkg) *aur.Pkg {
  581. size := len(options)
  582. if size == 1 {
  583. return &options[0]
  584. }
  585. str := text.Bold(gotext.Get("There are %d providers available for %s:", size, dep))
  586. str += "\n"
  587. size = 1
  588. str += g.logger.SprintOperationInfo(gotext.Get("Repository AUR"), "\n ")
  589. for i := range options {
  590. str += fmt.Sprintf("%d) %s ", size, options[i].Name)
  591. size++
  592. }
  593. g.logger.OperationInfoln(str)
  594. for {
  595. g.logger.Println(gotext.Get("\nEnter a number (default=1): "))
  596. if g.noConfirm {
  597. g.logger.Println("1")
  598. return &options[0]
  599. }
  600. numberBuf, err := g.logger.GetInput("", false)
  601. if err != nil {
  602. g.logger.Errorln(err)
  603. break
  604. }
  605. if numberBuf == "" {
  606. return &options[0]
  607. }
  608. num, err := strconv.Atoi(numberBuf)
  609. if err != nil {
  610. g.logger.Errorln(gotext.Get("invalid number: %s", numberBuf))
  611. continue
  612. }
  613. if num < 1 || num >= size {
  614. g.logger.Errorln(gotext.Get("invalid value: %d is not between %d and %d",
  615. num, 1, size-1))
  616. continue
  617. }
  618. return &options[num-1]
  619. }
  620. return nil
  621. }
  622. func makeAURPKGFromSrcinfo(dbExecutor db.Executor, srcInfo *gosrc.Srcinfo) ([]*aur.Pkg, error) {
  623. pkgs := make([]*aur.Pkg, 0, 1)
  624. alpmArch, err := dbExecutor.AlpmArchitectures()
  625. if err != nil {
  626. return nil, err
  627. }
  628. alpmArch = append(alpmArch, "") // srcinfo assumes no value as ""
  629. getDesc := func(pkg *gosrc.Package) string {
  630. if pkg.Pkgdesc != "" {
  631. return pkg.Pkgdesc
  632. }
  633. return srcInfo.Pkgdesc
  634. }
  635. for i := range srcInfo.Packages {
  636. pkg := &srcInfo.Packages[i]
  637. pkgs = append(pkgs, &aur.Pkg{
  638. ID: 0,
  639. Name: pkg.Pkgname,
  640. PackageBaseID: 0,
  641. PackageBase: srcInfo.Pkgbase,
  642. Version: srcInfo.Version(),
  643. Description: getDesc(pkg),
  644. URL: pkg.URL,
  645. Depends: append(archStringToString(alpmArch, pkg.Depends),
  646. archStringToString(alpmArch, srcInfo.Package.Depends)...),
  647. MakeDepends: archStringToString(alpmArch, srcInfo.PackageBase.MakeDepends),
  648. CheckDepends: archStringToString(alpmArch, srcInfo.PackageBase.CheckDepends),
  649. Conflicts: append(archStringToString(alpmArch, pkg.Conflicts),
  650. archStringToString(alpmArch, srcInfo.Package.Conflicts)...),
  651. Provides: append(archStringToString(alpmArch, pkg.Provides),
  652. archStringToString(alpmArch, srcInfo.Package.Provides)...),
  653. Replaces: append(archStringToString(alpmArch, pkg.Replaces),
  654. archStringToString(alpmArch, srcInfo.Package.Replaces)...),
  655. OptDepends: []string{},
  656. Groups: pkg.Groups,
  657. License: pkg.License,
  658. Keywords: []string{},
  659. })
  660. }
  661. return pkgs, nil
  662. }
  663. func archStringToString(alpmArches []string, archString []gosrc.ArchString) []string {
  664. pkgs := make([]string, 0, len(archString))
  665. for _, arch := range archString {
  666. if db.ArchIsSupported(alpmArches, arch.Arch) {
  667. pkgs = append(pkgs, arch.Value)
  668. }
  669. }
  670. return pkgs
  671. }
  672. func aurDepModToAlpmDep(mod string) alpm.DepMod {
  673. switch mod {
  674. case "=":
  675. return alpm.DepModEq
  676. case ">=":
  677. return alpm.DepModGE
  678. case "<=":
  679. return alpm.DepModLE
  680. case ">":
  681. return alpm.DepModGT
  682. case "<":
  683. return alpm.DepModLT
  684. }
  685. return alpm.DepModAny
  686. }