plugin-loader.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 and enable it
  43. local lazy_cache = require "lazy.core.cache"
  44. lazy_cache.path = join_paths(get_cache_dir(), "lazy", "luac")
  45. lazy_cache.enable()
  46. end)
  47. end
  48. function plugin_loader.reset_cache()
  49. -- TODO(kylo252): is this really necessary anymore?
  50. local lazy_cache = require "lazy.core.cache"
  51. local cache_path = lazy_cache.path
  52. if utils.is_directory(cache_path) then
  53. vim.fn.delete(cache_path, "rf")
  54. vim.fn.mkdir(cache_path, "p")
  55. end
  56. end
  57. function plugin_loader.reload(spec)
  58. local Config = require "lazy.core.config"
  59. local lazy = require "lazy"
  60. -- TODO: reset cache? and unload plugins?
  61. Config.spec = spec
  62. require("lazy.core.plugin").load(true)
  63. require("lazy.core.plugin").update_state()
  64. local not_installed_plugins = vim.tbl_filter(function(plugin)
  65. return not plugin._.installed
  66. end, Config.plugins)
  67. require("lazy.manage").clear()
  68. if #not_installed_plugins > 0 then
  69. lazy.install { wait = true }
  70. end
  71. if #Config.to_clean > 0 then
  72. -- TODO: set show to true when lazy shows something useful on clean
  73. lazy.clean { wait = true, show = false }
  74. end
  75. end
  76. function plugin_loader.load(configurations)
  77. Log:debug "loading plugins configuration"
  78. local lazy_available, lazy = pcall(require, "lazy")
  79. if not lazy_available then
  80. Log:warn "skipping loading plugins until lazy.nvim is installed"
  81. return
  82. end
  83. -- remove plugins from rtp before loading lazy, so that all plugins won't be loaded on startup
  84. vim.opt.runtimepath:remove(join_paths(plugins_dir, "*"))
  85. local status_ok = xpcall(function()
  86. local opts = {
  87. install = {
  88. missing = true,
  89. colorscheme = { lvim.colorscheme, "lunar", "habamax" },
  90. },
  91. ui = {
  92. border = "rounded",
  93. },
  94. root = plugins_dir,
  95. git = {
  96. timeout = 120,
  97. },
  98. lockfile = join_paths(get_config_dir(), "lazy-lock.json"),
  99. performance = {
  100. rtp = {
  101. reset = false,
  102. },
  103. },
  104. defaults = {
  105. lazy = false,
  106. version = nil,
  107. },
  108. readme = {
  109. root = join_paths(get_runtime_dir(), "lazy", "readme"),
  110. },
  111. }
  112. lazy.setup(configurations, opts)
  113. end, debug.traceback)
  114. if not status_ok then
  115. Log:warn "problems detected while loading plugins' configurations"
  116. Log:trace(debug.traceback())
  117. end
  118. end
  119. function plugin_loader.get_core_plugins()
  120. local names = {}
  121. local plugins = require "lvim.plugins"
  122. local get_name = require("lazy.core.plugin").Spec.get_name
  123. for _, spec in pairs(plugins) do
  124. if spec.enabled == true or spec.enabled == nil then
  125. table.insert(names, get_name(spec[1]))
  126. end
  127. end
  128. return names
  129. end
  130. function plugin_loader.sync_core_plugins()
  131. local core_plugins = plugin_loader.get_core_plugins()
  132. Log:trace(string.format("Syncing core plugins: [%q]", table.concat(core_plugins, ", ")))
  133. require("lazy").update { wait = true, plugins = core_plugins }
  134. end
  135. function plugin_loader.ensure_plugins()
  136. Log:debug "calling lazy.install()"
  137. require("lazy").install { wait = true }
  138. end
  139. return plugin_loader