From d9a6246c666c6b0fe6078adc194d3f101e9765e9 Mon Sep 17 00:00:00 2001 From: Ivan Bobev Date: Wed, 21 Aug 2019 14:45:22 +0300 Subject: [PATCH] Add changes required by Nimble lock file support Implemented support for Nimble local cache with package directories with a checksum of the package at the end of their names. Now the compiler supports package paths in the form: * /path_to_nimble_cache_dir/pkgs/package_name-1.2.3- FEBADEAEA2345E777F0F6F8433F7F0A52EDD5D1B * /path_to_nimble_cache_dir/pkgs/package_name-#head- 042D4BE2B90ED0672E717D71850ABDB0A2D19CD2 * /path_to_nimble_cache_dir/pkgs/package_name-#branch-name- DBC1F902CB79946E990E38AF51F0BAD36ACFABD9 Related to nim-lang/nimble#127 --- compiler/modulepaths.nim | 2 +- compiler/nimblecmd.nim | 82 ++++++++++++++++++----------- lib/std/sha1.nim | 11 ++++ tests/compiler/tnimblecmd.nim | 97 ++++++++++++++++++++++++++--------- tests/stdlib/tsha1.nim | 10 ++++ 5 files changed, 148 insertions(+), 54 deletions(-) diff --git a/compiler/modulepaths.nim b/compiler/modulepaths.nim index 8511b15921b5a..a16b669c45910 100644 --- a/compiler/modulepaths.nim +++ b/compiler/modulepaths.nim @@ -21,7 +21,7 @@ when false: for k, p in os.walkDir(dir, relative=true): if k == pcDir and p.len > pkg.len+1 and p[pkg.len] == '-' and p.startsWith(pkg): - let (_, a) = getPathVersion(p) + let (_, a, _) = getPathVersionChecksum(p) if bestv.len == 0 or bestv < a: bestv = a best = dir / p diff --git a/compiler/nimblecmd.nim b/compiler/nimblecmd.nim index 28398289ced85..18be8b3aa2ad4 100644 --- a/compiler/nimblecmd.nim +++ b/compiler/nimblecmd.nim @@ -9,8 +9,8 @@ ## Implements some helper procs for Nimble (Nim's package manager) support. -import parseutils, strutils, strtabs, os, options, msgs, sequtils, - lineinfos, pathutils +import parseutils, strutils, os, options, msgs, sequtils, lineinfos, pathutils, + std/sha1, tables proc addPath*(conf: ConfigRef; path: AbsoluteDir, info: TLineInfo) = if not conf.searchPaths.contains(path): @@ -18,6 +18,7 @@ proc addPath*(conf: ConfigRef; path: AbsoluteDir, info: TLineInfo) = type Version* = distinct string + PackageInfo = Table[string, tuple[version, checksum: string]] proc `$`*(ver: Version): string {.borrow.} @@ -62,43 +63,64 @@ proc `<`*(ver: Version, ver2: Version): bool = else: return false -proc getPathVersion*(p: string): tuple[name, version: string] = - ## Splits path ``p`` in the format ``/home/user/.nimble/pkgs/package-0.1`` - ## into ``(/home/user/.nimble/pkgs/package, 0.1)`` - result.name = "" - result.version = "" - - const specialSeparator = "-#" - let last = p.rfind(p.lastPathPart) # the index where the last path part begins - var sepIdx = p.find(specialSeparator, start = last) - if sepIdx == -1: - sepIdx = p.rfind('-', start = last) - - if sepIdx == -1: - result.name = p - return +proc getPathVersionChecksum*(p: string): tuple[name, version, checksum: string] = + ## Splits path ``p`` in the format + ## ``/home/user/.nimble/pkgs/package-0.1-febadeaea2345e777f0f6f8433f7f0a52edd5d1b`` into + ## ``("/home/user/.nimble/pkgs/package", "0.1", "febadeaea2345e777f0f6f8433f7f0a52edd5d1b")`` + + const checksumSeparator = '-' + const versionSeparator = '-' + const specialVersionSeparator = "-#" + const separatorNotFound = -1 + + var checksumSeparatorIndex = p.rfind(checksumSeparator) + if checksumSeparatorIndex != separatorNotFound: + result.checksum = p.substr(checksumSeparatorIndex + 1) + if not result.checksum.isValidSha1Hash(): + result.checksum = "" + checksumSeparatorIndex = p.len() + else: + checksumSeparatorIndex = p.len() - for i in sepIdx..