Make listInstalled available under the list command.

This commit is contained in:
Hitesh Jasani 2015-01-03 13:27:27 -05:00
commit a8039a7821

View file

@ -4,8 +4,6 @@
import httpclient, parseopt, os, strutils, osproc, pegs, tables, parseutils, import httpclient, parseopt, os, strutils, osproc, pegs, tables, parseutils,
strtabs, json, algorithm, sets strtabs, json, algorithm, sets
from terminal import nil
from sequtils import toSeq from sequtils import toSeq
import nimblepkg/packageinfo, nimblepkg/version, nimblepkg/tools, import nimblepkg/packageinfo, nimblepkg/version, nimblepkg/tools,
@ -18,17 +16,18 @@ type
TOptions = object TOptions = object
forcePrompts: TForcePrompt forcePrompts: TForcePrompt
queryVersions: bool queryVersions: bool
queryInstalled: bool
action: TAction action: TAction
config: TConfig config: TConfig
nimbleData: PJsonNode ## Nimbledata.json nimbleData: PJsonNode ## Nimbledata.json
TActionType = enum TActionType = enum
ActionNil, ActionUpdate, ActionInit, ActionInstall, ActionSearch, ActionNil, ActionUpdate, ActionInit, ActionInstall, ActionSearch,
ActionList, ActionBuild, ActionPath, ActionUninstall, ActionListInstalled ActionList, ActionBuild, ActionPath, ActionUninstall
TAction = object TAction = object
case typ: TActionType case typ: TActionType
of ActionNil, ActionList, ActionBuild, ActionListInstalled: nil of ActionNil, ActionList, ActionBuild: nil
of ActionUpdate: of ActionUpdate:
optionalURL: string # Overrides default package list. optionalURL: string # Overrides default package list.
of ActionInstall, ActionPath, ActionUninstall: of ActionInstall, ActionPath, ActionUninstall:
@ -57,7 +56,7 @@ Commands:
search [--ver] pkg/tag Searches for a specified package. Search is search [--ver] pkg/tag Searches for a specified package. Search is
performed by tag and by name. performed by tag and by name.
list [--ver] Lists all packages. list [--ver] Lists all packages.
installed Lists all installed packages and versions. [-i, --installed] Lists all installed packages.
path pkgname ... Shows absolute path to the installed packages path pkgname ... Shows absolute path to the installed packages
specified. specified.
@ -154,8 +153,6 @@ proc parseCmdLine(): TOptions =
result.action.search = @[] result.action.search = @[]
of "list": of "list":
result.action.typ = ActionList result.action.typ = ActionList
of "installed":
result.action.typ = ActionListInstalled
of "uninstall", "remove", "delete", "del", "rm": of "uninstall", "remove", "delete", "del", "rm":
result.action.typ = ActionUninstall result.action.typ = ActionUninstall
result.action.packages = @[] result.action.packages = @[]
@ -180,7 +177,7 @@ proc parseCmdLine(): TOptions =
if result.action.projName != "": if result.action.projName != "":
raise newException(ENimble, "Can only initialize one package at a time.") raise newException(ENimble, "Can only initialize one package at a time.")
result.action.projName = key result.action.projName = key
of ActionList, ActionBuild, ActionListInstalled: of ActionList, ActionBuild:
writeHelp() writeHelp()
of cmdLongOption, cmdShortOption: of cmdLongOption, cmdShortOption:
case key case key
@ -189,6 +186,7 @@ proc parseCmdLine(): TOptions =
of "accept", "y": result.forcePrompts = ForcePromptYes of "accept", "y": result.forcePrompts = ForcePromptYes
of "reject", "n": result.forcePrompts = ForcePromptNo of "reject", "n": result.forcePrompts = ForcePromptNo
of "ver": result.queryVersions = true of "ver": result.queryVersions = true
of "installed", "i": result.queryInstalled = true
of cmdEnd: assert(false) # cannot happen of cmdEnd: assert(false) # cannot happen
if result.action.typ == ActionNil: if result.action.typ == ActionNil:
writeHelp() writeHelp()
@ -668,28 +666,19 @@ proc list(options: TOptions) =
echoPackageVersions(pkg) echoPackageVersions(pkg)
echo(" ") echo(" ")
proc c_printf(frmt: cstring) {.importc: "printf", header: "<stdio.h>",
varargs.}
proc listInstalled(options: TOptions) = proc listInstalled(options: TOptions) =
let pkgsDir = options.getNimbleDir() / "pkgs"
if not existsDir(pkgsDir):
raise newException(ENimble, "Nimble dir not installed.")
var h = initTable[string, seq[string]]() var h = initTable[string, seq[string]]()
for kind, path in walkDir(pkgsDir): let pkgs = getInstalledPkgs(options.getPkgsDir())
let (dir, name, ext) = splitFile(path) for x in pkgs.items():
let r = split(name & ext, '-') let
if not h.hasKey(r[0]): pName = x.pkginfo.name
h[r[0]] = @[] pVer = x.pkginfo.version
var s = h[r[0]] if not h.hasKey(pName): h[pName] = @[]
add(s, r[1]) var s = h[pName]
h[r[0]] = s add(s, pVer)
h[pName] = s
for k in keys(h): for k in keys(h):
terminal.setForegroundColor(terminal.fgGreen) echo k & " [" & h[k].join(", ") & "]"
c_printf("%20s", k)
terminal.resetAttributes()
c_printf(" [%s]\n", h[k].join(", "))
terminal.resetAttributes()
type VersionAndPath = tuple[version: TVersion, path: string] type VersionAndPath = tuple[version: TVersion, path: string]
@ -842,9 +831,8 @@ proc doAction(options: TOptions) =
of ActionSearch: of ActionSearch:
search(options) search(options)
of ActionList: of ActionList:
list(options) if options.queryInstalled: listInstalled(options)
of ActionListInstalled: else: list(options)
listInstalled(options)
of ActionPath: of ActionPath:
listPaths(options) listPaths(options)
of ActionBuild: of ActionBuild: