bootstrap.lua 5.0 KB

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