bootstrap.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. local M = {}
  2. if vim.fn.has "nvim-0.7" ~= 1 then
  3. vim.notify("Please upgrade your Neovim base installation. Lunarvim requires v0.7+", vim.log.levels.WARN)
  4. vim.wait(5000, function()
  5. return false
  6. end)
  7. vim.cmd "cquit"
  8. end
  9. local uv = vim.loop
  10. local path_sep = uv.os_uname().version:match "Windows" and "\\" or "/"
  11. local in_headless = #vim.api.nvim_list_uis() == 0
  12. ---Join path segments that were passed as input
  13. ---@return string
  14. function _G.join_paths(...)
  15. local result = table.concat({ ... }, path_sep)
  16. return result
  17. end
  18. ---Require a module in protected mode without relying on its cached value
  19. ---@param module string
  20. ---@return any
  21. function _G.require_clean(module)
  22. package.loaded[module] = nil
  23. _G[module] = nil
  24. local _, requested = pcall(require, module)
  25. return requested
  26. end
  27. ---Get the full path to `$LUNARVIM_RUNTIME_DIR`
  28. ---@return string
  29. function _G.get_runtime_dir()
  30. local lvim_runtime_dir = os.getenv "LUNARVIM_RUNTIME_DIR"
  31. if not lvim_runtime_dir then
  32. -- when nvim is used directly
  33. return vim.call("stdpath", "data")
  34. end
  35. return lvim_runtime_dir
  36. end
  37. ---Get the full path to `$LUNARVIM_CONFIG_DIR`
  38. ---@return string
  39. function _G.get_config_dir()
  40. local lvim_config_dir = os.getenv "LUNARVIM_CONFIG_DIR"
  41. if not lvim_config_dir then
  42. return vim.call("stdpath", "config")
  43. end
  44. return lvim_config_dir
  45. end
  46. ---Get the full path to `$LUNARVIM_CACHE_DIR`
  47. ---@return string
  48. function _G.get_cache_dir()
  49. local lvim_cache_dir = os.getenv "LUNARVIM_CACHE_DIR"
  50. if not lvim_cache_dir then
  51. return vim.call("stdpath", "cache")
  52. end
  53. return lvim_cache_dir
  54. end
  55. ---Initialize the `&runtimepath` variables and prepare for startup
  56. ---@return table
  57. function M:init(base_dir)
  58. self.runtime_dir = get_runtime_dir()
  59. self.config_dir = get_config_dir()
  60. self.cache_dir = get_cache_dir()
  61. self.pack_dir = join_paths(self.runtime_dir, "site", "pack")
  62. self.packer_install_dir = join_paths(self.runtime_dir, "site", "pack", "packer", "start", "packer.nvim")
  63. self.packer_cache_path = join_paths(self.config_dir, "plugin", "packer_compiled.lua")
  64. ---@meta overridden to use LUNARVIM_CACHE_DIR instead, since a lot of plugins call this function internally
  65. ---NOTE: changes to "data" are currently unstable, see #2507
  66. vim.fn.stdpath = function(what)
  67. if what == "cache" then
  68. return _G.get_cache_dir()
  69. end
  70. return vim.call("stdpath", what)
  71. end
  72. ---Get the full path to LunarVim's base directory
  73. ---@return string
  74. function _G.get_lvim_base_dir()
  75. return base_dir
  76. end
  77. if os.getenv "LUNARVIM_RUNTIME_DIR" then
  78. -- vim.opt.rtp:append(os.getenv "LUNARVIM_RUNTIME_DIR" .. path_sep .. "lvim")
  79. vim.opt.rtp:remove(join_paths(vim.call("stdpath", "data"), "site"))
  80. vim.opt.rtp:remove(join_paths(vim.call("stdpath", "data"), "site", "after"))
  81. vim.opt.rtp:prepend(join_paths(self.runtime_dir, "site"))
  82. vim.opt.rtp:append(join_paths(self.runtime_dir, "lvim", "after"))
  83. vim.opt.rtp:append(join_paths(self.runtime_dir, "site", "after"))
  84. vim.opt.rtp:remove(vim.call("stdpath", "config"))
  85. vim.opt.rtp:remove(join_paths(vim.call("stdpath", "config"), "after"))
  86. vim.opt.rtp:prepend(self.config_dir)
  87. vim.opt.rtp:append(join_paths(self.config_dir, "after"))
  88. -- TODO: we need something like this: vim.opt.packpath = vim.opt.rtp
  89. vim.cmd [[let &packpath = &runtimepath]]
  90. end
  91. -- FIXME: currently unreliable in unit-tests
  92. if not in_headless then
  93. _G.PLENARY_DEBUG = false
  94. require "lvim.impatient"
  95. end
  96. require("lvim.config"):init()
  97. require("lvim.plugin-loader").init {
  98. package_root = self.pack_dir,
  99. install_path = self.packer_install_dir,
  100. }
  101. return self
  102. end
  103. ---Update LunarVim
  104. ---pulls the latest changes from github and, resets the startup cache
  105. function M:update()
  106. require_clean("lvim.utils.hooks").run_pre_update()
  107. local ret = require_clean("lvim.utils.git").update_base_lvim()
  108. if ret then
  109. require_clean("lvim.utils.hooks").run_post_update()
  110. end
  111. end
  112. return M