deps.nim 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import os, uri, strformat, strutils
  2. import std/private/gitutils
  3. proc exec(cmd: string) =
  4. echo "deps.cmd: " & cmd
  5. let status = execShellCmd(cmd)
  6. doAssert status == 0, cmd
  7. proc execRetry(cmd: string) =
  8. let ok = retryCall(call = block:
  9. let status = execShellCmd(cmd)
  10. let result = status == 0
  11. if not result:
  12. echo fmt"failed command: '{cmd}', status: {status}"
  13. result)
  14. doAssert ok, cmd
  15. proc cloneDependency*(destDirBase: string, url: string, commit = commitHead,
  16. appendRepoName = true, allowBundled = false) =
  17. let destDirBase = destDirBase.absolutePath
  18. let p = url.parseUri.path
  19. let name = p.splitFile.name
  20. var destDir = destDirBase
  21. if appendRepoName: destDir = destDir / name
  22. let quotedDestDir = destDir.quoteShell
  23. if not dirExists(destDir):
  24. # note: old code used `destDir / .git` but that wouldn't prevent git clone
  25. # from failing
  26. execRetry fmt"git clone -q {url} {quotedDestDir}"
  27. if isGitRepo(destDir):
  28. let oldDir = getCurrentDir()
  29. setCurrentDir(destDir)
  30. try:
  31. execRetry "git fetch -q"
  32. exec fmt"git checkout -q {commit}"
  33. finally:
  34. setCurrentDir(oldDir)
  35. elif allowBundled:
  36. discard "this dependency was bundled with Nim, don't do anything"
  37. else:
  38. quit "FAILURE: " & destdir & " already exists but is not a git repo"