From e14ffd56282a7056f6bc749f2bd695d16f2388c1 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Sun, 15 Dec 2013 17:21:41 +0000 Subject: [PATCH] Better download dir names for URL dependencies. --- babel.nim | 4 ++-- tools.nim | 27 ++++++++++++++++++++++++--- version.nim | 13 +++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/babel.nim b/babel.nim index f848334..61ac9fd 100644 --- a/babel.nim +++ b/babel.nim @@ -323,14 +323,14 @@ proc installFromDir(dir: string, latest: bool, options: TOptions): string = proc downloadPkg(url: string, verRange: PVersionRange, downMethod: TDownloadMethod): string = - let downloadDir = (getTempDir() / "babel" / "url_unknown") + let downloadDir = (getTempDir() / "babel" / getDownloadDirName(url, verRange)) if not existsDir(getTempDir() / "babel"): createDir(getTempDir() / "babel") echo("Downloading ", url, " into ", downloadDir, " using ", downMethod, "...") doDownload(url, downloadDir, verRange, downMethod) result = downloadDir proc downloadPkg(pkg: TPackage, verRange: PVersionRange): string = - let downloadDir = (getTempDir() / "babel" / pkg.name) + let downloadDir = (getTempDir() / "babel" / getDownloadDirName(pkg, verRange)) if not existsDir(getTempDir() / "babel"): createDir(getTempDir() / "babel") let downMethod = pkg.downloadMethod.getDownloadMethod() echo("Downloading ", pkg.name, " into ", downloadDir, " using ", downMethod, "...") diff --git a/tools.nim b/tools.nim index 0a45a74..d00adb0 100644 --- a/tools.nim +++ b/tools.nim @@ -2,8 +2,8 @@ # BSD License. Look at license.txt for more info. # # Various miscellaneous utility functions reside here. -import osproc, pegs, strutils, os -import version, common +import osproc, pegs, strutils, os, parseurl +import version, common, packageinfo # TODO: Merge with common.nim? @@ -45,4 +45,25 @@ proc changeRoot*(origRoot, newRoot, path: string): string = return newRoot / path[origRoot.len .. -1] else: raise newException(EInvalidValue, - "Cannot change root of path: Path does not begin with original root.") \ No newline at end of file + "Cannot change root of path: Path does not begin with original root.") + +proc getDownloadDirName*(url: string, verRange: PVersionRange): string = + ## Creates a directory name based on the specified ``url`` + result = "" + let purl = parseUrl(url) + for i in purl.hostname: + case i + of strutils.Letters, strutils.Digits: + result.add i + else: nil + result.add "_" + for i in purl.path: + case i + of strutils.Letters, strutils.Digits: + result.add i + else: nil + result.add "_" + result.add getSimpleString(verRange) + +proc getDownloadDirName*(pkg: TPackage, verRange: PVersionRange): string = + result = pkg.name & "_" & verRange.getSimpleString \ No newline at end of file diff --git a/version.nim b/version.nim index 9f9b8ac..fdf3a3c 100644 --- a/version.nim +++ b/version.nim @@ -202,6 +202,19 @@ 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 + 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) + of verAny: + result = "" + proc newVRAny*(): PVersionRange = new(result) result.kind = verAny