bootstrap.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. local M = {}
  2. local in_headless = #vim.api.nvim_list_uis() == 0
  3. ---Join path segments that were passed as input
  4. ---@return string
  5. function _G.join_paths(...)
  6. local uv = vim.loop
  7. local path_sep = uv.os_uname().version:match "Windows" and "\\" or "/"
  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 "config"
  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. ---Get currently installed version of LunarVim
  40. ---@param type string can be "short"
  41. ---@return string
  42. function _G.get_version(type)
  43. type = type or ""
  44. local lvim_full_ver = vim.fn.system("git -C " .. get_runtime_dir() .. "/lvim describe --tags")
  45. if string.match(lvim_full_ver, "%d") == nil then
  46. return nil
  47. end
  48. if type == "short" then
  49. return vim.fn.split(lvim_full_ver, "-")[1]
  50. else
  51. return string.sub(lvim_full_ver, 1, #lvim_full_ver - 1)
  52. end
  53. end
  54. ---Initialize the `&runtimepath` variables and prepare for startup
  55. ---@return table
  56. function M:init()
  57. self.runtime_dir = get_runtime_dir()
  58. self.config_dir = get_config_dir()
  59. self.cache_path = get_cache_dir()
  60. self.repo_dir = join_paths(self.runtime_dir, "lvim")
  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. if os.getenv "LUNARVIM_RUNTIME_DIR" then
  65. vim.opt.rtp:remove(join_paths(vim.fn.stdpath "data", "site"))
  66. vim.opt.rtp:remove(join_paths(vim.fn.stdpath "data", "site", "after"))
  67. vim.opt.rtp:prepend(join_paths(self.runtime_dir, "site"))
  68. vim.opt.rtp:append(join_paths(self.runtime_dir, "site", "after"))
  69. vim.opt.rtp:remove(vim.fn.stdpath "config")
  70. vim.opt.rtp:remove(join_paths(vim.fn.stdpath "config", "after"))
  71. vim.opt.rtp:prepend(self.config_dir)
  72. vim.opt.rtp:append(join_paths(self.config_dir, "after"))
  73. -- TODO: we need something like this: vim.opt.packpath = vim.opt.rtp
  74. vim.cmd [[let &packpath = &runtimepath]]
  75. vim.cmd("set spellfile=" .. join_paths(self.config_dir, "spell", "en.utf-8.add"))
  76. end
  77. vim.fn.mkdir(vim.fn.stdpath "cache", "p")
  78. -- FIXME: currently unreliable in unit-tests
  79. if not os.getenv "LVIM_TEST_ENV" then
  80. require("impatient").setup {
  81. path = vim.fn.stdpath "cache" .. "/lvim_cache",
  82. enable_profiling = true,
  83. }
  84. end
  85. local config = require "config"
  86. config:init {
  87. path = join_paths(self.config_dir, "config.lua"),
  88. }
  89. require("plugin-loader"):init {
  90. package_root = self.pack_dir,
  91. install_path = self.packer_install_dir,
  92. }
  93. return self
  94. end
  95. ---Update LunarVim
  96. ---pulls the latest changes from github and, resets the startup cache
  97. function M:update()
  98. M:update_repo()
  99. M:reset_cache()
  100. require("lsp.templates").generate_templates()
  101. if not in_headless then
  102. vim.schedule(function()
  103. require("packer").install()
  104. -- TODO: add a changelog
  105. vim.notify("Update complete", vim.log.levels.INFO)
  106. end)
  107. end
  108. end
  109. local function git_cmd(subcmd)
  110. local Job = require "plenary.job"
  111. local Log = require "core.log"
  112. local repo_dir = join_paths(get_runtime_dir(), "lvim")
  113. local args = { "-C", repo_dir }
  114. vim.list_extend(args, subcmd)
  115. local stderr = {}
  116. local stdout, ret = Job
  117. :new({
  118. command = "git",
  119. args = args,
  120. cwd = repo_dir,
  121. on_stderr = function(_, data)
  122. table.insert(stderr, data)
  123. end,
  124. })
  125. :sync()
  126. if not vim.tbl_isempty(stderr) then
  127. Log:debug(stderr)
  128. end
  129. if not vim.tbl_isempty(stdout) then
  130. Log:debug(stdout)
  131. end
  132. return ret
  133. end
  134. ---pulls the latest changes from github
  135. function M:update_repo()
  136. local Log = require "core.log"
  137. local sub_commands = {
  138. fetch = { "fetch" },
  139. diff = { "diff", "--quiet", "@{upstream}" },
  140. merge = { "merge", "--ff-only", "--progress" },
  141. }
  142. Log:info "Checking for updates"
  143. local ret = git_cmd(sub_commands.fetch)
  144. if ret ~= 0 then
  145. error "Update failed! Check the log for further information"
  146. end
  147. ret = git_cmd(sub_commands.diff)
  148. if ret == 0 then
  149. Log:info "LunarVim is already up-to-date"
  150. return
  151. end
  152. ret = git_cmd(sub_commands.merge)
  153. if ret ~= 0 then
  154. error "Error: unable to guarantee data integrity while updating your branch"
  155. error "Please pull the changes manually instead."
  156. end
  157. end
  158. ---Reset any startup cache files used by Packer and Impatient
  159. ---Tip: Useful for clearing any outdated settings
  160. function M:reset_cache()
  161. _G.__luacache.clear_cache()
  162. _G.__luacache.save_cache()
  163. require("plugin-loader"):cache_reset()
  164. end
  165. return M