Packages can now be searched by tags and name.
This commit is contained in:
parent
d9d1098b5e
commit
c1d216a8f4
2 changed files with 64 additions and 4 deletions
47
babel.nim
47
babel.nim
|
|
@ -4,7 +4,7 @@ import packageinfo
|
||||||
|
|
||||||
type
|
type
|
||||||
TActionType = enum
|
TActionType = enum
|
||||||
ActionNil, ActionUpdate, ActionInstall
|
ActionNil, ActionUpdate, ActionInstall, ActionSearch
|
||||||
|
|
||||||
TAction = object
|
TAction = object
|
||||||
case typ: TActionType
|
case typ: TActionType
|
||||||
|
|
@ -13,14 +13,21 @@ type
|
||||||
optionalURL: string # Overrides default package list.
|
optionalURL: string # Overrides default package list.
|
||||||
of ActionInstall:
|
of ActionInstall:
|
||||||
optionalName: seq[string] # When this is @[], installs package from current dir.
|
optionalName: seq[string] # When this is @[], installs package from current dir.
|
||||||
|
of ActionSearch:
|
||||||
|
search: seq[string] # Search string.
|
||||||
|
byTag: bool
|
||||||
|
|
||||||
const
|
const
|
||||||
help = """
|
help = """
|
||||||
Usage: babel COMMAND
|
Usage: babel COMMAND [opts]
|
||||||
|
|
||||||
Commands:
|
Commands:
|
||||||
install Installs a list of packages.
|
install Installs a list of packages.
|
||||||
update Updates package list. A package list URL can be optionally specificed.
|
update Updates package list. A package list URL can be optionally specificed.
|
||||||
|
search Searches for a specified package.
|
||||||
|
|
||||||
|
Search:
|
||||||
|
--tags Searches by tags, otherwise by name.
|
||||||
"""
|
"""
|
||||||
babelVersion = "0.1.0"
|
babelVersion = "0.1.0"
|
||||||
defaultPackageURL = "https://github.com/nimrod-code/packages/raw/master/packages.json"
|
defaultPackageURL = "https://github.com/nimrod-code/packages/raw/master/packages.json"
|
||||||
|
|
@ -46,6 +53,10 @@ proc parseCmdLine(): TAction =
|
||||||
of "update":
|
of "update":
|
||||||
result.typ = ActionUpdate
|
result.typ = ActionUpdate
|
||||||
result.optionalURL = ""
|
result.optionalURL = ""
|
||||||
|
of "search":
|
||||||
|
result.typ = ActionSearch
|
||||||
|
result.search = @[]
|
||||||
|
result.byTag = false
|
||||||
else: writeHelp()
|
else: writeHelp()
|
||||||
else:
|
else:
|
||||||
case result.typ
|
case result.typ
|
||||||
|
|
@ -55,10 +66,16 @@ proc parseCmdLine(): TAction =
|
||||||
result.optionalName.add(key)
|
result.optionalName.add(key)
|
||||||
of ActionUpdate:
|
of ActionUpdate:
|
||||||
result.optionalURL = key
|
result.optionalURL = key
|
||||||
|
of ActionSearch:
|
||||||
|
result.search.add(key)
|
||||||
of cmdLongOption, cmdShortOption:
|
of cmdLongOption, cmdShortOption:
|
||||||
case key
|
case key
|
||||||
of "help", "h": writeHelp()
|
of "help", "h": writeHelp()
|
||||||
of "version", "v": writeVersion()
|
of "version", "v": writeVersion()
|
||||||
|
of "tags", "t":
|
||||||
|
case result.typ
|
||||||
|
of ActionSearch: result.byTag = true
|
||||||
|
else: writeHelp()
|
||||||
of cmdEnd: assert(false) # cannot happen
|
of cmdEnd: assert(false) # cannot happen
|
||||||
if result.typ == ActionNil:
|
if result.typ == ActionNil:
|
||||||
writeHelp()
|
writeHelp()
|
||||||
|
|
@ -197,6 +214,30 @@ proc install(packages: seq[String]) =
|
||||||
else:
|
else:
|
||||||
quit("Package not found.", QuitFailure)
|
quit("Package not found.", QuitFailure)
|
||||||
|
|
||||||
|
proc search(action: TAction) =
|
||||||
|
assert action.typ == ActionSearch
|
||||||
|
if action.search == @[]:
|
||||||
|
quit("Please specify a search string.", QuitFailure)
|
||||||
|
let pkgList = getPackageList(getBabelDir() / "packages.json")
|
||||||
|
var notFound = true
|
||||||
|
if action.byTag:
|
||||||
|
for pkg in pkgList:
|
||||||
|
for word in action.search:
|
||||||
|
if word in pkg.tags:
|
||||||
|
echoPackage(pkg)
|
||||||
|
echo(" ")
|
||||||
|
notFound = false
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
for pkg in pkgList:
|
||||||
|
if pkg.name in action.search:
|
||||||
|
echoPackage(pkg)
|
||||||
|
echo(" ")
|
||||||
|
notFound = false
|
||||||
|
|
||||||
|
if notFound:
|
||||||
|
echo("No package found.")
|
||||||
|
|
||||||
proc doAction(action: TAction) =
|
proc doAction(action: TAction) =
|
||||||
case action.typ
|
case action.typ
|
||||||
of ActionUpdate:
|
of ActionUpdate:
|
||||||
|
|
@ -206,6 +247,8 @@ proc doAction(action: TAction) =
|
||||||
update()
|
update()
|
||||||
of ActionInstall:
|
of ActionInstall:
|
||||||
install(action.optionalName)
|
install(action.optionalName)
|
||||||
|
of ActionSearch:
|
||||||
|
search(action)
|
||||||
of ActionNil:
|
of ActionNil:
|
||||||
assert false
|
assert false
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,5 +67,22 @@ proc getPackage*(pkg: string, packagesPath: string, resPkg: var TPackage): bool
|
||||||
return true
|
return true
|
||||||
return false
|
return false
|
||||||
|
|
||||||
|
proc getPackageList*(packagesPath: string): seq[TPackage] =
|
||||||
|
result = @[]
|
||||||
|
let packages = parseFile(packagesPath)
|
||||||
|
for p in packages:
|
||||||
|
var pkg: TPackage
|
||||||
|
pkg.name = p["name"].str
|
||||||
|
pkg.url = p["url"].str
|
||||||
|
pkg.downloadMethod = p["method"].str
|
||||||
|
pkg.tags = @[]
|
||||||
|
for t in p["tags"]:
|
||||||
|
pkg.tags.add(t.str)
|
||||||
|
pkg.description = p["description"].str
|
||||||
|
result.add(pkg)
|
||||||
|
|
||||||
|
proc echoPackage*(pkg: TPackage) =
|
||||||
|
echo(pkg.name & ":")
|
||||||
|
echo(" url: " & pkg.url & " (" & pkg.downloadMethod & ")")
|
||||||
|
echo(" tags: " & pkg.tags.join(", "))
|
||||||
|
echo(" description: " & pkg.description)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue