# # # The Nim Compiler # (c) Copyright 2012 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # ## Implements some helper procs for Nimble (Nim's package manager) support. import parseutils, strutils, strtabs, os, options, msgs, sequtils, configuration proc addPath*(conf: ConfigRef; path: string, info: TLineInfo) = if not conf.searchPaths.contains(path): conf.searchPaths.insert(path, 0) type Version* = distinct string proc `$`*(ver: Version): string {.borrow.} proc newVersion*(ver: string): Version = doAssert(ver.len == 0 or ver[0] in {'#', '\0'} + Digits, "Wrong version: " & ver) return Version(ver) proc isSpecial(ver: Version): bool = return ($ver).len > 0 and ($ver)[0] == '#' proc isValidVersion(v: string): bool = if v.len > 0: if v[0] in {'#'} + Digits: return true proc `<`*(ver: Version, ver2: Version): bool = ## This is synced from Nimble's version module. # Handling for special versions such as "#head" or "#branch". if ver.isSpecial or ver2.isSpecial: if ver2.isSpecial and ($ver2).normalize == "#head": return ($ver).normalize != "#head" if not ver2.isSpecial: # `#aa111 < 1.1` return ($ver).normalize != "#head" # Handling for normal versions such as "0.1.0" or "1.0". var sVer = string(ver).split('.') var sVer2 = string(ver2).split('.') for i in 0..max(sVer.len, sVer2.len)-1: var sVerI = 0 if i < sVer.len: discard parseInt(sVer[i], sVerI) var sVerI2 = 0 if i < sVer2.len: discard parseInt(sVer2[i], sVerI2) if sVerI < sVerI2: return true elif sVerI == sVerI2: discard 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 = "-#" var sepIdx = p.find(specialSeparator) if sepIdx == -1: sepIdx = p.rfind('-') if sepIdx == -1: result.name = p return for i in sepIdx..