plugin-loader.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 = {
  25. clone_timeout = 300,
  26. subcommands = {
  27. -- this is more efficient than what Packer is using
  28. fetch = "fetch --no-tags --no-recurse-submodules --update-shallow --progress",
  29. },
  30. },
  31. max_jobs = 50,
  32. display = {
  33. open_fn = function()
  34. return require("packer.util").float { border = "rounded" }
  35. end,
  36. },
  37. }
  38. if not in_headless then
  39. vim.cmd [[autocmd User PackerComplete lua require('lvim.utils.hooks').run_on_packer_complete()]]
  40. end
  41. end
  42. -- packer expects a space separated list
  43. local function pcall_packer_command(cmd, kwargs)
  44. local status_ok, msg = pcall(function()
  45. require("packer")[cmd](unpack(kwargs or {}))
  46. end)
  47. if not status_ok then
  48. Log:warn(cmd .. " failed with: " .. vim.inspect(msg))
  49. Log:trace(vim.inspect(vim.fn.eval "v:errmsg"))
  50. end
  51. end
  52. function plugin_loader.cache_clear()
  53. if vim.fn.delete(compile_path) == 0 then
  54. Log:debug "deleted packer_compiled.lua"
  55. end
  56. end
  57. function plugin_loader.recompile()
  58. plugin_loader.cache_clear()
  59. pcall_packer_command "compile"
  60. if utils.is_file(compile_path) then
  61. Log:debug "generated packer_compiled.lua"
  62. end
  63. end
  64. function plugin_loader.load(configurations)
  65. Log:debug "loading plugins configuration"
  66. local packer_available, packer = pcall(require, "packer")
  67. if not packer_available then
  68. Log:warn "skipping loading plugins until Packer is installed"
  69. return
  70. end
  71. local status_ok, _ = xpcall(function()
  72. packer.startup(function(use)
  73. for _, plugins in ipairs(configurations) do
  74. for _, plugin in ipairs(plugins) do
  75. use(plugin)
  76. end
  77. end
  78. end)
  79. -- colorscheme must get called after plugins are loaded or it will break new installs.
  80. vim.g.colors_name = lvim.colorscheme
  81. vim.cmd("colorscheme " .. lvim.colorscheme)
  82. end, debug.traceback)
  83. if not status_ok then
  84. Log:warn "problems detected while loading plugins' configurations"
  85. Log:trace(debug.traceback())
  86. end
  87. end
  88. function plugin_loader.get_core_plugins()
  89. local list = {}
  90. local plugins = require "lvim.plugins"
  91. for _, item in pairs(plugins) do
  92. table.insert(list, item[1]:match "/(%S*)")
  93. end
  94. return list
  95. end
  96. function plugin_loader.sync_core_plugins()
  97. local core_plugins = plugin_loader.get_core_plugins()
  98. Log:trace(string.format("Syncing core plugins: [%q]", table.concat(core_plugins, ", ")))
  99. pcall_packer_command("sync", core_plugins)
  100. end
  101. function plugin_loader.ensure_installed()
  102. local all_plugins = _G.packer_plugins or plugin_loader.get_core_plugins()
  103. Log:trace(string.format("Syncing core plugins: [%q]", table.concat(all_plugins, ", ")))
  104. pcall_packer_command("install", all_plugins)
  105. end
  106. return plugin_loader