packagehandling.nim 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 resetPackageCache*(conf: ConfigRef) =
  17. conf.packageCache = newPackageCache()
  18. proc getPackageName*(conf: ConfigRef; path: string): string =
  19. var parents = 0
  20. block packageSearch:
  21. for d in myParentDirs(path):
  22. if conf.packageCache.hasKey(d):
  23. #echo "from cache ", d, " |", packageCache[d], "|", path.splitFile.name
  24. return conf.packageCache[d]
  25. inc parents
  26. for file in walkFiles(d / "*.nimble"):
  27. result = file.splitFile.name
  28. break packageSearch
  29. # we also store if we didn't find anything:
  30. when not defined(nimNoNilSeqs):
  31. if result.isNil: result = ""
  32. for d in myParentDirs(path):
  33. #echo "set cache ", d, " |", result, "|", parents
  34. conf.packageCache[d] = result
  35. dec parents
  36. if parents <= 0: break
  37. proc fakePackageName*(conf: ConfigRef; path: AbsoluteFile): string =
  38. # foo-#head/../bar becomes @foo-@hhead@s..@sbar
  39. result = "@m" & relativeTo(path, conf.projectPath, '/').string.multiReplace({"/": "@s", "#": "@h", "@": "@@", ":": "@c"})
  40. proc demanglePackageName*(path: string): string =
  41. result = path.multiReplace({"@@": "@", "@h": "#", "@s": "/", "@m": "", "@c": ":"})
  42. proc withPackageName*(conf: ConfigRef; path: AbsoluteFile): AbsoluteFile =
  43. let x = getPackageName(conf, path.string)
  44. let (p, file, ext) = path.splitFile
  45. if x == "stdlib":
  46. # Hot code reloading now relies on 'stdlib_system' names etc.
  47. result = p / RelativeFile((x & '_' & file) & ext)
  48. else:
  49. result = p / RelativeFile(fakePackageName(conf, path))