plugin-loader.lua 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. function plugin_loader:init(opts)
  7. opts = opts or {}
  8. local install_path = opts.install_path or vim.fn.stdpath "data" .. "/site/pack/packer/start/packer.nvim"
  9. local package_root = opts.package_root or vim.fn.stdpath "data" .. "/site/pack"
  10. if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
  11. vim.fn.system { "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path }
  12. vim.cmd "packadd packer.nvim"
  13. end
  14. local packer_ok, packer = pcall(require, "packer")
  15. if not packer_ok then
  16. return
  17. end
  18. packer.init {
  19. package_root = package_root,
  20. compile_path = compile_path,
  21. git = { clone_timeout = 300 },
  22. display = {
  23. open_fn = function()
  24. return require("packer.util").float { border = "rounded" }
  25. end,
  26. },
  27. }
  28. self.packer = packer
  29. return self
  30. end
  31. function plugin_loader:cache_clear()
  32. if vim.fn.delete(compile_path) == 0 then
  33. Log:debug "deleted packer_compiled.lua"
  34. end
  35. end
  36. function plugin_loader:cache_reset()
  37. plugin_loader:cache_clear()
  38. require("packer").compile()
  39. if utils.is_file(compile_path) then
  40. Log:debug "generated packer_compiled.lua"
  41. end
  42. end
  43. function plugin_loader:load(configurations)
  44. return self.packer.startup(function(use)
  45. for _, plugins in ipairs(configurations) do
  46. for _, plugin in ipairs(plugins) do
  47. use(plugin)
  48. end
  49. end
  50. end)
  51. end
  52. function plugin_loader:get_core_plugins()
  53. local list = {}
  54. local plugins = require "lvim.plugins"
  55. for _, item in pairs(plugins) do
  56. table.insert(list, item[1]:match "/(%S*)")
  57. end
  58. return list
  59. end
  60. function plugin_loader:sync_core_plugins()
  61. local core_plugins = plugin_loader.get_core_plugins()
  62. vim.cmd("PackerSync " .. unpack(core_plugins))
  63. end
  64. return plugin_loader