/src/luarocks/fetch/hg.lua

http://github.com/keplerproject/luarocks · Lua · 65 lines · 45 code · 11 blank · 9 comment · 12 complexity · 13ef8c92ef727d615a72ec1031a315ac MD5 · raw file

  1. --- Fetch back-end for retrieving sources from HG.
  2. local hg = {}
  3. local unpack = unpack or table.unpack
  4. local fs = require("luarocks.fs")
  5. local dir = require("luarocks.dir")
  6. local util = require("luarocks.util")
  7. --- Download sources for building a rock, using hg.
  8. -- @param rockspec table: The rockspec table
  9. -- @param extract boolean: Unused in this module (required for API purposes.)
  10. -- @param dest_dir string or nil: If set, will extract to the given directory.
  11. -- @return (string, string) or (nil, string): The absolute pathname of
  12. -- the fetched source tarball and the temporary directory created to
  13. -- store it; or nil and an error message.
  14. function hg.get_sources(rockspec, extract, dest_dir)
  15. assert(rockspec:type() == "rockspec")
  16. assert(type(dest_dir) == "string" or not dest_dir)
  17. local hg_cmd = rockspec.variables.HG
  18. local ok, err_msg = fs.is_tool_available(hg_cmd, "Mercurial")
  19. if not ok then
  20. return nil, err_msg
  21. end
  22. local name_version = rockspec.name .. "-" .. rockspec.version
  23. -- Strip off special hg:// protocol type
  24. local url = rockspec.source.url:gsub("^hg://", "")
  25. local module = dir.base_name(url)
  26. local command = {hg_cmd, "clone", url, module}
  27. local tag_or_branch = rockspec.source.tag or rockspec.source.branch
  28. if tag_or_branch then
  29. command = {hg_cmd, "clone", "--rev", tag_or_branch, url, module}
  30. end
  31. local store_dir
  32. if not dest_dir then
  33. store_dir = fs.make_temp_dir(name_version)
  34. if not store_dir then
  35. return nil, "Failed creating temporary directory."
  36. end
  37. util.schedule_function(fs.delete, store_dir)
  38. else
  39. store_dir = dest_dir
  40. end
  41. local ok, err = fs.change_dir(store_dir)
  42. if not ok then return nil, err end
  43. if not fs.execute(unpack(command)) then
  44. return nil, "Failed cloning hg repository."
  45. end
  46. ok, err = fs.change_dir(module)
  47. if not ok then return nil, err end
  48. fs.delete(dir.path(store_dir, module, ".hg"))
  49. fs.delete(dir.path(store_dir, module, ".hgignore"))
  50. fs.pop_dir()
  51. fs.pop_dir()
  52. return module, store_dir
  53. end
  54. return hg