generate_new_lockfile.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. })
  62. end
  63. table.sort(default_plugins, function(a, b)
  64. return a.name < b.name
  65. end)
  66. for _, entry in pairs(default_plugins) do
  67. local on_done = function(success, result, errors)
  68. completed = completed + 1
  69. if not success then
  70. print("error: " .. errors)
  71. return
  72. end
  73. local latest_sha = result:gsub("\tHEAD\n", ""):sub(1, 7)
  74. plugins_list[entry.name] = {
  75. commit = latest_sha,
  76. }
  77. end
  78. local handle = call_proc("git", { args = { "ls-remote", entry.url, entry.branch } }, on_done)
  79. assert(handle)
  80. table.insert(active_jobs, handle)
  81. end
  82. print("active: " .. #active_jobs)
  83. print("plugins: " .. #default_plugins)
  84. vim.wait(#active_jobs * 60 * 1000, function()
  85. return completed == #active_jobs
  86. end)
  87. if verbose then
  88. print(vim.inspect(plugins_list))
  89. end
  90. local fd = assert(io.open(sp, "w"))
  91. fd:write(vim.json.encode(plugins_list), "\n")
  92. fd:flush()
  93. end
  94. write_lockfile()
  95. vim.cmd "q"