plugin-loader.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. local plugin_loader = {}
  2. local in_headless = #vim.api.nvim_list_uis() == 0
  3. local utils = require "lvim.utils"
  4. local Log = require "lvim.core.log"
  5. -- we need to reuse this outside of init()
  6. local compile_path = get_config_dir() .. "/plugin/packer_compiled.lua"
  7. function plugin_loader.init(opts)
  8. opts = opts or {}
  9. local install_path = opts.install_path or vim.fn.stdpath "data" .. "/site/pack/packer/start/packer.nvim"
  10. local package_root = opts.package_root or vim.fn.stdpath "data" .. "/site/pack"
  11. if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
  12. vim.fn.system { "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path }
  13. vim.cmd "packadd packer.nvim"
  14. end
  15. local log_level = in_headless and "debug" or "warn"
  16. if lvim.log and lvim.log.level then
  17. log_level = lvim.log.level
  18. end
  19. local _, packer = pcall(require, "packer")
  20. packer.init {
  21. package_root = package_root,
  22. compile_path = compile_path,
  23. log = { level = log_level },
  24. git = { clone_timeout = 300 },
  25. max_jobs = 50,
  26. display = {
  27. open_fn = function()
  28. return require("packer.util").float { border = "rounded" }
  29. end,
  30. },
  31. }
  32. end
  33. -- packer expects a space separated list
  34. local function pcall_packer_command(cmd, kwargs)
  35. local status_ok, msg = pcall(function()
  36. require("packer")[cmd](unpack(kwargs or {}))
  37. end)
  38. if not status_ok then
  39. Log:warn(cmd .. " failed with: " .. vim.inspect(msg))
  40. Log:trace(vim.inspect(vim.fn.eval "v:errmsg"))
  41. end
  42. end
  43. function plugin_loader.cache_clear()
  44. if vim.fn.delete(compile_path) == 0 then
  45. Log:debug "deleted packer_compiled.lua"
  46. end
  47. end
  48. function plugin_loader.recompile()
  49. plugin_loader.cache_clear()
  50. pcall_packer_command "compile"
  51. if utils.is_file(compile_path) then
  52. Log:debug "generated packer_compiled.lua"
  53. end
  54. end
  55. function plugin_loader.load(configurations)
  56. Log:debug "loading plugins configuration"
  57. local packer_available, packer = pcall(require, "packer")
  58. if not packer_available then
  59. Log:warn "skipping loading plugins until Packer is installed"
  60. return
  61. end
  62. local status_ok, _ = xpcall(function()
  63. packer.startup(function(use)
  64. for _, plugins in ipairs(configurations) do
  65. for _, plugin in ipairs(plugins) do
  66. use(plugin)
  67. end
  68. end
  69. end)
  70. end, debug.traceback)
  71. if not status_ok then
  72. Log:warn "problems detected while loading plugins' configurations"
  73. Log:trace(debug.traceback())
  74. end
  75. -- Colorscheme must get called after plugins are loaded or it will break new installs.
  76. vim.g.colors_name = lvim.colorscheme
  77. vim.cmd("colorscheme " .. lvim.colorscheme)
  78. end
  79. function plugin_loader.get_core_plugins()
  80. local list = {}
  81. local plugins = require "lvim.plugins"
  82. for _, item in pairs(plugins) do
  83. table.insert(list, item[1]:match "/(%S*)")
  84. end
  85. return list
  86. end
  87. function plugin_loader.sync_core_plugins()
  88. local core_plugins = plugin_loader.get_core_plugins()
  89. Log:trace(string.format("Syncing core plugins: [%q]", table.concat(core_plugins, ", ")))
  90. pcall_packer_command("sync", core_plugins)
  91. end
  92. return plugin_loader