packagehandling.nim 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. result = ""
  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
  28. break packageSearch
  29. # we also store if we didn't find anything:
  30. for d in myParentDirs(path):
  31. #echo "set cache ", d, " |", result, "|", parents
  32. conf.packageCache[d] = result
  33. dec parents
  34. if parents <= 0: break
  35. proc getPackageName*(conf: ConfigRef; path: string): string =
  36. ## returns nimble package name, e.g.: `cligen`
  37. let path = getNimbleFile(conf, path)
  38. if path.len > 0:
  39. return path.splitFile.name
  40. else:
  41. return "unknown"