nimblecmd.nim 5.2 KB

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