deps.nim 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 destDir2 = 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} {destDir2}"
  27. if isGitRepo(destDir):
  28. execRetry fmt"git -C {destDir2} fetch -q"
  29. exec fmt"git -C {destDir2} checkout -q {commit}"
  30. elif allowBundled:
  31. discard "this dependency was bundled with Nim, don't do anything"
  32. else:
  33. quit "FAILURE: " & destdir & " already exists but is not a git repo"