packagehandling.nim 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2017 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. iterator myParentDirs(p: string): string =
  10. # XXX os's parentDirs is stupid (multiple yields) and triggers an old bug...
  11. var current = p
  12. while true:
  13. current = current.parentDir
  14. if current.len == 0: break
  15. yield current
  16. proc getNimbleFile*(conf: ConfigRef; path: string): string =
  17. ## returns absolute path to nimble file, e.g.: /pathto/cligen.nimble
  18. var parents = 0
  19. block packageSearch:
  20. for d in myParentDirs(path):
  21. if conf.packageCache.hasKey(d):
  22. #echo "from cache ", d, " |", packageCache[d], "|", path.splitFile.name
  23. return conf.packageCache[d]
  24. inc parents
  25. for file in walkFiles(d / "*.nimble"):
  26. result = file
  27. break packageSearch
  28. # we also store if we didn't find anything:
  29. for d in myParentDirs(path):
  30. #echo "set cache ", d, " |", result, "|", parents
  31. conf.packageCache[d] = result
  32. dec parents
  33. if parents <= 0: break
  34. proc getPackageName*(conf: ConfigRef; path: string): string =
  35. ## returns nimble package name, e.g.: `cligen`
  36. let path = getNimbleFile(conf, path)
  37. result = path.splitFile.name
  38. proc fakePackageName*(conf: ConfigRef; path: AbsoluteFile): string =
  39. # Convert `path` so that 2 modules with same name
  40. # in different directory get different name and they can be
  41. # placed in a directory.
  42. # foo-#head/../bar becomes @foo-@hhead@s..@sbar
  43. result = "@m" & relativeTo(path, conf.projectPath).string.multiReplace(
  44. {$os.DirSep: "@s", $os.AltSep: "@s", "#": "@h", "@": "@@", ":": "@c"})
  45. proc demanglePackageName*(path: string): string =
  46. result = path.multiReplace({"@@": "@", "@h": "#", "@s": "/", "@m": "", "@c": ":"})
  47. proc withPackageName*(conf: ConfigRef; path: AbsoluteFile): AbsoluteFile =
  48. let x = getPackageName(conf, path.string)
  49. let (p, file, ext) = path.splitFile
  50. if x == "stdlib":
  51. # Hot code reloading now relies on 'stdlib_system' names etc.
  52. result = p / RelativeFile((x & '_' & file) & ext)
  53. else:
  54. result = p / RelativeFile(fakePackageName(conf, path))