generate_new_lockfile.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. local sp = os.getenv "SNAPSHOT_PATH"
  2. local function call_proc(process, opts, cb)
  3. local output, error_output = "", ""
  4. local handle_stdout = function(err, chunk)
  5. assert(not err, err)
  6. if chunk then
  7. output = output .. chunk
  8. end
  9. end
  10. local handle_stderr = function(err, chunk)
  11. assert(not err, err)
  12. if chunk then
  13. error_output = error_output .. chunk
  14. end
  15. end
  16. local uv = vim.loop
  17. local handle
  18. local stdout = uv.new_pipe(false)
  19. local stderr = uv.new_pipe(false)
  20. local stdio = { nil, stdout, stderr }
  21. handle = uv.spawn(
  22. process,
  23. { args = opts.args, cwd = opts.cwd or uv.cwd(), stdio = stdio },
  24. vim.schedule_wrap(function(code)
  25. if code ~= 0 then
  26. stdout:read_stop()
  27. stderr:read_stop()
  28. end
  29. local check = uv.new_check()
  30. check:start(function()
  31. for _, pipe in ipairs(stdio) do
  32. if pipe and not pipe:is_closing() then
  33. return
  34. end
  35. end
  36. check:stop()
  37. handle:close()
  38. cb(code, output, error_output)
  39. end)
  40. end)
  41. )
  42. uv.read_start(stdout, handle_stdout)
  43. uv.read_start(stderr, handle_stderr)
  44. return handle
  45. end
  46. local plugins_list = {}
  47. local completed = 0
  48. local function write_lockfile(verbose)
  49. local default_plugins = {}
  50. local active_jobs = {}
  51. local core_plugins = require "lvim.plugins"
  52. for _, plugin in pairs(core_plugins) do
  53. local name = plugin[1]:match "/(%S*)"
  54. local url = "https://github.com/" .. plugin[1]
  55. local commit = ""
  56. table.insert(default_plugins, {
  57. name = name,
  58. url = url,
  59. commit = commit,
  60. branch = plugin.branch or "HEAD",
  61. tag = plugin.tag,
  62. })
  63. end
  64. table.sort(default_plugins, function(a, b)
  65. return a.name < b.name
  66. end)
  67. for _, entry in pairs(default_plugins) do
  68. local on_done = function(success, result, errors)
  69. completed = completed + 1
  70. if not success then
  71. print("error: " .. errors)
  72. return
  73. end
  74. local latest_sha = result:gsub("\tHEAD\n", ""):sub(1, 7)
  75. if entry.tag then
  76. local dereferenced_commit = result:match("\n(.*)\trefs/tags/" .. entry.tag .. "%^{}\n")
  77. if dereferenced_commit then
  78. latest_sha = dereferenced_commit:sub(1, 7)
  79. end
  80. end
  81. plugins_list[entry.name] = {
  82. commit = latest_sha,
  83. }
  84. end
  85. local handle = call_proc("git", {
  86. args = { "ls-remote", entry.url, entry.tag and entry.tag .. "*" or entry.branch },
  87. }, on_done)
  88. assert(handle)
  89. table.insert(active_jobs, handle)
  90. end
  91. print("active: " .. #active_jobs)
  92. print("plugins: " .. #default_plugins)
  93. vim.wait(#active_jobs * 60 * 1000, function()
  94. return completed == #active_jobs
  95. end)
  96. if verbose then
  97. print(vim.inspect(plugins_list))
  98. end
  99. local fd = assert(io.open(sp, "w"))
  100. fd:write(vim.json.encode(plugins_list), "\n")
  101. fd:flush()
  102. end
  103. write_lockfile()
  104. vim.cmd "q"