plugin-loader.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.loop.fs_rmdir(plugins_dir)
  16. require("lvim.utils").fs_copy(core_plugins_dir, plugins_dir)
  17. else
  18. vim.fn.system {
  19. "git",
  20. "clone",
  21. "--filter=blob:none",
  22. "--branch=stable",
  23. "https://github.com/folke/lazy.nvim.git",
  24. lazy_install_dir,
  25. }
  26. local default_snapshot_path = join_paths(get_lvim_base_dir(), "snapshots", "default.json")
  27. local snapshot = assert(vim.fn.json_decode(vim.fn.readfile(default_snapshot_path)))
  28. vim.fn.system {
  29. "git",
  30. "-C",
  31. lazy_install_dir,
  32. "checkout",
  33. snapshot["lazy.nvim"].commit,
  34. }
  35. end
  36. end
  37. vim.opt.runtimepath:append(lazy_install_dir)
  38. vim.opt.runtimepath:append(join_paths(plugins_dir, "*"))
  39. local lazy_cache = require "lazy.core.cache"
  40. lazy_cache.setup {
  41. performance = {
  42. cache = {
  43. enabled = true,
  44. path = join_paths(get_cache_dir(), "lazy", "cache"),
  45. },
  46. },
  47. }
  48. -- HACK: Don't allow lazy to call setup second time
  49. lazy_cache.setup = function() end
  50. end
  51. function plugin_loader.reset_cache()
  52. os.remove(require("lazy.core.cache").config.path)
  53. end
  54. function plugin_loader.reload(spec)
  55. local Config = require "lazy.core.config"
  56. local lazy = require "lazy"
  57. -- TODO: reset cache? and unload plugins?
  58. Config.spec = spec
  59. require("lazy.core.plugin").load(true)
  60. require("lazy.core.plugin").update_state()
  61. local not_installed_plugins = vim.tbl_filter(function(plugin)
  62. return not plugin._.installed
  63. end, Config.plugins)
  64. require("lazy.manage").clear()
  65. if #not_installed_plugins > 0 then
  66. lazy.install { wait = true }
  67. end
  68. if #Config.to_clean > 0 then
  69. -- TODO: set show to true when lazy shows something useful on clean
  70. lazy.clean { wait = true, show = false }
  71. end
  72. end
  73. function plugin_loader.load(configurations)
  74. Log:debug "loading plugins configuration"
  75. local lazy_available, lazy = pcall(require, "lazy")
  76. if not lazy_available then
  77. Log:warn "skipping loading plugins until lazy.nvim is installed"
  78. return
  79. end
  80. -- remove plugins from rtp before loading lazy, so that all plugins won't be loaded on startup
  81. vim.opt.runtimepath:remove(join_paths(plugins_dir, "*"))
  82. local status_ok = xpcall(function()
  83. local opts = {
  84. install = {
  85. missing = true,
  86. colorscheme = { lvim.colorscheme, "lunar", "habamax" },
  87. },
  88. ui = {
  89. border = "rounded",
  90. },
  91. root = plugins_dir,
  92. git = {
  93. timeout = 120,
  94. },
  95. lockfile = join_paths(get_config_dir(), "lazy-lock.json"),
  96. performance = {
  97. rtp = {
  98. reset = false,
  99. },
  100. },
  101. readme = {
  102. root = join_paths(get_runtime_dir(), "lazy", "readme"),
  103. },
  104. }
  105. lazy.setup(configurations, opts)
  106. end, debug.traceback)
  107. if not status_ok then
  108. Log:warn "problems detected while loading plugins' configurations"
  109. Log:trace(debug.traceback())
  110. end
  111. end
  112. function plugin_loader.get_core_plugins()
  113. local names = {}
  114. local plugins = require "lvim.plugins"
  115. local get_name = require("lazy.core.plugin").Spec.get_name
  116. for _, spec in pairs(plugins) do
  117. if spec.enabled == true or spec.enabled == nil then
  118. table.insert(names, get_name(spec[1]))
  119. end
  120. end
  121. return names
  122. end
  123. function plugin_loader.sync_core_plugins()
  124. local core_plugins = plugin_loader.get_core_plugins()
  125. Log:trace(string.format("Syncing core plugins: [%q]", table.concat(core_plugins, ", ")))
  126. require("lazy").sync { wait = true, plugins = core_plugins }
  127. end
  128. function plugin_loader.ensure_plugins()
  129. Log:debug "calling lazy.install()"
  130. require("lazy").install { wait = true }
  131. end
  132. return plugin_loader