verify_plugins.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. -- see packer.init()
  23. local packdir = join_paths(get_runtime_dir(), "site", "pack", "packer")
  24. local packer_config = { opt_dir = join_paths(packdir, "opt"), start_dir = join_paths(packdir, "start") }
  25. local is_optional = function(spec)
  26. return spec.opt or spec.event or spec.cmd or spec.module
  27. end
  28. local get_install_path = function(spec)
  29. local prefix = is_optional(spec) and packer_config.opt_dir or packer_config.start_dir
  30. local path = join_paths(prefix, get_short_name(spec))
  31. assert(is_directory(path))
  32. return path
  33. end
  34. local function call_proc(process, opts, cb)
  35. local output, error_output = "", ""
  36. local handle_stdout = function(err, chunk)
  37. assert(not err, err)
  38. if chunk then
  39. output = output .. chunk
  40. end
  41. end
  42. local handle_stderr = function(err, chunk)
  43. assert(not err, err)
  44. if chunk then
  45. error_output = error_output .. chunk
  46. end
  47. end
  48. local uv = vim.loop
  49. local handle
  50. local stdout = uv.new_pipe(false)
  51. local stderr = uv.new_pipe(false)
  52. local stdio = { nil, stdout, stderr }
  53. handle = uv.spawn(
  54. process,
  55. { args = opts.args, cwd = opts.cwd or uv.cwd(), stdio = stdio },
  56. vim.schedule_wrap(function(code)
  57. if code ~= 0 then
  58. stdout:read_stop()
  59. stderr:read_stop()
  60. end
  61. local check = uv.new_check()
  62. check:start(function()
  63. for _, pipe in ipairs(stdio) do
  64. if pipe and not pipe:is_closing() then
  65. return
  66. end
  67. end
  68. check:stop()
  69. handle:close()
  70. cb(code, output, error_output)
  71. end)
  72. end)
  73. )
  74. uv.read_start(stdout, handle_stdout)
  75. uv.read_start(stderr, handle_stderr)
  76. return handle
  77. end
  78. local function verify_core_plugins(verbose)
  79. for _, spec in pairs(core_plugins) do
  80. if not spec.disable then
  81. table.insert(collection, {
  82. name = get_short_name(spec),
  83. commit = get_default_sha1(spec),
  84. path = get_install_path(spec),
  85. })
  86. end
  87. end
  88. for _, entry in pairs(collection) do
  89. local on_done = function(code, result, errors)
  90. completed = completed + 1
  91. if code ~= 0 then
  92. io.write(errors .. "\n")
  93. -- os.exit(code)
  94. else
  95. if verbose then
  96. io.write(fmt("verified [%s]\n", entry.name))
  97. end
  98. end
  99. local current_commit = result:gsub("\n", ""):gsub([[']], [[]])
  100. -- just in case there are some extra qutoes or it's a longer commit hash
  101. if current_commit ~= entry.commit then
  102. io.write(fmt("mismatch at [%s]: expected [%s], got [%s]\n", entry.name, entry.commit, current_commit))
  103. os.exit(1)
  104. end
  105. end
  106. local handle = call_proc("git", { args = { "log", "--pretty='%h'", "-1" }, cwd = entry.path }, on_done)
  107. assert(handle)
  108. table.insert(active_jobs, handle)
  109. end
  110. vim.wait(#active_jobs * 60 * 1000, function()
  111. return completed == #active_jobs
  112. end)
  113. end
  114. verify_core_plugins()
  115. vim.cmd "q"