commit
911957d6f3
1 changed files with 48 additions and 37 deletions
|
|
@ -22,14 +22,17 @@ type
|
||||||
backend*: string
|
backend*: string
|
||||||
|
|
||||||
TPackage* = object
|
TPackage* = object
|
||||||
|
# Required fields in a package.
|
||||||
name*: string
|
name*: string
|
||||||
version*: string
|
url*: string # Download location.
|
||||||
license*: string
|
license*: string
|
||||||
url*: string
|
|
||||||
dvcsTag*: string
|
|
||||||
downloadMethod*: string
|
downloadMethod*: string
|
||||||
tags*: seq[string]
|
|
||||||
description*: string
|
description*: string
|
||||||
|
tags*: seq[string] # Even if empty, always a valid non nil seq. \
|
||||||
|
# From here on, optional fields set to the emtpy string if not available.
|
||||||
|
version*: string
|
||||||
|
dvcsTag*: string
|
||||||
|
web*: string # Info url for humans.
|
||||||
|
|
||||||
proc initPackageInfo(): TPackageInfo =
|
proc initPackageInfo(): TPackageInfo =
|
||||||
result.mypath = ""
|
result.mypath = ""
|
||||||
|
|
@ -137,55 +140,61 @@ proc readPackageInfo*(path: string): TPackageInfo =
|
||||||
raise newException(EInvalidValue, "Cannot open package info: " & path)
|
raise newException(EInvalidValue, "Cannot open package info: " & path)
|
||||||
validatePackageInfo(result, path)
|
validatePackageInfo(result, path)
|
||||||
|
|
||||||
proc optionalField(obj: PJsonNode, name: string): string =
|
proc optionalField(obj: PJsonNode, name: string, default = ""): string =
|
||||||
|
## Queries ``obj`` for the optional ``name`` string.
|
||||||
|
##
|
||||||
|
## Returns the value of ``name`` if it is a valid string, or aborts execution
|
||||||
|
## if the field exists but is not of string type. If ``name`` is not present,
|
||||||
|
## returns ``default``.
|
||||||
if existsKey(obj, name):
|
if existsKey(obj, name):
|
||||||
if obj[name].kind == JString:
|
if obj[name].kind == JString:
|
||||||
return obj[name].str
|
return obj[name].str
|
||||||
else:
|
else:
|
||||||
quit("Corrupted packages.json file. " & name & " field is of unexpected type.")
|
quit("Corrupted packages.json file. " & name & " field is of unexpected type.")
|
||||||
else: return ""
|
else: return default
|
||||||
|
|
||||||
proc requiredField(obj: PJsonNode, name: string): string =
|
proc requiredField(obj: PJsonNode, name: string): string =
|
||||||
if existsKey(obj, name):
|
## Queries ``obj`` for the required ``name`` string.
|
||||||
if obj[name].kind == JString:
|
##
|
||||||
return obj[name].str
|
## Aborts execution if the field does not exist or is of invalid json type.
|
||||||
else:
|
result = optionalField(obj, name, nil)
|
||||||
quit("Corrupted packages.json file. " & name & " field is of unexpected type.")
|
if result == nil:
|
||||||
else:
|
|
||||||
quit("Package in packages.json file does not contain a " & name & " field.")
|
quit("Package in packages.json file does not contain a " & name & " field.")
|
||||||
|
|
||||||
|
proc fromJson(obj: PJSonNode): TPackage =
|
||||||
|
## Constructs a TPackage object from a JSON node.
|
||||||
|
##
|
||||||
|
## Aborts execution if the JSON node doesn't contain the required fields.
|
||||||
|
result.name = obj.requiredField("name")
|
||||||
|
result.version = obj.optionalField("version")
|
||||||
|
result.url = obj.requiredField("url")
|
||||||
|
result.downloadMethod = obj.requiredField("method")
|
||||||
|
result.dvcsTag = obj.optionalField("dvcs-tag")
|
||||||
|
result.license = obj.requiredField("license")
|
||||||
|
result.tags = @[]
|
||||||
|
for t in obj["tags"]:
|
||||||
|
result.tags.add(t.str)
|
||||||
|
result.description = obj.requiredField("description")
|
||||||
|
result.web = obj.optionalField("web")
|
||||||
|
|
||||||
proc getPackage*(pkg: string, packagesPath: string, resPkg: var TPackage): bool =
|
proc getPackage*(pkg: string, packagesPath: string, resPkg: var TPackage): bool =
|
||||||
|
## Searches ``packagesPath`` file saving into ``resPkg`` the found package.
|
||||||
|
##
|
||||||
|
## Pass in ``pkg`` the name of the package you are searching for. As
|
||||||
|
## convenience the proc returns a boolean specifying if the ``resPkg`` was
|
||||||
|
## successfully filled with good data.
|
||||||
let packages = parseFile(packagesPath)
|
let packages = parseFile(packagesPath)
|
||||||
for p in packages:
|
for p in packages:
|
||||||
if p["name"].str != pkg: continue
|
if p["name"].str == pkg:
|
||||||
resPkg.name = pkg
|
resPkg = p.fromJson()
|
||||||
resPkg.url = p.requiredField("url")
|
return true
|
||||||
resPkg.version = p.optionalField("version")
|
|
||||||
resPkg.downloadMethod = p.requiredField("method")
|
|
||||||
resPkg.dvcsTag = p.optionalField("dvcs-tag")
|
|
||||||
resPkg.license = p.requiredField("license")
|
|
||||||
resPkg.tags = @[]
|
|
||||||
for t in p["tags"]:
|
|
||||||
resPkg.tags.add(t.str)
|
|
||||||
resPkg.description = p.requiredField("description")
|
|
||||||
return true
|
|
||||||
return false
|
|
||||||
|
|
||||||
proc getPackageList*(packagesPath: string): seq[TPackage] =
|
proc getPackageList*(packagesPath: string): seq[TPackage] =
|
||||||
|
## Returns the list of packages found at the specified path.
|
||||||
result = @[]
|
result = @[]
|
||||||
let packages = parseFile(packagesPath)
|
let packages = parseFile(packagesPath)
|
||||||
for p in packages:
|
for p in packages:
|
||||||
var pkg: TPackage
|
let pkg: TPackage = p.fromJson()
|
||||||
pkg.name = p.requiredField("name")
|
|
||||||
pkg.version = p.optionalField("version")
|
|
||||||
pkg.url = p.requiredField("url")
|
|
||||||
pkg.downloadMethod = p.requiredField("method")
|
|
||||||
pkg.dvcsTag = p.optionalField("dvcs-tag")
|
|
||||||
pkg.license = p.requiredField("license")
|
|
||||||
pkg.tags = @[]
|
|
||||||
for t in p["tags"]:
|
|
||||||
pkg.tags.add(t.str)
|
|
||||||
pkg.description = p.requiredField("description")
|
|
||||||
result.add(pkg)
|
result.add(pkg)
|
||||||
|
|
||||||
proc findBabelFile*(dir: string): string =
|
proc findBabelFile*(dir: string): string =
|
||||||
|
|
@ -245,3 +254,5 @@ proc echoPackage*(pkg: TPackage) =
|
||||||
echo(" tags: " & pkg.tags.join(", "))
|
echo(" tags: " & pkg.tags.join(", "))
|
||||||
echo(" description: " & pkg.description)
|
echo(" description: " & pkg.description)
|
||||||
echo(" license: " & pkg.license)
|
echo(" license: " & pkg.license)
|
||||||
|
if pkg.web.len > 0:
|
||||||
|
echo(" website: " & pkg.web)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue