Better error message when an installed package fails validation.
Fixes #175.
This commit is contained in:
parent
ac9e6f00d1
commit
98045ea39c
1 changed files with 49 additions and 24 deletions
|
|
@ -25,6 +25,9 @@ type
|
||||||
|
|
||||||
NimbleFile* = string
|
NimbleFile* = string
|
||||||
|
|
||||||
|
ValidationError* = object of NimbleError
|
||||||
|
warnInstalled*: bool # Determines whether to show a warning for installed pkgs
|
||||||
|
|
||||||
proc initPackageInfo(path: string): PackageInfo =
|
proc initPackageInfo(path: string): PackageInfo =
|
||||||
result.mypath = path
|
result.mypath = path
|
||||||
# reasonable default:
|
# reasonable default:
|
||||||
|
|
@ -45,6 +48,10 @@ proc initPackageInfo(path: string): PackageInfo =
|
||||||
result.binDir = ""
|
result.binDir = ""
|
||||||
result.backend = "c"
|
result.backend = "c"
|
||||||
|
|
||||||
|
proc newValidationError(msg: string, warnInstalled: bool): ref ValidationError =
|
||||||
|
result = newException(ValidationError, msg)
|
||||||
|
result.warnInstalled = warnInstalled
|
||||||
|
|
||||||
proc validatePackageName*(name: string) =
|
proc validatePackageName*(name: string) =
|
||||||
## Raises an error if specified package name contains invalid characters.
|
## Raises an error if specified package name contains invalid characters.
|
||||||
##
|
##
|
||||||
|
|
@ -53,20 +60,22 @@ proc validatePackageName*(name: string) =
|
||||||
if name.len == 0: return
|
if name.len == 0: return
|
||||||
|
|
||||||
if name[0] in {'0'..'9'}:
|
if name[0] in {'0'..'9'}:
|
||||||
raise newException(NimbleError,
|
raise newValidationError(name &
|
||||||
"Invalid package name: cannot begin with " & name[0])
|
"\"$1\" is an invalid package name: cannot begin with $2" %
|
||||||
|
[name, $name[0]], true)
|
||||||
|
|
||||||
var prevWasUnderscore = false
|
var prevWasUnderscore = false
|
||||||
for c in name:
|
for c in name:
|
||||||
case c
|
case c
|
||||||
of '_':
|
of '_':
|
||||||
if prevWasUnderscore:
|
if prevWasUnderscore:
|
||||||
raise newException(NimbleError,
|
raise newValidationError(
|
||||||
"Invalid package name: cannot contain \"__\"")
|
"$1 is an invalid package name: cannot contain \"__\"" % name, true)
|
||||||
prevWasUnderscore = true
|
prevWasUnderscore = true
|
||||||
of AllChars - IdentChars:
|
of AllChars - IdentChars:
|
||||||
raise newException(NimbleError,
|
raise newValidationError(
|
||||||
"Invalid package name: cannot contain '$1'" % $c)
|
"$1 is an invalid package name: cannot contain '$2'" % [name, $c],
|
||||||
|
true)
|
||||||
else:
|
else:
|
||||||
prevWasUnderscore = false
|
prevWasUnderscore = false
|
||||||
|
|
||||||
|
|
@ -82,36 +91,36 @@ proc toValidPackageName*(name: string): string =
|
||||||
proc validateVersion*(ver: string) =
|
proc validateVersion*(ver: string) =
|
||||||
for c in ver:
|
for c in ver:
|
||||||
if c notin ({'.'} + Digits):
|
if c notin ({'.'} + Digits):
|
||||||
raise newException(NimbleError,
|
raise newValidationError(
|
||||||
"Version may only consist of numbers and the '.' character " &
|
"Version may only consist of numbers and the '.' character " &
|
||||||
"but found '" & c & "'.")
|
"but found '" & c & "'.", false)
|
||||||
|
|
||||||
proc validatePackageInfo(pkgInfo: PackageInfo, path: string) =
|
proc validatePackageInfo(pkgInfo: PackageInfo, path: string) =
|
||||||
if pkgInfo.name == "":
|
if pkgInfo.name == "":
|
||||||
raise newException(NimbleError, "Incorrect .nimble file: " & path &
|
raise newValidationError("Incorrect .nimble file: " & path &
|
||||||
" does not contain a name field.")
|
" does not contain a name field.", false)
|
||||||
|
|
||||||
if pkgInfo.name.normalize != path.splitFile.name.normalize:
|
if pkgInfo.name.normalize != path.splitFile.name.normalize:
|
||||||
raise newException(NimbleError,
|
raise newValidationError(
|
||||||
"The .nimble file name must match name specified inside it.")
|
"The .nimble file name must match name specified inside it.", false)
|
||||||
|
|
||||||
if pkgInfo.version == "":
|
if pkgInfo.version == "":
|
||||||
raise newException(NimbleError, "Incorrect .nimble file: " & path &
|
raise newValidationError("Incorrect .nimble file: " & path &
|
||||||
" does not contain a version field.")
|
" does not contain a version field.", false)
|
||||||
|
|
||||||
if not pkgInfo.isMinimal:
|
if not pkgInfo.isMinimal:
|
||||||
if pkgInfo.author == "":
|
if pkgInfo.author == "":
|
||||||
raise newException(NimbleError, "Incorrect .nimble file: " & path &
|
raise newValidationError("Incorrect .nimble file: " & path &
|
||||||
" does not contain an author field.")
|
" does not contain an author field.", false)
|
||||||
if pkgInfo.description == "":
|
if pkgInfo.description == "":
|
||||||
raise newException(NimbleError, "Incorrect .nimble file: " & path &
|
raise newValidationError("Incorrect .nimble file: " & path &
|
||||||
" does not contain a description field.")
|
" does not contain a description field.", false)
|
||||||
if pkgInfo.license == "":
|
if pkgInfo.license == "":
|
||||||
raise newException(NimbleError, "Incorrect .nimble file: " & path &
|
raise newValidationError("Incorrect .nimble file: " & path &
|
||||||
" does not contain a license field.")
|
" does not contain a license field.", false)
|
||||||
if pkgInfo.backend notin ["c", "cc", "objc", "cpp", "js"]:
|
if pkgInfo.backend notin ["c", "cc", "objc", "cpp", "js"]:
|
||||||
raise newException(NimbleError, "'" & pkgInfo.backend &
|
raise newValidationError("'" & pkgInfo.backend &
|
||||||
"' is an invalid backend.")
|
"' is an invalid backend.", false)
|
||||||
|
|
||||||
validateVersion(pkgInfo.version)
|
validateVersion(pkgInfo.version)
|
||||||
|
|
||||||
|
|
@ -225,7 +234,7 @@ proc getNameVersion*(pkgpath: string): tuple[name, version: string] =
|
||||||
break
|
break
|
||||||
|
|
||||||
proc readPackageInfo*(nf: NimbleFile; onlyMinimalInfo=false): PackageInfo =
|
proc readPackageInfo*(nf: NimbleFile; onlyMinimalInfo=false): PackageInfo =
|
||||||
## Reads package info from the specifed Nimble file.
|
## Reads package info from the specified Nimble file.
|
||||||
##
|
##
|
||||||
## Attempts to read it using the "old" Nimble ini format first, if that
|
## Attempts to read it using the "old" Nimble ini format first, if that
|
||||||
## fails attempts to evaluate it as a nimscript file.
|
## fails attempts to evaluate it as a nimscript file.
|
||||||
|
|
@ -382,7 +391,23 @@ proc getInstalledPkgs*(libsDir: string):
|
||||||
let nimbleFile = findNimbleFile(path, false)
|
let nimbleFile = findNimbleFile(path, false)
|
||||||
if nimbleFile != "":
|
if nimbleFile != "":
|
||||||
let meta = readMetaData(path)
|
let meta = readMetaData(path)
|
||||||
result.add((readPackageInfo(nimbleFile, true), meta))
|
try:
|
||||||
|
result.add((readPackageInfo(nimbleFile, true), meta))
|
||||||
|
except ValidationError:
|
||||||
|
let exc = (ref ValidationError)(getCurrentException())
|
||||||
|
if exc.warnInstalled:
|
||||||
|
echo("WARNING: Unable to read package info for " & path & "\n" &
|
||||||
|
" Package did not pass validation: " & exc.msg)
|
||||||
|
else:
|
||||||
|
exc.msg = "Unable to read package info for " & path & "\n" &
|
||||||
|
" Package did not pass validation: " & exc.msg
|
||||||
|
raise exc
|
||||||
|
except:
|
||||||
|
let exc = getCurrentException()
|
||||||
|
exc.msg = "Unable to read package info for " & path & "\n" &
|
||||||
|
" Error: " & exc.msg
|
||||||
|
raise exc
|
||||||
|
|
||||||
|
|
||||||
proc findPkg*(pkglist: seq[tuple[pkginfo: PackageInfo, meta: MetaData]],
|
proc findPkg*(pkglist: seq[tuple[pkginfo: PackageInfo, meta: MetaData]],
|
||||||
dep: PkgTuple,
|
dep: PkgTuple,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue