nimblecmd.nim 4.9 KB

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