plugin-loader.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. local plugin_loader = {}
  2. local utils = require "lvim.utils"
  3. local Log = require "lvim.core.log"
  4. local join_paths = utils.join_paths
  5. local in_headless = #vim.api.nvim_list_uis() == 0
  6. -- we need to reuse this outside of init()
  7. local compile_path = join_paths(get_config_dir(), "plugin", "packer_compiled.lua")
  8. local snapshot_path = join_paths(get_cache_dir(), "snapshots")
  9. local default_snapshot = join_paths(get_lvim_base_dir(), "snapshots", "default.json")
  10. function plugin_loader.init(opts)
  11. opts = opts or {}
  12. local install_path = opts.install_path
  13. or join_paths(vim.fn.stdpath "data", "site", "pack", "packer", "start", "packer.nvim")
  14. local max_jobs = 100
  15. if vim.fn.has "mac" == 1 then
  16. max_jobs = 50
  17. end
  18. local init_opts = {
  19. package_root = opts.package_root or join_paths(vim.fn.stdpath "data", "site", "pack"),
  20. compile_path = compile_path,
  21. snapshot_path = snapshot_path,
  22. max_jobs = max_jobs,
  23. log = { level = "warn" },
  24. git = {
  25. clone_timeout = 120,
  26. },
  27. display = {
  28. open_fn = function()
  29. return require("packer.util").float { border = "rounded" }
  30. end,
  31. },
  32. }
  33. if in_headless then
  34. init_opts.display = nil
  35. end
  36. if not utils.is_directory(install_path) then
  37. vim.fn.system { "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path }
  38. vim.cmd "packadd packer.nvim"
  39. -- IMPORTANT: we only set this the very first time to avoid constantly triggering the rollback function
  40. -- https://github.com/wbthomason/packer.nvim/blob/c576ab3f1488ee86d60fd340d01ade08dcabd256/lua/packer.lua#L998-L995
  41. init_opts.snapshot = default_snapshot
  42. end
  43. local status_ok, packer = pcall(require, "packer")
  44. if status_ok then
  45. packer.on_complete = vim.schedule_wrap(function()
  46. require("lvim.utils.hooks").run_on_packer_complete()
  47. end)
  48. packer.init(init_opts)
  49. end
  50. end
  51. -- packer expects a space separated list
  52. local function pcall_packer_command(cmd, kwargs)
  53. local status_ok, msg = pcall(function()
  54. require("packer")[cmd](unpack(kwargs or {}))
  55. end)
  56. if not status_ok then
  57. Log:warn(cmd .. " failed with: " .. vim.inspect(msg))
  58. Log:trace(vim.inspect(vim.fn.eval "v:errmsg"))
  59. end
  60. end
  61. function plugin_loader.cache_clear()
  62. if vim.fn.delete(compile_path) == 0 then
  63. Log:debug "deleted packer_compiled.lua"
  64. end
  65. end
  66. function plugin_loader.recompile()
  67. plugin_loader.cache_clear()
  68. pcall_packer_command "compile"
  69. if utils.is_file(compile_path) then
  70. Log:debug "generated packer_compiled.lua"
  71. end
  72. end
  73. function plugin_loader.reload(configurations)
  74. _G.packer_plugins = _G.packer_plugins or {}
  75. for k, v in pairs(_G.packer_plugins) do
  76. if k ~= "packer.nvim" then
  77. _G.packer_plugins[v] = nil
  78. end
  79. end
  80. plugin_loader.load(configurations)
  81. plugin_loader.ensure_plugins()
  82. end
  83. function plugin_loader.load(configurations)
  84. Log:debug "loading plugins configuration"
  85. local packer_available, packer = pcall(require, "packer")
  86. if not packer_available then
  87. Log:warn "skipping loading plugins until Packer is installed"
  88. return
  89. end
  90. local status_ok, _ = xpcall(function()
  91. packer.reset()
  92. packer.startup(function(use)
  93. for _, plugins in ipairs(configurations) do
  94. for _, plugin in ipairs(plugins) do
  95. use(plugin)
  96. end
  97. end
  98. end)
  99. end, debug.traceback)
  100. if not status_ok then
  101. Log:warn "problems detected while loading plugins' configurations"
  102. Log:trace(debug.traceback())
  103. end
  104. end
  105. function plugin_loader.get_core_plugins()
  106. local list = {}
  107. local plugins = require "lvim.plugins"
  108. for _, item in pairs(plugins) do
  109. if not item.disable then
  110. table.insert(list, item[1]:match "/(%S*)")
  111. end
  112. end
  113. return list
  114. end
  115. function plugin_loader.load_snapshot(snapshot_file)
  116. snapshot_file = snapshot_file or default_snapshot
  117. if not in_headless then
  118. vim.notify("Syncing core plugins is in progress..", vim.log.levels.INFO, { title = "lvim" })
  119. end
  120. Log:debug(string.format("Using snapshot file [%s]", snapshot_file))
  121. local core_plugins = plugin_loader.get_core_plugins()
  122. require("packer").rollback(snapshot_file, unpack(core_plugins))
  123. end
  124. function plugin_loader.sync_core_plugins()
  125. -- problem: rollback() will get stuck if a plugin directory doesn't exist
  126. -- solution: call sync() beforehand
  127. -- see https://github.com/wbthomason/packer.nvim/issues/862
  128. vim.api.nvim_create_autocmd("User", {
  129. pattern = "PackerComplete",
  130. once = true,
  131. callback = function()
  132. require("lvim.plugin-loader").load_snapshot(default_snapshot)
  133. end,
  134. })
  135. pcall_packer_command "sync"
  136. end
  137. function plugin_loader.ensure_plugins()
  138. vim.api.nvim_create_autocmd("User", {
  139. pattern = "PackerComplete",
  140. once = true,
  141. callback = function()
  142. Log:debug "calling packer.clean()"
  143. pcall_packer_command "clean"
  144. end,
  145. })
  146. Log:debug "calling packer.install()"
  147. pcall_packer_command "install"
  148. end
  149. return plugin_loader