aboutsummaryrefslogtreecommitdiff
path: root/src/feature/localquery.nim
blob: 1d58bdc0bbc6af90fa2801b2f0a0730df48af50a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import
  algorithm, options, sequtils, sets, strutils, sugar, tables,
  "../args", "../common", "../config", "../format", "../package", "../pacman", "../utils",
  "../wrapper/alpm"

proc handleQueryOrphans*(args: seq[Argument], config: Config): int =
  let (installed, orphans, _, alternatives) = withAlpmConfig(config, false, handle, dbs, errors):
    for e in errors: printError(config.color, e)
    queryUnrequired(handle, true, false, initSet[string]())

  let targets = args.packageTargets(false)

  proc isOrphanOrNotFound(reference: PackageReference): bool =
    for r in installed:
      if reference.isProvidedBy(r, true):
        return reference.name in orphans
    for name, references in alternatives:
      for r in references:
        let hasConstraint = r.constraint.isSome and not r.constraint.unsafeGet.impliedVersion
        if (not hasConstraint and reference.constraint.isNone and r.name == reference.name) or
          (hasConstraint and reference.isProvidedBy(r, true)):
          return name in orphans
    return true

  # Provide similar output for not installed packages
  let results = if targets.len > 0:
      targets.filter(t => (t.repo.isNone or t.repo == some("local")) and
        t.reference.isOrphanOrNotFound).map(t => $t)
    else:
      toSeq(orphans.items).sorted(cmp)

  if results.len > 0:
    let newArgs = args.filter(arg => not arg.isTarget and
      not arg.matchOption(%%%"unrequired") and
      not arg.matchOption(%%%"deps")) &
      results.map(r => (r, none(string), ArgumentType.target))
    pacmanExec(false, config.color, newArgs)
  elif targets.len == 0:
    0
  else:
    1