bootstrap.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. local M = {}
  2. if vim.fn.has "nvim-0.8" ~= 1 then
  3. vim.notify("Please upgrade your Neovim base installation. Lunarvim requires v0.8+", 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. ---Join path segments that were passed as input
  12. ---@return string
  13. function _G.join_paths(...)
  14. local result = table.concat({ ... }, path_sep)
  15. return result
  16. end
  17. _G.require_clean = require("lvim.utils.modules").require_clean
  18. _G.require_safe = require("lvim.utils.modules").require_safe
  19. _G.reload = require("lvim.utils.modules").reload
  20. ---Get the full path to `$LUNARVIM_RUNTIME_DIR`
  21. ---@return string
  22. function _G.get_runtime_dir()
  23. local lvim_runtime_dir = os.getenv "LUNARVIM_RUNTIME_DIR"
  24. if not lvim_runtime_dir then
  25. -- when nvim is used directly
  26. return vim.call("stdpath", "data")
  27. end
  28. return lvim_runtime_dir
  29. end
  30. ---Get the full path to `$LUNARVIM_CONFIG_DIR`
  31. ---@return string
  32. function _G.get_config_dir()
  33. local lvim_config_dir = os.getenv "LUNARVIM_CONFIG_DIR"
  34. if not lvim_config_dir then
  35. return vim.call("stdpath", "config")
  36. end
  37. return lvim_config_dir
  38. end
  39. ---Get the full path to `$LUNARVIM_CACHE_DIR`
  40. ---@return string
  41. function _G.get_cache_dir()
  42. local lvim_cache_dir = os.getenv "LUNARVIM_CACHE_DIR"
  43. if not lvim_cache_dir then
  44. return vim.call("stdpath", "cache")
  45. end
  46. return lvim_cache_dir
  47. end
  48. ---Initialize the `&runtimepath` variables and prepare for startup
  49. ---@return table
  50. function M:init(base_dir)
  51. self.runtime_dir = get_runtime_dir()
  52. self.config_dir = get_config_dir()
  53. self.cache_dir = get_cache_dir()
  54. self.pack_dir = join_paths(self.runtime_dir, "site", "pack")
  55. self.packer_install_dir = join_paths(self.runtime_dir, "site", "pack", "packer", "start", "packer.nvim")
  56. self.packer_cache_path = join_paths(self.config_dir, "plugin", "packer_compiled.lua")
  57. ---@meta overridden to use LUNARVIM_CACHE_DIR instead, since a lot of plugins call this function internally
  58. ---NOTE: changes to "data" are currently unstable, see #2507
  59. vim.fn.stdpath = function(what)
  60. if what == "cache" then
  61. return _G.get_cache_dir()
  62. end
  63. return vim.call("stdpath", what)
  64. end
  65. ---Get the full path to LunarVim's base directory
  66. ---@return string
  67. function _G.get_lvim_base_dir()
  68. return base_dir
  69. end
  70. if os.getenv "LUNARVIM_RUNTIME_DIR" then
  71. -- vim.opt.rtp:append(os.getenv "LUNARVIM_RUNTIME_DIR" .. path_sep .. "lvim")
  72. vim.opt.rtp:remove(join_paths(vim.call("stdpath", "data"), "site"))
  73. vim.opt.rtp:remove(join_paths(vim.call("stdpath", "data"), "site", "after"))
  74. vim.opt.rtp:prepend(join_paths(self.runtime_dir, "site"))
  75. vim.opt.rtp:append(join_paths(self.runtime_dir, "lvim", "after"))
  76. vim.opt.rtp:append(join_paths(self.runtime_dir, "site", "after"))
  77. vim.opt.rtp:remove(vim.call("stdpath", "config"))
  78. vim.opt.rtp:remove(join_paths(vim.call("stdpath", "config"), "after"))
  79. vim.opt.rtp:prepend(self.config_dir)
  80. vim.opt.rtp:append(join_paths(self.config_dir, "after"))
  81. -- TODO: we need something like this: vim.opt.packpath = vim.opt.rtp
  82. vim.cmd [[let &packpath = &runtimepath]]
  83. end
  84. if not vim.env.LVIM_TEST_ENV then
  85. require "lvim.impatient"
  86. end
  87. require("lvim.config"):init()
  88. require("lvim.plugin-loader").init {
  89. package_root = self.pack_dir,
  90. install_path = self.packer_install_dir,
  91. }
  92. return self
  93. end
  94. ---Update LunarVim
  95. ---pulls the latest changes from github and, resets the startup cache
  96. function M:update()
  97. require("lvim.core.log"):info "Trying to update LunarVim..."
  98. vim.schedule(function()
  99. reload("lvim.utils.hooks").run_pre_update()
  100. local ret = reload("lvim.utils.git").update_base_lvim()
  101. if ret then
  102. reload("lvim.utils.hooks").run_post_update()
  103. end
  104. end)
  105. end
  106. return M