plugin-loader.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. local plugin_loader = {}
  2. local utils = require "lvim.utils"
  3. local Log = require "lvim.core.log"
  4. local join_paths = utils.join_paths
  5. local plugins_dir = join_paths(get_runtime_dir(), "site", "pack", "lazy", "opt")
  6. function plugin_loader.init(opts)
  7. opts = opts or {}
  8. local lazy_install_dir = opts.install_path
  9. or join_paths(vim.fn.stdpath "data", "site", "pack", "lazy", "opt", "lazy.nvim")
  10. if not utils.is_directory(lazy_install_dir) then
  11. print "Initializing first time setup"
  12. local core_plugins_dir = join_paths(get_lvim_base_dir(), "plugins")
  13. if utils.is_directory(core_plugins_dir) then
  14. vim.fn.mkdir(plugins_dir, "p")
  15. vim.fn.delete(plugins_dir, "rf")
  16. require("lvim.utils").fs_copy(core_plugins_dir, plugins_dir)
  17. else
  18. vim.fn.system {
  19. "git",
  20. "clone",
  21. "--branch=stable",
  22. "https://github.com/folke/lazy.nvim.git",
  23. lazy_install_dir,
  24. }
  25. local default_snapshot_path = join_paths(get_lvim_base_dir(), "snapshots", "default.json")
  26. local snapshot = assert(vim.fn.json_decode(vim.fn.readfile(default_snapshot_path)))
  27. vim.fn.system {
  28. "git",
  29. "-C",
  30. lazy_install_dir,
  31. "checkout",
  32. snapshot["lazy.nvim"].commit,
  33. }
  34. end
  35. vim.schedule(function()
  36. require("lvim.lsp").setup()
  37. end)
  38. end
  39. vim.opt.runtimepath:append(lazy_install_dir)
  40. vim.opt.runtimepath:append(join_paths(plugins_dir, "*"))
  41. pcall(function()
  42. -- set a custom path for lazy's cache
  43. local lazy_cache = require "lazy.core.cache"
  44. lazy_cache.path = join_paths(get_cache_dir(), "lazy", "luac")
  45. end)
  46. end
  47. function plugin_loader.reload(spec)
  48. local Config = require "lazy.core.config"
  49. local lazy = require "lazy"
  50. -- TODO: reset cache? and unload plugins?
  51. Config.spec = spec
  52. require("lazy.core.plugin").load(true)
  53. require("lazy.core.plugin").update_state()
  54. local not_installed_plugins = vim.tbl_filter(function(plugin)
  55. return not plugin._.installed
  56. end, Config.plugins)
  57. require("lazy.manage").clear()
  58. if #not_installed_plugins > 0 then
  59. lazy.install { wait = true }
  60. end
  61. if #Config.to_clean > 0 then
  62. -- TODO: set show to true when lazy shows something useful on clean
  63. lazy.clean { wait = true, show = false }
  64. end
  65. end
  66. function plugin_loader.load(configurations)
  67. Log:debug "loading plugins configuration"
  68. local lazy_available, lazy = pcall(require, "lazy")
  69. if not lazy_available then
  70. Log:warn "skipping loading plugins until lazy.nvim is installed"
  71. return
  72. end
  73. -- remove plugins from rtp before loading lazy, so that all plugins won't be loaded on startup
  74. vim.opt.runtimepath:remove(join_paths(plugins_dir, "*"))
  75. local status_ok = xpcall(function()
  76. table.insert(lvim.lazy.opts.install.colorscheme, 1, lvim.colorscheme)
  77. lazy.setup(configurations, lvim.lazy.opts)
  78. end, debug.traceback)
  79. if not status_ok then
  80. Log:warn "problems detected while loading plugins' configurations"
  81. Log:trace(debug.traceback())
  82. end
  83. end
  84. function plugin_loader.get_core_plugins()
  85. local names = {}
  86. local plugins = require "lvim.plugins"
  87. local get_name = require("lazy.core.plugin").Spec.get_name
  88. for _, spec in pairs(plugins) do
  89. if spec.enabled == true or spec.enabled == nil then
  90. table.insert(names, get_name(spec[1]))
  91. end
  92. end
  93. return names
  94. end
  95. function plugin_loader.sync_core_plugins()
  96. local core_plugins = plugin_loader.get_core_plugins()
  97. Log:trace(string.format("Syncing core plugins: [%q]", table.concat(core_plugins, ", ")))
  98. require("lazy").update { wait = true, plugins = core_plugins }
  99. end
  100. function plugin_loader.ensure_plugins()
  101. Log:debug "calling lazy.install()"
  102. require("lazy").install { wait = true }
  103. end
  104. return plugin_loader