plugin-loader.lua 5.0 KB

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