plugin-loader.lua 2.6 KB

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