plugin-loader.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. local plugin_loader = {}
  2. local utils = require "lvim.utils"
  3. local Log = require "lvim.core.log"
  4. -- we need to reuse this outside of init()
  5. local compile_path = get_config_dir() .. "/plugin/packer_compiled.lua"
  6. local _, packer = pcall(require, "packer")
  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. packer.init {
  16. package_root = package_root,
  17. compile_path = compile_path,
  18. log = { level = "warn" },
  19. git = { clone_timeout = 300 },
  20. display = {
  21. open_fn = function()
  22. return require("packer.util").float { border = "rounded" }
  23. end,
  24. },
  25. }
  26. end
  27. -- packer expects a space separated list
  28. local function pcall_packer_command(cmd, kwargs)
  29. local status_ok, msg = pcall(function()
  30. require("packer")[cmd](unpack(kwargs or {}))
  31. end)
  32. if not status_ok then
  33. Log:warn(cmd .. " failed with: " .. vim.inspect(msg))
  34. Log:trace(vim.inspect(vim.fn.eval "v:errmsg"))
  35. end
  36. end
  37. function plugin_loader.cache_clear()
  38. if vim.fn.delete(compile_path) == 0 then
  39. Log:debug "deleted packer_compiled.lua"
  40. end
  41. end
  42. function plugin_loader.recompile()
  43. plugin_loader.cache_clear()
  44. pcall_packer_command "compile"
  45. if utils.is_file(compile_path) then
  46. Log:debug "generated packer_compiled.lua"
  47. end
  48. end
  49. function plugin_loader.load(configurations)
  50. Log:debug "loading plugins configuration"
  51. local status_ok, _ = xpcall(function()
  52. packer.startup(function(use)
  53. for _, plugins in ipairs(configurations) do
  54. for _, plugin in ipairs(plugins) do
  55. use(plugin)
  56. end
  57. end
  58. end)
  59. end, debug.traceback)
  60. if not status_ok then
  61. Log:warn "problems detected while loading plugins' configurations"
  62. Log:trace(debug.traceback())
  63. end
  64. end
  65. function plugin_loader.get_core_plugins()
  66. local list = {}
  67. local plugins = require "lvim.plugins"
  68. for _, item in pairs(plugins) do
  69. table.insert(list, item[1]:match "/(%S*)")
  70. end
  71. return list
  72. end
  73. function plugin_loader.sync_core_plugins()
  74. local core_plugins = plugin_loader.get_core_plugins()
  75. Log:trace(string.format("Syncing core plugins: [%q]", table.concat(core_plugins, ", ")))
  76. pcall_packer_command("sync", core_plugins)
  77. end
  78. return plugin_loader