deps.nim 1.4 KB

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