dep_graph.go 20 KB

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