deps.nim 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import os, uri, strformat, osproc, strutils
  2. proc exec(cmd: string) =
  3. echo "deps.cmd: " & cmd
  4. let status = execShellCmd(cmd)
  5. doAssert status == 0, cmd
  6. proc execEx(cmd: string): tuple[output: TaintedString, exitCode: int] =
  7. echo "deps.cmd: " & cmd
  8. execCmdEx(cmd, {poStdErrToStdOut, poUsePath, poEvalCommand})
  9. proc isGitRepo(dir: string): bool =
  10. # This command is used to get the relative path to the root of the repository.
  11. # Using this, we can verify whether a folder is a git repository by checking
  12. # whether the command success and if the output is empty.
  13. let (output, status) = execEx fmt"git -C {quoteShell(dir)} rev-parse --show-cdup"
  14. # On Windows there will be a trailing newline on success, remove it.
  15. # The value of a successful call typically won't have a whitespace (it's
  16. # usually a series of ../), so we know that it's safe to unconditionally
  17. # remove trailing whitespaces from the result.
  18. result = status == 0 and output.strip() == ""
  19. const commitHead* = "HEAD"
  20. proc cloneDependency*(destDirBase: string, url: string, commit = commitHead,
  21. appendRepoName = true, allowBundled = false) =
  22. let destDirBase = destDirBase.absolutePath
  23. let p = url.parseUri.path
  24. let name = p.splitFile.name
  25. var destDir = destDirBase
  26. if appendRepoName: destDir = destDir / name
  27. let destDir2 = destDir.quoteShell
  28. if not dirExists(destDir):
  29. # note: old code used `destDir / .git` but that wouldn't prevent git clone
  30. # from failing
  31. exec fmt"git clone -q {url} {destDir2}"
  32. if isGitRepo(destDir):
  33. exec fmt"git -C {destDir2} fetch -q"
  34. exec fmt"git -C {destDir2} checkout -q {commit}"
  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"