bootstrap.lua 5.1 KB

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