bootstrap.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. ---@diagnostic disable-next-line: redundant-return-value
  6. return false
  7. end)
  8. vim.cmd "cquit"
  9. end
  10. local uv = vim.loop
  11. local path_sep = uv.os_uname().version:match "Windows" and "\\" or "/"
  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. _G.require_clean = require("lvim.utils.modules").require_clean
  19. _G.require_safe = require("lvim.utils.modules").require_safe
  20. _G.reload = require("lvim.utils.modules").reload
  21. ---Get the full path to `$LUNARVIM_RUNTIME_DIR`
  22. ---@return string|nil
  23. function _G.get_runtime_dir()
  24. local lvim_runtime_dir = os.getenv "LUNARVIM_RUNTIME_DIR"
  25. if not lvim_runtime_dir then
  26. -- when nvim is used directly
  27. return vim.call("stdpath", "data")
  28. end
  29. return lvim_runtime_dir
  30. end
  31. ---Get the full path to `$LUNARVIM_CONFIG_DIR`
  32. ---@return string|nil
  33. function _G.get_config_dir()
  34. local lvim_config_dir = os.getenv "LUNARVIM_CONFIG_DIR"
  35. if not lvim_config_dir then
  36. return vim.call("stdpath", "config")
  37. end
  38. return lvim_config_dir
  39. end
  40. ---Get the full path to `$LUNARVIM_CACHE_DIR`
  41. ---@return string|nil
  42. function _G.get_cache_dir()
  43. local lvim_cache_dir = os.getenv "LUNARVIM_CACHE_DIR"
  44. if not lvim_cache_dir then
  45. return vim.call("stdpath", "cache")
  46. end
  47. return lvim_cache_dir
  48. end
  49. ---Initialize the `&runtimepath` variables and prepare for startup
  50. ---@return table
  51. function M:init(base_dir)
  52. self.runtime_dir = get_runtime_dir()
  53. self.config_dir = get_config_dir()
  54. self.cache_dir = get_cache_dir()
  55. self.pack_dir = join_paths(self.runtime_dir, "site", "pack")
  56. self.lazy_install_dir = join_paths(self.pack_dir, "lazy", "opt", "lazy.nvim")
  57. ---@meta overridden to use LUNARVIM_CACHE_DIR instead, since a lot of plugins call this function internally
  58. ---@diagnostic disable-next-line: duplicate-set-field
  59. vim.fn.stdpath = function(what)
  60. if what == "cache" then
  61. return _G.get_cache_dir()
  62. elseif what == "config" then
  63. return _G.get_config_dir()
  64. -- elseif what == "data" then
  65. -- return _G.get_runtime_dir()
  66. end
  67. return vim.call("stdpath", what)
  68. end
  69. vim.api.nvim_call_function = function(fn, args)
  70. if fn == "stdpath" then
  71. return vim.fn.stdpath(args[1])
  72. end
  73. return vim.call(fn, unpack(args))
  74. end
  75. ---Get the full path to LunarVim's base directory
  76. ---@return string
  77. function _G.get_lvim_base_dir()
  78. return base_dir
  79. end
  80. if os.getenv "LUNARVIM_RUNTIME_DIR" then
  81. vim.opt.rtp:remove(join_paths(vim.call("stdpath", "data"), "site"))
  82. vim.opt.rtp:remove(join_paths(vim.call("stdpath", "data"), "site", "after"))
  83. -- vim.opt.rtp:prepend(join_paths(self.runtime_dir, "site"))
  84. vim.opt.rtp:append(join_paths(self.runtime_dir, "lvim", "after"))
  85. vim.opt.rtp:append(join_paths(self.runtime_dir, "site", "after"))
  86. vim.opt.rtp:remove(vim.call("stdpath", "config"))
  87. vim.opt.rtp:remove(join_paths(vim.call("stdpath", "config"), "after"))
  88. vim.opt.rtp:prepend(self.config_dir)
  89. vim.opt.rtp:append(join_paths(self.config_dir, "after"))
  90. vim.opt.packpath = vim.opt.rtp:get()
  91. end
  92. require("lvim.plugin-loader").init {
  93. package_root = self.pack_dir,
  94. install_path = self.lazy_install_dir,
  95. }
  96. require("lvim.config"):init()
  97. require("lvim.core.mason").bootstrap()
  98. return self
  99. end
  100. ---Update LunarVim
  101. ---pulls the latest changes from github and, resets the startup cache
  102. function M:update()
  103. require("lvim.core.log"):info "Trying to update LunarVim..."
  104. vim.schedule(function()
  105. reload("lvim.utils.hooks").run_pre_update()
  106. local ret = reload("lvim.utils.git").update_base_lvim()
  107. if ret then
  108. reload("lvim.utils.hooks").run_post_update()
  109. end
  110. end)
  111. end
  112. return M