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