verify_plugins.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. local completed = 0
  2. local collection = {}
  3. local active_jobs = {}
  4. local fmt = string.format
  5. local core_plugins = require "lvim.plugins"
  6. local default_snapshot_path = join_paths(get_lvim_base_dir(), "snapshots", "default.json")
  7. local fd = io.open(default_snapshot_path, "rb")
  8. local content
  9. if fd then
  10. content = fd:read "*a"
  11. end
  12. local default_sha1 = vim.json.decode(content)
  13. local get_short_name = function(spec)
  14. return spec[1]:match "/(%S*)"
  15. end
  16. local get_default_sha1 = function(spec)
  17. local short_name, _ = get_short_name(spec)
  18. assert(default_sha1[short_name])
  19. return default_sha1[short_name].commit
  20. end
  21. local is_directory = require("lvim.utils").is_directory
  22. local lazydir = join_paths(get_runtime_dir(), "site", "pack", "lazy")
  23. local verify_lazy = function()
  24. if not is_directory(lazydir) then
  25. io.write "Lazy.nvim not installed!"
  26. os.exit(1)
  27. end
  28. local status_ok, lazy = pcall(require, "lazy")
  29. if status_ok and lazy then
  30. return
  31. end
  32. io.write "Lazy.nvim not installed!"
  33. os.exit(1)
  34. end
  35. local get_install_path = function(spec)
  36. local prefix = join_paths(lazydir, "opt")
  37. local path = join_paths(prefix, get_short_name(spec))
  38. return is_directory(path) and path
  39. end
  40. local function call_proc(process, opts, cb)
  41. local output, error_output = "", ""
  42. local handle_stdout = function(err, chunk)
  43. assert(not err, err)
  44. if chunk then
  45. output = output .. chunk
  46. end
  47. end
  48. local handle_stderr = function(err, chunk)
  49. assert(not err, err)
  50. if chunk then
  51. error_output = error_output .. chunk
  52. end
  53. end
  54. local uv = vim.loop
  55. local handle
  56. local stdout = uv.new_pipe(false)
  57. local stderr = uv.new_pipe(false)
  58. local stdio = { nil, stdout, stderr }
  59. handle = uv.spawn(
  60. process,
  61. { args = opts.args, cwd = opts.cwd or uv.cwd(), stdio = stdio },
  62. vim.schedule_wrap(function(code)
  63. if code ~= 0 then
  64. ---@diagnostic disable-next-line: undefined-field
  65. stdout:read_stop()
  66. ---@diagnostic disable-next-line: undefined-field
  67. stderr:read_stop()
  68. end
  69. local check = uv.new_check()
  70. check:start(function()
  71. for _, pipe in ipairs(stdio) do
  72. if pipe and not pipe:is_closing() then
  73. return
  74. end
  75. end
  76. check:stop()
  77. handle:close()
  78. cb(code, output, error_output)
  79. end)
  80. end)
  81. )
  82. uv.read_start(stdout, handle_stdout)
  83. uv.read_start(stderr, handle_stderr)
  84. return handle
  85. end
  86. local function verify_core_plugins(verbose)
  87. for _, spec in pairs(core_plugins) do
  88. local path = get_install_path(spec)
  89. if spec.enabled or spec.enabled == nil and path then
  90. table.insert(collection, {
  91. name = get_short_name(spec),
  92. commit = get_default_sha1(spec),
  93. path = path,
  94. })
  95. end
  96. end
  97. for _, entry in pairs(collection) do
  98. local on_done = function(code, result, errors)
  99. completed = completed + 1
  100. if code ~= 0 then
  101. io.write(errors .. "\n")
  102. -- os.exit(code)
  103. else
  104. if verbose then
  105. io.write(fmt("verified [%s]\n", entry.name))
  106. end
  107. end
  108. local current_commit = result:gsub("\n", ""):gsub([[']], [[]]):sub(1, 7)
  109. -- just in case there are some extra qutoes or it's a longer commit hash
  110. if current_commit ~= entry.commit then
  111. io.write(fmt("mismatch at [%s]: expected [%s], got [%s]\n", entry.name, entry.commit, current_commit))
  112. os.exit(1)
  113. end
  114. end
  115. local handle = call_proc("git", { args = { "rev-parse", "--short", "HEAD" }, cwd = entry.path }, on_done)
  116. assert(handle)
  117. table.insert(active_jobs, handle)
  118. end
  119. vim.wait(#active_jobs * 60 * 1000, function()
  120. ---@diagnostic disable-next-line: redundant-return-value
  121. return completed == #active_jobs
  122. end)
  123. end
  124. verify_lazy()
  125. verify_core_plugins()
  126. vim.cmd "q"