packagehandling.nim 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. if path.len > 0:
  38. return path.splitFile.name
  39. else:
  40. return "unknown"