- Converted types from the old T/P convention

- Deleted compat.nim
This commit is contained in:
Louis Berube 2014-12-31 10:13:23 -05:00
commit 521ee1f696
9 changed files with 303 additions and 302 deletions

View file

@ -1,29 +0,0 @@
# Copyright (C) Dominik Picheta. All rights reserved.
# BSD License. Look at license.txt for more info.
## This module contains additional code from the development version of
## Nimrod's standard library. These procs are required to be able to compile
## against the last stable release 0.9.4. Once 0.9.6 is release these procs
## will disappear.
import json
when false:
when not (NimrodPatch >= 5):
when not defined(`{}`):
proc `{}`*(node: PJsonNode, key: string): PJsonNode =
## Transverses the node and gets the given value. If any of the
## names does not exist, returns nil
result = node
if isNil(node): return nil
result = result[key]
when not defined(`{}=`):
proc `{}=`*(node: PJsonNode, names: varargs[string], value: PJsonNode) =
## Transverses the node and tries to set the value at the given location
## to `value` If any of the names are missing, they are added
var node = node
for i in 0..(names.len-2):
if isNil(node[names[i]]):
node[names[i]] = newJObject()
node = node[names[i]]
node[names[names.len-1]] = value

View file

@ -5,12 +5,12 @@ import parsecfg, streams, strutils, os
import tools, version, nimbletypes
type
TConfig* = object
Config* = object
nimbleDir*: string
chcp*: bool # Whether to change the code page in .cmd files on Win.
proc initConfig(): TConfig =
proc initConfig(): Config =
if getNimrodVersion() > newVersion("0.9.6"):
result.nimbleDir = getHomeDir() / ".nimble"
else:
@ -18,7 +18,7 @@ proc initConfig(): TConfig =
result.chcp = true
proc parseConfig*(): TConfig =
proc parseConfig*(): Config =
result = initConfig()
var confFile = getConfigDir() / "nimble" / "nimble.ini"
@ -49,8 +49,8 @@ proc parseConfig*(): TConfig =
of "chcp":
result.chcp = parseBool(e.value)
else:
raise newException(ENimble, "Unable to parse config file:" &
raise newException(NimbleError, "Unable to parse config file:" &
" Unknown key: " & e.key)
of cfgError:
raise newException(ENimble, "Unable to parse config file: " & e.msg)
raise newException(NimbleError, "Unable to parse config file: " & e.msg)
close(p)

View file

@ -6,44 +6,45 @@ import parseutils, os, osproc, strutils, tables, pegs
import packageinfo, version, tools, nimbletypes
type
TDownloadMethod* {.pure.} = enum
Git = "git", Hg = "hg"
DownloadMethod* {.pure.} = enum
git = "git", hg = "hg"
proc getSpecificDir(meth: TDownloadMethod): string =
proc getSpecificDir(meth: DownloadMethod): string =
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
".git"
of TDownloadMethod.Hg:
of DownloadMethod.hg:
".hg"
proc doCheckout(meth: TDownloadMethod, downloadDir, branch: string) =
proc doCheckout(meth: DownloadMethod, downloadDir, branch: string) =
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
cd downloadDir:
# Force is used here because local changes may appear straight after a
# clone has happened. Like in the case of git on Windows where it
# messes up the damn line endings.
doCmd("git checkout --force " & branch)
of TDownloadMethod.Hg:
of DownloadMethod.hg:
cd downloadDir:
doCmd("hg checkout " & branch)
proc doPull(meth: TDownloadMethod, downloadDir: string) =
proc doPull(meth: DownloadMethod, downloadDir: string) =
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
doCheckout(meth, downloadDir, "master")
cd downloadDir:
doCmd("git pull")
if existsFile(".gitmodules"):
doCmd("git submodule update")
of TDownloadMethod.Hg:
of DownloadMethod.hg:
doCheckout(meth, downloadDir, "default")
cd downloadDir:
doCmd("hg pull")
proc doClone(meth: TDownloadMethod, url, downloadDir: string, branch = "", tip = true) =
proc doClone(meth: DownloadMethod, url, downloadDir: string, branch = "",
tip = true) =
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
let
depthArg = if tip: "--depth 1 " else: ""
branchArg = if branch == "": "-b origin/master" else: "-b " & branch & " "
@ -60,28 +61,28 @@ proc doClone(meth: TDownloadMethod, url, downloadDir: string, branch = "", tip =
doCmd("git reset --hard FETCH_HEAD")
doCmd("git checkout --force " & branchArg)
doCmd("git submodule update --init --recursive")
of TDownloadMethod.Hg:
of DownloadMethod.hg:
let
tipArg = if tip: "-r tip " else: ""
branchArg = if branch == "": "" else: "-b " & branch & " "
doCmd("hg clone " & tipArg & branchArg & url & " " & downloadDir)
proc getTagsList(dir: string, meth: TDownloadMethod): seq[string] =
proc getTagsList(dir: string, meth: DownloadMethod): seq[string] =
cd dir:
var output = execProcess("git tag")
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
output = execProcess("git tag")
of TDownloadMethod.Hg:
of DownloadMethod.hg:
output = execProcess("hg tags")
if output.len > 0:
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
result = @[]
for i in output.splitLines():
if i == "": continue
result.add(i)
of TDownloadMethod.Hg:
of DownloadMethod.hg:
result = @[]
for i in output.splitLines():
if i == "": continue
@ -92,10 +93,10 @@ proc getTagsList(dir: string, meth: TDownloadMethod): seq[string] =
else:
result = @[]
proc getTagsListRemote*(url: string, meth: TDownloadMethod): seq[string] =
proc getTagsListRemote*(url: string, meth: DownloadMethod): seq[string] =
result = @[]
case meth
of TDownloadMethod.Git:
of DownloadMethod.git:
var (output, exitCode) = doCmdEx("git ls-remote --tags " & url)
if exitCode != QuitSuccess:
raise newException(OSError, "Unable to query remote tags for " & url &
@ -106,47 +107,47 @@ proc getTagsListRemote*(url: string, meth: TDownloadMethod): seq[string] =
let tag = i[start .. -1]
if not tag.endswith("^{}"): result.add(tag)
of TDownloadMethod.Hg:
of DownloadMethod.hg:
# http://stackoverflow.com/questions/2039150/show-tags-for-remote-hg-repository
raise newException(ValueError, "Hg doesn't support remote tag querying.")
proc getVersionList*(tags: seq[string]): Table[TVersion, string] =
proc getVersionList*(tags: seq[string]): Table[Version, string] =
# Returns: TTable of version -> git tag name
result = initTable[TVersion, string]()
result = initTable[Version, string]()
for tag in tags:
if tag != "":
let i = skipUntil(tag, Digits) # skip any chars before the version
# TODO: Better checking, tags can have any names. Add warnings and such.
result[newVersion(tag[i .. -1])] = tag
proc getDownloadMethod*(meth: string): TDownloadMethod =
proc getDownloadMethod*(meth: string): DownloadMethod =
case meth
of "git": return TDownloadMethod.Git
of "hg", "mercurial": return TDownloadMethod.Hg
of "git": return DownloadMethod.git
of "hg", "mercurial": return DownloadMethod.hg
else:
raise newException(ENimble, "Invalid download method: " & meth)
raise newException(NimbleError, "Invalid download method: " & meth)
proc getHeadName*(meth: TDownloadMethod): string =
proc getHeadName*(meth: DownloadMethod): string =
## Returns the name of the download method specific head. i.e. for git
## it's ``head`` for hg it's ``tip``.
case meth
of TDownloadMethod.Git: "head"
of TDownloadMethod.Hg: "tip"
of DownloadMethod.git: "head"
of DownloadMethod.hg: "tip"
proc checkUrlType*(url: string): TDownloadMethod =
proc checkUrlType*(url: string): DownloadMethod =
## Determines the download method based on the URL.
if doCmdEx("git ls-remote " & url).exitCode == QuitSuccess:
return TDownloadMethod.Git
return DownloadMethod.git
elif doCmdEx("hg identify " & url).exitCode == QuitSuccess:
return TDownloadMethod.Hg
return DownloadMethod.hg
else:
raise newException(ENimble, "Unable to identify url.")
raise newException(NimbleError, "Unable to identify url.")
proc isURL*(name: string): bool =
name.startsWith(peg" @'://' ")
proc doDownload*(url: string, downloadDir: string, verRange: PVersionRange,
downMethod: TDownloadMethod) =
proc doDownload*(url: string, downloadDir: string, verRange: VersionRangeRef,
downMethod: DownloadMethod) =
template getLatestByTag(meth: stmt): stmt {.dirty, immediate.} =
echo("Found tags...")
# Find latest version that fits our ``verRange``.
@ -164,7 +165,7 @@ proc doDownload*(url: string, downloadDir: string, verRange: PVersionRange,
## version range.
let pkginfo = getPkgInfo(downloadDir)
if pkginfo.version.newVersion notin verRange:
raise newException(ENimble,
raise newException(NimbleError,
"Downloaded package's version does not satisfy requested version " &
"range: wanted $1 got $2." %
[$verRange, $pkginfo.version])
@ -177,14 +178,14 @@ proc doDownload*(url: string, downloadDir: string, verRange: PVersionRange,
else:
# Mercurial requies a clone and checkout. The git clone operation is
# already fragmented into multiple steps so we just call doClone().
if downMethod == TDownloadMethod.Git:
if downMethod == DownloadMethod.git:
doClone(downMethod, url, downloadDir, $verRange.spe)
else:
doClone(downMethod, url, downloadDir, tip = false)
doCheckout(downMethod, downloadDir, $verRange.spe)
else:
case downMethod
of TDownloadMethod.Git:
of DownloadMethod.git:
# For Git we have to query the repo remotely for its tags. This is
# necessary as cloning with a --depth of 1 removes all tag info.
let versions = getTagsListRemote(url, downMethod).getVersionList()
@ -197,7 +198,7 @@ proc doDownload*(url: string, downloadDir: string, verRange: PVersionRange,
doClone(downMethod, url, downloadDir) # Grab HEAD.
verifyClone()
of TDownloadMethod.Hg:
of DownloadMethod.hg:
doClone(downMethod, url, downloadDir)
let versions = getTagsList(downloadDir, downMethod).getVersionList()
@ -208,10 +209,10 @@ proc doDownload*(url: string, downloadDir: string, verRange: PVersionRange,
verifyClone()
proc echoPackageVersions*(pkg: TPackage) =
proc echoPackageVersions*(pkg: Package) =
let downMethod = pkg.downloadMethod.getDownloadMethod()
case downMethod
of TDownloadMethod.Git:
of DownloadMethod.git:
try:
let versions = getTagsListRemote(pkg.url, downMethod).getVersionList()
if versions.len > 0:
@ -227,5 +228,6 @@ proc echoPackageVersions*(pkg: TPackage) =
echo(" versions: (No versions tagged in the remote repository)")
except OSError:
echo(getCurrentExceptionMsg())
of TDownloadMethod.Hg:
echo(" versions: (Remote tag retrieval not supported by " & pkg.downloadMethod & ")")
of DownloadMethod.hg:
echo(" versions: (Remote tag retrieval not supported by " &
pkg.downloadMethod & ")")

View file

@ -4,4 +4,4 @@
# recursive imports
type
ENimble* = object of Exception
NimbleError* = object of Exception

View file

@ -4,9 +4,9 @@ import parsecfg, json, streams, strutils, parseutils, os
import version, tools, nimbletypes
type
## Tuple containing package name and version range.
TPkgTuple* = tuple[name: string, ver: PVersionRange]
PkgTuple* = tuple[name: string, ver: VersionRangeRef]
TPackageInfo* = object
PackageInfo* = object
mypath*: string ## The path of this .nimble file
name*: string
version*: string
@ -19,12 +19,12 @@ type
installDirs*: seq[string]
installFiles*: seq[string]
installExt*: seq[string]
requires*: seq[TPkgTuple]
requires*: seq[PkgTuple]
bin*: seq[string]
srcDir*: string
backend*: string
TPackage* = object
Package* = object
# Required fields in a package.
name*: string
url*: string # Download location.
@ -37,10 +37,10 @@ type
dvcsTag*: string
web*: string # Info url for humans.
TMetadata* = object
MetaData* = object
url*: string
proc initPackageInfo(): TPackageInfo =
proc initPackageInfo(): PackageInfo =
result.mypath = ""
result.name = ""
result.version = ""
@ -58,31 +58,32 @@ proc initPackageInfo(): TPackageInfo =
result.srcDir = ""
result.backend = "c"
proc validatePackageInfo(pkgInfo: TPackageInfo, path: string) =
proc validatePackageInfo(pkgInfo: PackageInfo, path: string) =
if pkgInfo.name == "":
raise newException(ENimble, "Incorrect .nimble file: " & path &
raise newException(NimbleError, "Incorrect .nimble file: " & path &
" does not contain a name field.")
if pkgInfo.version == "":
raise newException(ENimble, "Incorrect .nimble file: " & path &
raise newException(NimbleError, "Incorrect .nimble file: " & path &
" does not contain a version field.")
if pkgInfo.author == "":
raise newException(ENimble, "Incorrect .nimble file: " & path &
raise newException(NimbleError, "Incorrect .nimble file: " & path &
" does not contain an author field.")
if pkgInfo.description == "":
raise newException(ENimble, "Incorrect .nimble file: " & path &
raise newException(NimbleError, "Incorrect .nimble file: " & path &
" does not contain a description field.")
if pkgInfo.license == "":
raise newException(ENimble, "Incorrect .nimble file: " & path &
raise newException(NimbleError, "Incorrect .nimble file: " & path &
" does not contain a license field.")
if pkgInfo.backend notin ["c", "cc", "objc", "cpp", "js"]:
raise newException(ENimble, "'" & pkgInfo.backend & "' is an invalid backend.")
raise newException(NimbleError, "'" & pkgInfo.backend &
"' is an invalid backend.")
for c in pkgInfo.version:
if c notin ({'.'} + Digits):
raise newException(ENimble,
raise newException(NimbleError,
"Version may only consist of numbers and the '.' character " &
"but found '" & c & "'.")
proc parseRequires(req: string): TPkgTuple =
proc parseRequires(req: string): PkgTuple =
try:
if ' ' in req:
var i = skipUntil(req, Whitespace)
@ -94,10 +95,10 @@ proc parseRequires(req: string): TPkgTuple =
result.ver = parseVersionRange(req[i .. -1])
else:
result.name = req.strip
result.ver = PVersionRange(kind: verAny)
except EParseVersion:
raise newException(ENimble, "Unable to parse dependency version range: " &
getCurrentExceptionMsg())
result.ver = VersionRangeRef(kind: verAny)
except ParseVersionError:
raise newException(NimbleError,
"Unable to parse dependency version range: " & getCurrentExceptionMsg())
proc multiSplit(s: string): seq[string] =
## Returns ``s`` split by newline and comma characters.
@ -115,7 +116,7 @@ proc multiSplit(s: string): seq[string] =
if len(result) < 1:
return @[s]
proc readPackageInfo*(path: string): TPackageInfo =
proc readPackageInfo*(path: string): PackageInfo =
result = initPackageInfo()
result.mypath = path
var fs = newFileStream(path, fmRead)
@ -161,18 +162,20 @@ proc readPackageInfo*(path: string): TPackageInfo =
of "javascript": result.backend = "js"
else: discard
else:
raise newException(ENimble, "Invalid field: " & ev.key)
raise newException(NimbleError, "Invalid field: " & ev.key)
of "deps", "dependencies":
case ev.key.normalize
of "requires":
for v in ev.value.multiSplit:
result.requires.add(parseRequires(v.strip))
else:
raise newException(ENimble, "Invalid field: " & ev.key)
else: raise newException(ENimble, "Invalid section: " & currentSection)
of cfgOption: raise newException(ENimble, "Invalid package info, should not contain --" & ev.value)
raise newException(NimbleError, "Invalid field: " & ev.key)
else: raise newException(NimbleError,
"Invalid section: " & currentSection)
of cfgOption: raise newException(NimbleError,
"Invalid package info, should not contain --" & ev.value)
of cfgError:
raise newException(ENimble, "Error parsing .nimble file: " & ev.msg)
raise newException(NimbleError, "Error parsing .nimble file: " & ev.msg)
close(p)
else:
raise newException(ValueError, "Cannot open package info: " & path)
@ -188,7 +191,8 @@ proc optionalField(obj: JsonNode, name: string, default = ""): string =
if obj[name].kind == JString:
return obj[name].str
else:
raise newException(ENimble, "Corrupted packages.json file. " & name & " field is of unexpected type.")
raise newException(NimbleError, "Corrupted packages.json file. " & name &
" field is of unexpected type.")
else: return default
proc requiredField(obj: JsonNode, name: string): string =
@ -197,11 +201,11 @@ proc requiredField(obj: JsonNode, name: string): string =
## Aborts execution if the field does not exist or is of invalid json type.
result = optionalField(obj, name, nil)
if result == nil:
raise newException(ENimble,
raise newException(NimbleError,
"Package in packages.json file does not contain a " & name & " field.")
proc fromJson(obj: JSonNode): TPackage =
## Constructs a TPackage object from a JSON node.
proc fromJson(obj: JSonNode): Package =
## Constructs a Package object from a JSON node.
##
## Aborts execution if the JSON node doesn't contain the required fields.
result.name = obj.requiredField("name")
@ -216,7 +220,7 @@ proc fromJson(obj: JSonNode): TPackage =
result.description = obj.requiredField("description")
result.web = obj.optionalField("web")
proc readMetadata*(path: string): TMetadata =
proc readMetaData*(path: string): MetaData =
## Reads the metadata present in ``~/.nimble/pkgs/pkg-0.1/nimblemeta.json``
var bmeta = path / "nimblemeta.json"
if not existsFile(bmeta):
@ -232,7 +236,7 @@ proc readMetadata*(path: string): TMetadata =
let jsonmeta = parseJson(cont)
result.url = jsonmeta["url"].str
proc getPackage*(pkg: string, packagesPath: string, resPkg: var TPackage): bool =
proc getPackage*(pkg: string, packagesPath: string, resPkg: var Package): bool =
## Searches ``packagesPath`` file saving into ``resPkg`` the found package.
##
## Pass in ``pkg`` the name of the package you are searching for. As
@ -244,12 +248,12 @@ proc getPackage*(pkg: string, packagesPath: string, resPkg: var TPackage): bool
resPkg = p.fromJson()
return true
proc getPackageList*(packagesPath: string): seq[TPackage] =
proc getPackageList*(packagesPath: string): seq[Package] =
## Returns the list of packages found at the specified path.
result = @[]
let packages = parseFile(packagesPath)
for p in packages:
let pkg: TPackage = p.fromJson()
let pkg: Package = p.fromJson()
result.add(pkg)
proc findNimbleFile*(dir: string): string =
@ -257,17 +261,20 @@ proc findNimbleFile*(dir: string): string =
for kind, path in walkDir(dir):
if kind == pcFile and path.splitFile.ext in [".babel", ".nimble"]:
if result != "":
raise newException(ENimble, "Only one .nimble file should be present in " & dir)
raise newException(NimbleError,
"Only one .nimble file should be present in " & dir)
result = path
proc getPkgInfo*(dir: string): TPackageInfo =
## Find the .nimble file in ``dir`` and parses it, returning a TPackageInfo.
proc getPkgInfo*(dir: string): PackageInfo =
## Find the .nimble file in ``dir`` and parses it, returning a PackageInfo.
let nimbleFile = findNimbleFile(dir)
if nimbleFile == "":
raise newException(ENimble, "Specified directory does not contain a .nimble file.")
raise newException(NimbleError,
"Specified directory does not contain a .nimble file.")
result = readPackageInfo(nimbleFile)
proc getInstalledPkgs*(libsDir: string): seq[tuple[pkginfo: TPackageInfo, meta: TMetaData]] =
proc getInstalledPkgs*(libsDir: string):
seq[tuple[pkginfo: PackageInfo, meta: MetaData]] =
## Gets a list of installed packages.
##
## ``libsDir`` is in most cases: ~/.nimble/pkgs/
@ -276,15 +283,15 @@ proc getInstalledPkgs*(libsDir: string): seq[tuple[pkginfo: TPackageInfo, meta:
if kind == pcDir:
let nimbleFile = findNimbleFile(path)
if nimbleFile != "":
let meta = readMetadata(path)
let meta = readMetaData(path)
result.add((readPackageInfo(nimbleFile), meta))
else:
# TODO: Abstract logging.
echo("WARNING: No .nimble file found for ", path)
proc findPkg*(pkglist: seq[tuple[pkginfo: TPackageInfo, meta: TMetaData]],
dep: TPkgTuple,
r: var TPackageInfo): bool =
proc findPkg*(pkglist: seq[tuple[pkginfo: PackageInfo, meta: MetaData]],
dep: PkgTuple,
r: var PackageInfo): bool =
## Searches ``pkglist`` for a package of which version is within the range
## of ``dep.ver``. ``True`` is returned if a package is found. If multiple
## packages are found the newest one is returned (the one with the highest
@ -299,8 +306,8 @@ proc findPkg*(pkglist: seq[tuple[pkginfo: TPackageInfo, meta: TMetaData]],
r = pkg.pkginfo
result = true
proc findAllPkgs*(pkglist: seq[tuple[pkginfo: TPackageInfo, meta: TMetaData]],
dep: TPkgTuple): seq[TPackageInfo] =
proc findAllPkgs*(pkglist: seq[tuple[pkginfo: PackageInfo, meta: MetaData]],
dep: PkgTuple): seq[PackageInfo] =
## Searches ``pkglist`` for packages of which version is within the range
## of ``dep.ver``. This is similar to ``findPkg`` but returns multiple
## packages if multiple are found.
@ -311,7 +318,7 @@ proc findAllPkgs*(pkglist: seq[tuple[pkginfo: TPackageInfo, meta: TMetaData]],
if withinRange(newVersion(pkg.pkginfo.version), dep.ver):
result.add pkg.pkginfo
proc getRealDir*(pkgInfo: TPackageInfo): string =
proc getRealDir*(pkgInfo: PackageInfo): string =
## Returns the ``pkgInfo.srcDir`` or the .mypath directory if package does
## not specify the src dir.
if pkgInfo.srcDir != "":
@ -335,7 +342,7 @@ proc getNameVersion*(pkgpath: string): tuple[name, version: string] =
result.version = tail[i+1 .. -1]
break
proc echoPackage*(pkg: TPackage) =
proc echoPackage*(pkg: Package) =
echo(pkg.name & ":")
echo(" url: " & pkg.url & " (" & pkg.downloadMethod & ")")
echo(" tags: " & pkg.tags.join(", "))
@ -344,7 +351,7 @@ proc echoPackage*(pkg: TPackage) =
if pkg.web.len > 0:
echo(" website: " & pkg.web)
proc getDownloadDirName*(pkg: TPackage, verRange: PVersionRange): string =
proc getDownloadDirName*(pkg: Package, verRange: VersionRangeRef): string =
result = pkg.name
let verSimple = getSimpleString(verRange)
if verSimple != "":
@ -352,5 +359,7 @@ proc getDownloadDirName*(pkg: TPackage, verRange: PVersionRange): string =
result.add verSimple
when isMainModule:
doAssert getNameVersion("/home/user/.nimble/libs/packagea-0.1") == ("packagea", "0.1")
doAssert getNameVersion("/home/user/.nimble/libs/package-a-0.1") == ("package-a", "0.1")
doAssert getNameVersion("/home/user/.nimble/libs/packagea-0.1") ==
("packagea", "0.1")
doAssert getNameVersion("/home/user/.nimble/libs/package-a-0.1") ==
("package-a", "0.1")

View file

@ -8,16 +8,17 @@ import version, packageinfo, nimbletypes
proc doCmd*(cmd: string) =
let bin = cmd.split(' ')[0]
if findExe(bin) == "":
raise newException(ENimble, "'" & bin & "' not in PATH.")
raise newException(NimbleError, "'" & bin & "' not in PATH.")
let exitCode = execCmd(cmd)
if exitCode != QuitSuccess:
raise newException(ENimble, "Execution failed with exit code " & $exitCode)
raise newException(NimbleError,
"Execution failed with exit code " & $exitCode)
proc doCmdEx*(cmd: string): tuple[output: TaintedString, exitCode: int] =
let bin = cmd.split(' ')[0]
if findExe(bin) == "":
raise newException(ENimble, "'" & bin & "' not in PATH.")
raise newException(NimbleError, "'" & bin & "' not in PATH.")
return execCmdEx(cmd)
template cd*(dir: string, body: stmt) =
@ -33,7 +34,7 @@ proc getNimBin*: string =
if findExe("nim") != "": result = "nim"
elif findExe("nimrod") != "": result = "nimrod"
proc getNimrodVersion*: TVersion =
proc getNimrodVersion*: Version =
let nimBin = getNimBin()
let vOutput = doCmdEx(nimBin & " -v").output
var matches: array[0..MaxSubpatterns, string]
@ -75,7 +76,7 @@ proc copyDirD*(fro, to: string): seq[string] =
createDir(changeRoot(fro, to, path.splitFile.dir))
result.add copyFileD(path, changeRoot(fro, to, path))
proc getDownloadDirName*(uri: string, verRange: PVersionRange): string =
proc getDownloadDirName*(uri: string, verRange: VersionRangeRef): string =
## Creates a directory name based on the specified ``uri`` (url)
result = ""
let puri = parseUri(uri)

View file

@ -4,10 +4,10 @@
## Module for handling versions and version ranges such as ``>= 1.0 & <= 1.5``
import strutils, tables, hashes, parseutils
type
TVersion* = distinct string
TSpecial* = distinct string
Version* = distinct string
Special* = distinct string
TVersionRangeEnum* = enum
VersionRangeEnum* = enum
verLater, # > V
verEarlier, # < V
verEqLater, # >= V -- Equal or later
@ -17,32 +17,32 @@ type
verAny, # *
verSpecial # #head
PVersionRange* = ref TVersionRange
TVersionRange* = object
case kind*: TVersionRangeEnum
VersionRangeRef* = ref VersionRange
VersionRange* = object
case kind*: VersionRangeEnum
of verLater, verEarlier, verEqLater, verEqEarlier, verEq:
ver*: TVersion
ver*: Version
of verSpecial:
spe*: TSpecial
spe*: Special
of verIntersect:
verILeft, verIRight: PVersionRange
verILeft, verIRight: VersionRangeRef
of verAny:
nil
EParseVersion* = object of ValueError
ParseVersionError* = object of ValueError
proc newVersion*(ver: string): TVersion = return TVersion(ver)
proc newSpecial*(spe: string): TSpecial = return TSpecial(spe)
proc newVersion*(ver: string): Version = return Version(ver)
proc newSpecial*(spe: string): Special = return Special(spe)
proc `$`*(ver: TVersion): string {.borrow.}
proc `$`*(ver: Version): string {.borrow.}
proc hash*(ver: TVersion): THash {.borrow.}
proc hash*(ver: Version): THash {.borrow.}
proc `$`*(ver: TSpecial): string {.borrow.}
proc `$`*(ver: Special): string {.borrow.}
proc hash*(ver: TSpecial): THash {.borrow.}
proc hash*(ver: Special): THash {.borrow.}
proc `<`*(ver: TVersion, ver2: TVersion): bool =
proc `<`*(ver: Version, ver2: Version): bool =
var sVer = string(ver).split('.')
var sVer2 = string(ver2).split('.')
for i in 0..max(sVer.len, sVer2.len)-1:
@ -59,7 +59,7 @@ proc `<`*(ver: TVersion, ver2: TVersion): bool =
else:
return false
proc `==`*(ver: TVersion, ver2: TVersion): bool =
proc `==`*(ver: Version, ver2: Version): bool =
var sVer = string(ver).split('.')
var sVer2 = string(ver2).split('.')
for i in 0..max(sVer.len, sVer2.len)-1:
@ -74,13 +74,13 @@ proc `==`*(ver: TVersion, ver2: TVersion): bool =
else:
return false
proc `==`*(spe: TSpecial, spe2: TSpecial): bool =
proc `==`*(spe: Special, spe2: Special): bool =
return ($spe).toLower() == ($spe2).toLower()
proc `<=`*(ver: TVersion, ver2: TVersion): bool =
proc `<=`*(ver: Version, ver2: Version): bool =
return (ver == ver2) or (ver < ver2)
proc withinRange*(ver: TVersion, ran: PVersionRange): bool =
proc withinRange*(ver: Version, ran: VersionRangeRef): bool =
case ran.kind
of verLater:
return ver > ran.ver
@ -99,7 +99,7 @@ proc withinRange*(ver: TVersion, ran: PVersionRange): bool =
of verAny:
return true
proc withinRange*(spe: TSpecial, ran: PVersionRange): bool =
proc withinRange*(spe: Special, ran: VersionRangeRef): bool =
case ran.kind
of verLater, verEarlier, verEqLater, verEqEarlier, verEq, verIntersect:
return false
@ -108,16 +108,17 @@ proc withinRange*(spe: TSpecial, ran: PVersionRange): bool =
of verAny:
return true
proc contains*(ran: PVersionRange, ver: TVersion): bool =
proc contains*(ran: VersionRangeRef, ver: Version): bool =
return withinRange(ver, ran)
proc contains*(ran: PVersionRange, spe: TSpecial): bool =
proc contains*(ran: VersionRangeRef, spe: Special): bool =
return withinRange(spe, ran)
proc makeRange*(version: string, op: string): PVersionRange =
proc makeRange*(version: string, op: string): VersionRangeRef =
new(result)
if version == "":
raise newException(EParseVersion, "A version needs to accompany the operator.")
raise newException(ParseVersionError,
"A version needs to accompany the operator.")
case op
of ">":
result.kind = verLater
@ -130,15 +131,15 @@ proc makeRange*(version: string, op: string): PVersionRange =
of "":
result.kind = verEq
else:
raise newException(EParseVersion, "Invalid operator: " & op)
result.ver = TVersion(version)
raise newException(ParseVersionError, "Invalid operator: " & op)
result.ver = Version(version)
proc parseVersionRange*(s: string): PVersionRange =
proc parseVersionRange*(s: string): VersionRangeRef =
# >= 1.5 & <= 1.8
new(result)
if s[0] == '#':
result.kind = verSpecial
result.spe = s[1 .. -1].TSpecial
result.spe = s[1 .. -1].Special
return
var i = 0
@ -159,7 +160,7 @@ proc parseVersionRange*(s: string): PVersionRange =
# Disallow more than one verIntersect. It's pointless and could lead to
# major unpredictable mistakes.
if result.verIRight.kind == verIntersect:
raise newException(EParseVersion,
raise newException(ParseVersionError,
"Having more than one `&` in a version range is pointless")
break
@ -175,13 +176,15 @@ proc parseVersionRange*(s: string): PVersionRange =
# Make sure '0.9 8.03' is not allowed.
if version != "" and i < s.len:
if s[i+1] in {'0'..'9', '.'}:
raise newException(EParseVersion, "Whitespace is not allowed in a version literal.")
raise newException(ParseVersionError,
"Whitespace is not allowed in a version literal.")
else:
raise newException(EParseVersion, "Unexpected char in version range: " & s[i])
raise newException(ParseVersionError,
"Unexpected char in version range: " & s[i])
inc(i)
proc `$`*(verRange: PVersionRange): string =
proc `$`*(verRange: VersionRangeRef): string =
case verRange.kind
of verLater:
result = "> "
@ -202,34 +205,36 @@ proc `$`*(verRange: PVersionRange): string =
result.add(string(verRange.ver))
proc getSimpleString*(verRange: PVersionRange): string =
## Gets a string with no special symbols and spaces. Used for dir name creation
## in tools.nim
proc getSimpleString*(verRange: VersionRangeRef): string =
## Gets a string with no special symbols and spaces. Used for dir name
## creation in tools.nim
case verRange.kind
of verSpecial:
result = $verRange.spe
of verLater, verEarlier, verEqLater, verEqEarlier, verEq:
result = $verRange.ver
of verIntersect:
result = getSimpleString(verRange.verILeft) & "_" & getSimpleString(verRange.verIRight)
result = getSimpleString(verRange.verILeft) & "_" &
getSimpleString(verRange.verIRight)
of verAny:
result = ""
proc newVRAny*(): PVersionRange =
proc newVRAny*(): VersionRangeRef =
new(result)
result.kind = verAny
proc newVREarlier*(ver: string): PVersionRange =
proc newVREarlier*(ver: string): VersionRangeRef =
new(result)
result.kind = verEarlier
result.ver = newVersion(ver)
proc newVREq*(ver: string): PVersionRange =
proc newVREq*(ver: string): VersionRangeRef =
new(result)
result.kind = verEq
result.ver = newVersion(ver)
proc findLatest*(verRange: PVersionRange, versions: Table[TVersion, string]): tuple[ver: TVersion, tag: string] =
proc findLatest*(verRange: VersionRangeRef,
versions: Table[Version, string]): tuple[ver: Version, tag: string] =
result = (newVersion(""), "")
for ver, tag in versions:
if not withinRange(ver, verRange): continue
@ -263,8 +268,10 @@ when isMainModule:
doAssert(newVersion("") < newVersion("1.0.0"))
doAssert(newVersion("") < newVersion("0.1.0"))
var versions = toTable[TVersion, string]({newVersion("0.1.1"): "v0.1.1", newVersion("0.2.3"): "v0.2.3", newVersion("0.5"): "v0.5"})
doAssert findLatest(parseVersionRange(">= 0.1 & <= 0.4"), versions) == (newVersion("0.2.3"), "v0.2.3")
var versions = toTable[Version, string]({newVersion("0.1.1"): "v0.1.1",
newVersion("0.2.3"): "v0.2.3", newVersion("0.5"): "v0.5"})
doAssert findLatest(parseVersionRange(">= 0.1 & <= 0.4"), versions) ==
(newVersion("0.2.3"), "v0.2.3")
# TODO: Allow these in later versions?
#doAssert newVersion("0.1-rc1") < newVersion("0.2")