nimblecmd.nim 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Implements some helper procs for Nimble (Nim's package manager) support.
  10. import parseutils, strutils, os, options, msgs, sequtils, lineinfos, pathutils,
  11. tables
  12. when defined(nimPreviewSlimSystem):
  13. import std/[syncio, assertions]
  14. import ../dist/checksums/src/checksums/sha1
  15. proc addPath*(conf: ConfigRef; path: AbsoluteDir, info: TLineInfo) =
  16. if not conf.searchPaths.contains(path):
  17. conf.searchPaths.insert(path, 0)
  18. type
  19. Version* = distinct string
  20. PackageInfo = Table[string, tuple[version, checksum: string]]
  21. proc `$`*(ver: Version): string {.borrow.}
  22. proc newVersion*(ver: string): Version =
  23. doAssert(ver.len == 0 or ver[0] in {'#', '\0'} + Digits,
  24. "Wrong version: " & ver)
  25. return Version(ver)
  26. proc isSpecial(ver: Version): bool =
  27. return ($ver).len > 0 and ($ver)[0] == '#'
  28. proc isValidVersion(v: string): bool =
  29. if v.len > 0:
  30. if v[0] in {'#'} + Digits:
  31. result = true
  32. else:
  33. result = false
  34. else:
  35. result = false
  36. proc `<`*(ver: Version, ver2: Version): bool =
  37. ## This is synced from Nimble's version module.
  38. result = false
  39. # Handling for special versions such as "#head" or "#branch".
  40. if ver.isSpecial or ver2.isSpecial:
  41. if ver2.isSpecial and ($ver2).normalize == "#head":
  42. return ($ver).normalize != "#head"
  43. if not ver2.isSpecial:
  44. # `#aa111 < 1.1`
  45. return ($ver).normalize != "#head"
  46. # Handling for normal versions such as "0.1.0" or "1.0".
  47. var sVer = string(ver).split('.')
  48. var sVer2 = string(ver2).split('.')
  49. for i in 0..<max(sVer.len, sVer2.len):
  50. var sVerI = 0
  51. if i < sVer.len:
  52. discard parseInt(sVer[i], sVerI)
  53. var sVerI2 = 0
  54. if i < sVer2.len:
  55. discard parseInt(sVer2[i], sVerI2)
  56. if sVerI < sVerI2:
  57. return true
  58. elif sVerI == sVerI2:
  59. discard
  60. else:
  61. return false
  62. proc getPathVersionChecksum*(p: string): tuple[name, version, checksum: string] =
  63. ## Splits path ``p`` in the format
  64. ## ``/home/user/.nimble/pkgs/package-0.1-febadeaea2345e777f0f6f8433f7f0a52edd5d1b`` into
  65. ## ``("/home/user/.nimble/pkgs/package", "0.1", "febadeaea2345e777f0f6f8433f7f0a52edd5d1b")``
  66. const checksumSeparator = '-'
  67. const versionSeparator = '-'
  68. const specialVersionSepartator = "-#"
  69. const separatorNotFound = -1
  70. var checksumSeparatorIndex = p.rfind(checksumSeparator)
  71. if checksumSeparatorIndex != separatorNotFound:
  72. result.checksum = p.substr(checksumSeparatorIndex + 1)
  73. if not result.checksum.isValidSha1Hash():
  74. result.checksum = ""
  75. checksumSeparatorIndex = p.len()
  76. else:
  77. checksumSeparatorIndex = p.len()
  78. var versionSeparatorIndex = p.rfind(
  79. specialVersionSepartator, 0, checksumSeparatorIndex - 1)
  80. if versionSeparatorIndex != separatorNotFound:
  81. result.version = p.substr(
  82. versionSeparatorIndex + 1, checksumSeparatorIndex - 1)
  83. else:
  84. versionSeparatorIndex = p.rfind(
  85. versionSeparator, 0, checksumSeparatorIndex - 1)
  86. if versionSeparatorIndex != separatorNotFound:
  87. result.version = p.substr(
  88. versionSeparatorIndex + 1, checksumSeparatorIndex - 1)
  89. else:
  90. versionSeparatorIndex = checksumSeparatorIndex
  91. result.name = p[0..<versionSeparatorIndex]
  92. proc addPackage*(conf: ConfigRef; packages: var PackageInfo, p: string;
  93. info: TLineInfo) =
  94. let (name, ver, checksum) = getPathVersionChecksum(p)
  95. if isValidVersion(ver):
  96. let version = newVersion(ver)
  97. if packages.getOrDefault(name).version.newVersion < version or
  98. (not packages.hasKey(name)):
  99. if checksum.isValidSha1Hash():
  100. packages[name] = ($version, checksum)
  101. else:
  102. packages[name] = ($version, "")
  103. else:
  104. localError(conf, info, "invalid package name: " & p)
  105. iterator chosen(packages: PackageInfo): string =
  106. for key, val in pairs(packages):
  107. var res = key
  108. if val.version.len != 0:
  109. res &= '-'
  110. res &= val.version
  111. if val.checksum.len != 0:
  112. res &= '-'
  113. res &= val.checksum
  114. yield res
  115. proc addNimblePath(conf: ConfigRef; p: string, info: TLineInfo) =
  116. var path = p
  117. let nimbleLinks = toSeq(walkPattern(p / "*.nimble-link"))
  118. if nimbleLinks.len > 0:
  119. # If the user has more than one .nimble-link file then... we just ignore it.
  120. # Spec for these files is available in Nimble's readme:
  121. # https://github.com/nim-lang/nimble#nimble-link
  122. let nimbleLinkLines = readFile(nimbleLinks[0]).splitLines()
  123. path = nimbleLinkLines[1]
  124. if not path.isAbsolute():
  125. path = p / path
  126. if not contains(conf.searchPaths, AbsoluteDir path):
  127. message(conf, info, hintPath, path)
  128. conf.lazyPaths.insert(AbsoluteDir path, 0)
  129. proc addPathRec(conf: ConfigRef; dir: string, info: TLineInfo) =
  130. var packages: PackageInfo = initTable[string, tuple[version, checksum: string]]()
  131. var pos = dir.len-1
  132. if dir[pos] in {DirSep, AltSep}: inc(pos)
  133. for k,p in os.walkDir(dir):
  134. if k == pcDir and p[pos] != '.':
  135. addPackage(conf, packages, p, info)
  136. for p in packages.chosen:
  137. addNimblePath(conf, p, info)
  138. proc nimblePath*(conf: ConfigRef; path: AbsoluteDir, info: TLineInfo) =
  139. addPathRec(conf, path.string, info)
  140. addNimblePath(conf, path.string, info)
  141. let i = conf.nimblePaths.find(path)
  142. if i != -1:
  143. conf.nimblePaths.delete(i)
  144. conf.nimblePaths.insert(path, 0)