verify_plugins.lua 3.6 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. return is_directory(path) and path
  32. end
  33. local function call_proc(process, opts, cb)
  34. local output, error_output = "", ""
  35. local handle_stdout = function(err, chunk)
  36. assert(not err, err)
  37. if chunk then
  38. output = output .. chunk
  39. end
  40. end
  41. local handle_stderr = function(err, chunk)
  42. assert(not err, err)
  43. if chunk then
  44. error_output = error_output .. chunk
  45. end
  46. end
  47. local uv = vim.loop
  48. local handle
  49. local stdout = uv.new_pipe(false)
  50. local stderr = uv.new_pipe(false)
  51. local stdio = { nil, stdout, stderr }
  52. handle = uv.spawn(
  53. process,
  54. { args = opts.args, cwd = opts.cwd or uv.cwd(), stdio = stdio },
  55. vim.schedule_wrap(function(code)
  56. if code ~= 0 then
  57. stdout:read_stop()
  58. stderr:read_stop()
  59. end
  60. local check = uv.new_check()
  61. check:start(function()
  62. for _, pipe in ipairs(stdio) do
  63. if pipe and not pipe:is_closing() then
  64. return
  65. end
  66. end
  67. check:stop()
  68. handle:close()
  69. cb(code, output, error_output)
  70. end)
  71. end)
  72. )
  73. uv.read_start(stdout, handle_stdout)
  74. uv.read_start(stderr, handle_stderr)
  75. return handle
  76. end
  77. local function verify_core_plugins(verbose)
  78. for _, spec in pairs(core_plugins) do
  79. local path = get_install_path(spec)
  80. if not spec.disable and path then
  81. table.insert(collection, {
  82. name = get_short_name(spec),
  83. commit = get_default_sha1(spec),
  84. path = path,
  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([[']], [[]]):sub(1, 7)
  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 = { "rev-parse", "--short", "HEAD" }, 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"