Add command to list installed packages.

This commit is contained in:
Hitesh Jasani 2014-12-31 17:53:18 -05:00
commit 1bc9953086

View file

@ -4,6 +4,8 @@
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,
@ -22,11 +24,11 @@ type
TActionType = enum TActionType = enum
ActionNil, ActionUpdate, ActionInit, ActionInstall, ActionSearch, ActionNil, ActionUpdate, ActionInit, ActionInstall, ActionSearch,
ActionList, ActionBuild, ActionPath, ActionUninstall ActionList, ActionBuild, ActionPath, ActionUninstall, ActionListInstalled
TAction = object TAction = object
case typ: TActionType case typ: TActionType
of ActionNil, ActionList, ActionBuild: nil of ActionNil, ActionList, ActionBuild, ActionListInstalled: 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:
@ -55,6 +57,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.
path pkgname ... Shows absolute path to the installed packages path pkgname ... Shows absolute path to the installed packages
specified. specified.
@ -151,6 +154,8 @@ 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 = @[]
@ -175,7 +180,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: of ActionList, ActionBuild, ActionListInstalled:
writeHelp() writeHelp()
of cmdLongOption, cmdShortOption: of cmdLongOption, cmdShortOption:
case key case key
@ -663,6 +668,29 @@ 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) =
let pkgsDir = options.getNimbleDir() / "pkgs"
if not existsDir(pkgsDir):
raise newException(ENimble, "Nimble dir not installed.")
var h = initTable[string, seq[string]]()
for kind, path in walkDir(pkgsDir):
let (dir, name, ext) = splitFile(path)
let r = split(name & ext, '-')
if not h.hasKey(r[0]):
h[r[0]] = @[]
var s = h[r[0]]
add(s, r[1])
h[r[0]] = s
for k in keys(h):
terminal.setForegroundColor(terminal.fgGreen)
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]
proc listPaths(options: TOptions) = proc listPaths(options: TOptions) =
@ -815,6 +843,8 @@ proc doAction(options: TOptions) =
search(options) search(options)
of ActionList: of ActionList:
list(options) list(options)
of ActionListInstalled:
listInstalled(options)
of ActionPath: of ActionPath:
listPaths(options) listPaths(options)
of ActionBuild: of ActionBuild: