packagehandling.nim 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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({$os.DirSep: "@s", $os.AltSep: "@s", "#": "@h", "@": "@@", ":": "@c"})
  44. proc demanglePackageName*(path: string): string =
  45. result = path.multiReplace({"@@": "@", "@h": "#", "@s": "/", "@m": "", "@c": ":"})
  46. proc withPackageName*(conf: ConfigRef; path: AbsoluteFile): AbsoluteFile =
  47. let x = getPackageName(conf, path.string)
  48. let (p, file, ext) = path.splitFile
  49. if x == "stdlib":
  50. # Hot code reloading now relies on 'stdlib_system' names etc.
  51. result = p / RelativeFile((x & '_' & file) & ext)
  52. else:
  53. result = p / RelativeFile(fakePackageName(conf, path))