plugin-loader.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. print "Initializing first time setup"
  38. print "Installing packer"
  39. print(vim.fn.system { "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path })
  40. vim.cmd "packadd packer.nvim"
  41. end
  42. local status_ok, packer = pcall(require, "packer")
  43. if status_ok then
  44. packer.on_complete = vim.schedule_wrap(function()
  45. require("lvim.utils.hooks").run_on_packer_complete()
  46. end)
  47. packer.init(init_opts)
  48. end
  49. end
  50. -- packer expects a space separated list
  51. local function pcall_packer_command(cmd, kwargs)
  52. local status_ok, msg = pcall(function()
  53. require("packer")[cmd](unpack(kwargs or {}))
  54. end)
  55. if not status_ok then
  56. Log:warn(cmd .. " failed with: " .. vim.inspect(msg))
  57. Log:trace(vim.inspect(vim.fn.eval "v:errmsg"))
  58. end
  59. end
  60. function plugin_loader.cache_clear()
  61. if not utils.is_file(compile_path) then
  62. return
  63. end
  64. if vim.fn.delete(compile_path) == 0 then
  65. Log:debug "deleted packer_compiled.lua"
  66. end
  67. end
  68. function plugin_loader.compile()
  69. Log:debug "calling packer.compile()"
  70. vim.api.nvim_create_autocmd("User", {
  71. pattern = "PackerCompileDone",
  72. once = true,
  73. callback = function()
  74. if utils.is_file(compile_path) then
  75. Log:debug "finished compiling packer_compiled.lua"
  76. end
  77. end,
  78. })
  79. pcall_packer_command "compile"
  80. end
  81. function plugin_loader.recompile()
  82. plugin_loader.cache_clear()
  83. plugin_loader.compile()
  84. end
  85. function plugin_loader.reload(configurations)
  86. _G.packer_plugins = _G.packer_plugins or {}
  87. for k, v in pairs(_G.packer_plugins) do
  88. if k ~= "packer.nvim" then
  89. _G.packer_plugins[v] = nil
  90. end
  91. end
  92. plugin_loader.load(configurations)
  93. plugin_loader.ensure_plugins()
  94. end
  95. function plugin_loader.load(configurations)
  96. Log:debug "loading plugins configuration"
  97. local packer_available, packer = pcall(require, "packer")
  98. if not packer_available then
  99. Log:warn "skipping loading plugins until Packer is installed"
  100. return
  101. end
  102. local status_ok, _ = xpcall(function()
  103. packer.reset()
  104. packer.startup(function(use)
  105. for _, plugins in ipairs(configurations) do
  106. for _, plugin in ipairs(plugins) do
  107. use(plugin)
  108. end
  109. end
  110. end)
  111. end, debug.traceback)
  112. if not status_ok then
  113. Log:warn "problems detected while loading plugins' configurations"
  114. Log:trace(debug.traceback())
  115. end
  116. end
  117. function plugin_loader.get_core_plugins()
  118. local list = {}
  119. local plugins = require "lvim.plugins"
  120. for _, item in pairs(plugins) do
  121. if not item.disable then
  122. table.insert(list, item[1]:match "/(%S*)")
  123. end
  124. end
  125. return list
  126. end
  127. function plugin_loader.load_snapshot(snapshot_file)
  128. snapshot_file = snapshot_file or default_snapshot
  129. if not in_headless then
  130. vim.notify("Syncing core plugins is in progress..", vim.log.levels.INFO, { title = "lvim" })
  131. end
  132. Log:debug(string.format("Using snapshot file [%s]", snapshot_file))
  133. local core_plugins = plugin_loader.get_core_plugins()
  134. require("packer").rollback(snapshot_file, unpack(core_plugins))
  135. end
  136. function plugin_loader.sync_core_plugins()
  137. plugin_loader.cache_clear()
  138. local core_plugins = plugin_loader.get_core_plugins()
  139. Log:trace(string.format("Syncing core plugins: [%q]", table.concat(core_plugins, ", ")))
  140. pcall_packer_command("sync", core_plugins)
  141. end
  142. function plugin_loader.ensure_plugins()
  143. vim.api.nvim_create_autocmd("User", {
  144. pattern = "PackerComplete",
  145. once = true,
  146. callback = function()
  147. plugin_loader.compile()
  148. end,
  149. })
  150. Log:debug "calling packer.install()"
  151. pcall_packer_command "install"
  152. end
  153. return plugin_loader