bootstrap.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. end
  66. -- FIXME: currently unreliable in unit-tests
  67. if not in_headless then
  68. _G.PLENARY_DEBUG = false
  69. require("lvim.impatient").setup {
  70. path = join_paths(self.cache_dir, "lvim_cache"),
  71. enable_profiling = true,
  72. }
  73. end
  74. require("lvim.config"):init()
  75. require("lvim.plugin-loader").init {
  76. package_root = self.pack_dir,
  77. install_path = self.packer_install_dir,
  78. }
  79. return self
  80. end
  81. ---Update LunarVim
  82. ---pulls the latest changes from github and, resets the startup cache
  83. function M:update()
  84. package.loaded["lvim.utils.hooks"] = nil
  85. local _, hooks = pcall(require, "lvim.utils.hooks")
  86. hooks.run_pre_update()
  87. M:update_repo()
  88. hooks.run_post_update()
  89. end
  90. local function git_cmd(subcmd, opts)
  91. local Job = require "plenary.job"
  92. local Log = require "lvim.core.log"
  93. local args = { "-C", opts.cwd }
  94. vim.list_extend(args, subcmd)
  95. local stderr = {}
  96. local stdout, ret = Job
  97. :new({
  98. command = "git",
  99. args = args,
  100. cwd = opts.cwd,
  101. on_stderr = function(_, data)
  102. table.insert(stderr, data)
  103. end,
  104. })
  105. :sync()
  106. if not vim.tbl_isempty(stderr) then
  107. Log:debug(stderr)
  108. end
  109. if not vim.tbl_isempty(stdout) then
  110. Log:debug(stdout)
  111. end
  112. return ret, stdout
  113. end
  114. ---pulls the latest changes from github
  115. function M:update_repo()
  116. local Log = require "lvim.core.log"
  117. local sub_commands = {
  118. fetch = { "fetch" },
  119. diff = { "diff", "--quiet", "@{upstream}" },
  120. merge = { "merge", "--ff-only", "--progress" },
  121. }
  122. local opts = {
  123. cwd = get_lvim_base_dir(),
  124. }
  125. Log:info "Checking for updates"
  126. local ret = git_cmd(sub_commands.fetch, opts)
  127. if ret ~= 0 then
  128. Log:error "Update failed! Check the log for further information"
  129. return
  130. end
  131. ret = git_cmd(sub_commands.diff, opts)
  132. if ret == 0 then
  133. Log:info "LunarVim is already up-to-date"
  134. return
  135. end
  136. ret = git_cmd(sub_commands.merge, opts)
  137. if ret ~= 0 then
  138. Log:error "Update failed! Please pull the changes manually instead."
  139. return
  140. end
  141. end
  142. ---Get currently installed version of LunarVim
  143. ---@param type string can be "short"
  144. ---@return string
  145. function M:get_version(type)
  146. type = type or ""
  147. local opts = { cwd = get_lvim_base_dir() }
  148. local _, branch = git_cmd({ "branch", "--show-current" }, opts)
  149. local is_on_master = branch == "master"
  150. if not is_on_master then
  151. local log_status_ok, log_results = git_cmd({ "log", "--pretty=format:%h", "-1" }, opts)
  152. local abbrev_version = log_results[1] or ""
  153. if not log_status_ok or string.match(abbrev_version, "%d") == nil then
  154. return nil
  155. end
  156. return "dev-" .. abbrev_version
  157. end
  158. local tag_status_ok, results = git_cmd({ "describe", "--tags" }, opts)
  159. local lvim_full_ver = results[1] or ""
  160. if not tag_status_ok or string.match(lvim_full_ver, "%d") == nil then
  161. return nil
  162. end
  163. if type == "short" then
  164. return vim.fn.split(lvim_full_ver, "-")[1]
  165. else
  166. return string.sub(lvim_full_ver, 1, #lvim_full_ver - 1)
  167. end
  168. end
  169. return M