bootstrap.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. local M = {}
  2. local uv = vim.loop
  3. local path_sep = uv.os_uname().version:match "Windows" and "\\" or "/"
  4. local in_headless = #vim.api.nvim_list_uis() == 0
  5. ---Join path segments that were passed as input
  6. ---@return string
  7. function _G.join_paths(...)
  8. local result = table.concat({ ... }, path_sep)
  9. return result
  10. end
  11. ---Get the full path to `$LUNARVIM_RUNTIME_DIR`
  12. ---@return string
  13. function _G.get_runtime_dir()
  14. local lvim_runtime_dir = os.getenv "LUNARVIM_RUNTIME_DIR"
  15. if not lvim_runtime_dir then
  16. -- when nvim is used directly
  17. return vim.fn.stdpath "data"
  18. end
  19. return lvim_runtime_dir
  20. end
  21. ---Get the full path to `$LUNARVIM_CONFIG_DIR`
  22. ---@return string
  23. function _G.get_config_dir()
  24. local lvim_config_dir = os.getenv "LUNARVIM_CONFIG_DIR"
  25. if not lvim_config_dir then
  26. return vim.fn.stdpath "config"
  27. end
  28. return lvim_config_dir
  29. end
  30. ---Get the full path to `$LUNARVIM_CACHE_DIR`
  31. ---@return string
  32. function _G.get_cache_dir()
  33. local lvim_cache_dir = os.getenv "LUNARVIM_CACHE_DIR"
  34. if not lvim_cache_dir then
  35. return vim.fn.stdpath "cache"
  36. end
  37. return lvim_cache_dir
  38. end
  39. ---Initialize the `&runtimepath` variables and prepare for startup
  40. ---@return table
  41. function M:init(base_dir)
  42. self.runtime_dir = get_runtime_dir()
  43. self.config_dir = get_config_dir()
  44. self.cache_dir = get_cache_dir()
  45. self.pack_dir = join_paths(self.runtime_dir, "site", "pack")
  46. self.packer_install_dir = join_paths(self.runtime_dir, "site", "pack", "packer", "start", "packer.nvim")
  47. self.packer_cache_path = join_paths(self.config_dir, "plugin", "packer_compiled.lua")
  48. ---Get the full path to LunarVim's base directory
  49. ---@return string
  50. function _G.get_lvim_base_dir()
  51. return base_dir
  52. end
  53. if os.getenv "LUNARVIM_RUNTIME_DIR" then
  54. -- vim.opt.rtp:append(os.getenv "LUNARVIM_RUNTIME_DIR" .. path_sep .. "lvim")
  55. vim.opt.rtp:remove(join_paths(vim.fn.stdpath "data", "site"))
  56. vim.opt.rtp:remove(join_paths(vim.fn.stdpath "data", "site", "after"))
  57. vim.opt.rtp:prepend(join_paths(self.runtime_dir, "site"))
  58. vim.opt.rtp:append(join_paths(self.runtime_dir, "site", "after"))
  59. vim.opt.rtp:remove(vim.fn.stdpath "config")
  60. vim.opt.rtp:remove(join_paths(vim.fn.stdpath "config", "after"))
  61. vim.opt.rtp:prepend(self.config_dir)
  62. vim.opt.rtp:append(join_paths(self.config_dir, "after"))
  63. -- TODO: we need something like this: vim.opt.packpath = vim.opt.rtp
  64. vim.cmd [[let &packpath = &runtimepath]]
  65. vim.cmd("set spellfile=" .. join_paths(self.config_dir, "spell", "en.utf-8.add"))
  66. end
  67. vim.fn.mkdir(get_cache_dir(), "p")
  68. -- FIXME: currently unreliable in unit-tests
  69. if not in_headless then
  70. _G.PLENARY_DEBUG = false
  71. require("lvim.impatient").setup {
  72. path = join_paths(self.cache_dir, "lvim_cache"),
  73. enable_profiling = true,
  74. }
  75. end
  76. require("lvim.config"):init()
  77. require("lvim.plugin-loader").init {
  78. package_root = self.pack_dir,
  79. install_path = self.packer_install_dir,
  80. }
  81. return self
  82. end
  83. ---Update LunarVim
  84. ---pulls the latest changes from github and, resets the startup cache
  85. function M:update()
  86. package.loaded["lvim.utils.hooks"] = nil
  87. local _, hooks = pcall(require, "lvim.utils.hooks")
  88. hooks.run_pre_update()
  89. M:update_repo()
  90. hooks.run_post_update()
  91. end
  92. local function git_cmd(subcmd, opts)
  93. local Job = require "plenary.job"
  94. local Log = require "lvim.core.log"
  95. local args = { "-C", opts.cwd }
  96. vim.list_extend(args, subcmd)
  97. local stderr = {}
  98. local stdout, ret = Job
  99. :new({
  100. command = "git",
  101. args = args,
  102. cwd = opts.cwd,
  103. on_stderr = function(_, data)
  104. table.insert(stderr, data)
  105. end,
  106. })
  107. :sync()
  108. if not vim.tbl_isempty(stderr) then
  109. Log:debug(stderr)
  110. end
  111. if not vim.tbl_isempty(stdout) then
  112. Log:debug(stdout)
  113. end
  114. return ret, stdout
  115. end
  116. ---pulls the latest changes from github
  117. function M:update_repo()
  118. local Log = require "lvim.core.log"
  119. local sub_commands = {
  120. fetch = { "fetch" },
  121. diff = { "diff", "--quiet", "@{upstream}" },
  122. merge = { "merge", "--ff-only", "--progress" },
  123. }
  124. local opts = {
  125. cwd = get_lvim_base_dir(),
  126. }
  127. Log:info "Checking for updates"
  128. local ret = git_cmd(sub_commands.fetch, opts)
  129. if ret ~= 0 then
  130. Log:error "Update failed! Check the log for further information"
  131. return
  132. end
  133. ret = git_cmd(sub_commands.diff, opts)
  134. if ret == 0 then
  135. Log:info "LunarVim is already up-to-date"
  136. return
  137. end
  138. ret = git_cmd(sub_commands.merge, opts)
  139. if ret ~= 0 then
  140. Log:error "Update failed! Please pull the changes manually instead."
  141. return
  142. end
  143. end
  144. ---Get currently installed version of LunarVim
  145. ---@param type string can be "short"
  146. ---@return string
  147. function M:get_version(type)
  148. type = type or ""
  149. local opts = { cwd = get_lvim_base_dir() }
  150. local status_ok, results = git_cmd({ "describe", "--tags" }, opts)
  151. local lvim_full_ver = results[1] or ""
  152. if not status_ok or string.match(lvim_full_ver, "%d") == nil then
  153. return nil
  154. end
  155. if type == "short" then
  156. return vim.fn.split(lvim_full_ver, "-")[1]
  157. else
  158. return string.sub(lvim_full_ver, 1, #lvim_full_ver - 1)
  159. end
  160. end
  161. return M