generate_new_lockfile.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. local sp = os.getenv "SNAPSHOT_PATH"
  2. local function call_proc(process, opts, cb)
  3. local std_output = ""
  4. local error_output = ""
  5. local function onread(_, is_stderr)
  6. return function(err, data)
  7. if data then
  8. if is_stderr then
  9. error_output = (error_output or "") .. err
  10. else
  11. std_output = (std_output or "") .. data
  12. end
  13. end
  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 = 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, std_output, error_output)
  39. end)
  40. end)
  41. )
  42. uv.read_start(stdout, onread(handle, false))
  43. uv.read_start(stderr, onread(handle, true))
  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. })
  61. end
  62. table.sort(default_plugins, function(a, b)
  63. return a.name < b.name
  64. end)
  65. for _, entry in pairs(default_plugins) do
  66. local on_done = function(success, result, errors)
  67. completed = completed + 1
  68. if not success then
  69. print("error: " .. errors)
  70. return
  71. end
  72. local latest_sha = result:gsub("\tHEAD\n", ""):sub(1, 7)
  73. plugins_list[entry.name] = {
  74. commit = latest_sha,
  75. }
  76. end
  77. local handle = call_proc("git", { args = { "ls-remote", entry.url, "HEAD" } }, on_done)
  78. table.insert(active_jobs, handle)
  79. end
  80. print("active: " .. #active_jobs)
  81. print("parsers: " .. #default_plugins)
  82. vim.wait(#active_jobs * 60 * 1000, function()
  83. return completed == #active_jobs
  84. end)
  85. if verbose then
  86. print(vim.inspect(plugins_list))
  87. end
  88. local fd = assert(io.open(sp, "w"))
  89. fd:write(vim.json.encode(plugins_list), "\n")
  90. fd:flush()
  91. end
  92. write_lockfile()
  93. vim.cmd "q"