packagehandling.nim 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. for file in walkFiles(d / "*.babel"):
  30. result = file.splitFile.name
  31. break packageSearch
  32. # we also store if we didn't find anything:
  33. when not defined(nimNoNilSeqs):
  34. if result.isNil: result = ""
  35. for d in myParentDirs(path):
  36. #echo "set cache ", d, " |", result, "|", parents
  37. conf.packageCache[d] = result
  38. dec parents
  39. if parents <= 0: break
  40. proc withPackageName*(conf: ConfigRef; path: AbsoluteFile): AbsoluteFile =
  41. let x = getPackageName(conf, path.string)
  42. if x.len == 0:
  43. result = path
  44. else:
  45. let (p, file, ext) = path.splitFile
  46. result = p / RelativeFile((x & '_' & file) & ext)