Browse Source

feat: add LvimUpdate command (#1634)

* feat: add prelimenary LvimUpdate command

* feat: use native process management

* feat: add a telescope change-log utility

* fix: update readme to include the new command
kylo252 3 years ago
parent
commit
a273c46eee
7 changed files with 194 additions and 102 deletions
  1. 9 36
      README.md
  2. 95 1
      lua/bootstrap.lua
  3. 2 0
      lua/core/commands.lua
  4. 45 0
      lua/core/telescope.lua
  5. 5 0
      lua/core/which-key.lua
  6. 1 13
      lua/utils/init.lua
  7. 37 52
      utils/installer/install.sh

+ 9 - 36
README.md

@@ -16,21 +16,19 @@
     <a href="https://twitter.com/intent/follow?screen_name=chrisatmachine">
       <img src="https://img.shields.io/twitter/follow/chrisatmachine?style=social&logo=twitter" alt="follow on Twitter">
     </a>
-</p>	
+</p>
 
 </div>
 
 ## Documentation
 
-You can find all of the documentation for Lunarvim at [lunarvim.org](https://www.lunarvim.org)
+You can find all the documentation for Lunarvim at [lunarvim.org](https://www.lunarvim.org)
 
 ## Install In One Command!
 
 Make sure you have the release version of Neovim (0.5).
 
-If you have previously installed LunarVim, make sure to remove `/usr/local/bin/lvim`, as we've moved the launcher to `~/.local/bin/lvim`
-
-``` bash
+```bash
 bash <(curl -s https://raw.githubusercontent.com/lunarvim/lunarvim/master/utils/installer/install.sh)
 ```
 
@@ -42,7 +40,6 @@ bash <(curl -s https://raw.githubusercontent.com/lunarvim/lunarvim/master/utils/
 
 **NOTE** I recommend installing `lua` for autocomplete in `config.lua`
 
-
 ![Demo1](./utils/media/demo1.png)
 ![Demo2](./utils/media/demo2.png)
 ![Demo3](./utils/media/demo3.png)
@@ -111,38 +108,12 @@ lvim.plugins = {
 
 ## Updating LunarVim
 
-In order to update you should be aware of three things `Plugins`, `LunarVim` and `Neovim`
-
-To update plugins:
-
-```
-:PackerUpdate
-```
-
-To update LunarVim:
+- inside LunarVim `:LvimUpdate`
+- from the command-line `lvim +LvimUpdate +q`
 
-```bash
-cd ~/.local/share/lunarvim/lvim && git pull
-lvim +LvimCacheReset +PackerUpdate
-```
-## Known Issues
-
-If you get either of the following errors
-- init.lua:6: module 'bootstrap' not found:
-- /home/user/.config/nvim/config.lua not found, falling back to /home/user/.config/nvim/lv-config.lua
-
-Try the following methods:
-1. clear up the cache files used by the startup processing. You can either pass it as an argument
-```bash
-lvim +LvimCacheReset
-```
-or just call it manually when inside LunarVim `:LvimCacheReset`
+### Update the plugins
 
-2. make sure your `lvim` binary is up-to-date
-```bash
-export LUNARVIM_RUNTIME_DIR="${LUNARVIM_RUNTIME_DIR:-$HOME/.local/share/lunarvim}"
-bash "$LUNARVIM_RUNTIME_DIR/lvim/utils/installer/install_bin.sh"
-```
+- inside LunarVim `:PackerUpdate`
 
 ## Resources
 
@@ -157,9 +128,11 @@ bash "$LUNARVIM_RUNTIME_DIR/lvim/utils/installer/install_bin.sh"
 ## Testimonials
 
 > "I have the processing power of a potato with 4 gb of ram and LunarVim runs perfectly."
+>
 > - @juanCortelezzi, LunarVim user.
 
 > "My minimal config with a good amount less code than LunarVim loads 40ms slower. Time to switch."
+>
 > - @mvllow, Potential LunarVim user.
 
 <div align="center" id="madewithlua">

+ 95 - 1
lua/bootstrap.lua

@@ -1,5 +1,7 @@
 local M = {}
--- It's not safe to require 'utils' without adjusting the runtimepath
+
+---Join path segments that were passed as input
+---@return string
 function _G.join_paths(...)
   local uv = vim.loop
   local path_sep = uv.os_uname().version:match "Windows" and "\\" or "/"
@@ -7,6 +9,8 @@ function _G.join_paths(...)
   return result
 end
 
+---Get the full path to `$LUNARVIM_RUNTIME_DIR`
+---@return string
 function _G.get_runtime_dir()
   local lvim_runtime_dir = os.getenv "LUNARVIM_RUNTIME_DIR"
   if not lvim_runtime_dir then
@@ -16,6 +20,8 @@ function _G.get_runtime_dir()
   return lvim_runtime_dir
 end
 
+---Get the full path to `$LUNARVIM_CONFIG_DIR`
+---@return string
 function _G.get_config_dir()
   local lvim_config_dir = os.getenv "LUNARVIM_CONFIG_DIR"
   if not lvim_config_dir then
@@ -24,6 +30,8 @@ function _G.get_config_dir()
   return lvim_config_dir
 end
 
+---Get the full path to `$LUNARVIM_CACHE_DIR`
+---@return string
 function _G.get_cache_dir()
   local lvim_cache_dir = os.getenv "LUNARVIM_CACHE_DIR"
   if not lvim_cache_dir then
@@ -32,7 +40,11 @@ function _G.get_cache_dir()
   return lvim_cache_dir
 end
 
+---Get currently installed version of LunarVim
+---@param type string can be "short"
+---@return string
 function _G.get_version(type)
+  type = type or ""
   local lvim_full_ver = vim.fn.system("git -C " .. get_runtime_dir() .. "/lvim describe --tags")
 
   if string.match(lvim_full_ver, "%d") == nil then
@@ -45,10 +57,13 @@ function _G.get_version(type)
   end
 end
 
+---Initialize the `&runtimepath` variables and prepare for startup
+---@return table
 function M:init()
   self.runtime_dir = get_runtime_dir()
   self.config_dir = get_config_dir()
   self.cache_path = get_cache_dir()
+  self.repo_dir = join_paths(self.runtime_dir, "lvim")
 
   self.pack_dir = join_paths(self.runtime_dir, "site", "pack")
   self.packer_install_dir = join_paths(self.runtime_dir, "site", "pack", "packer", "start", "packer.nvim")
@@ -92,4 +107,83 @@ function M:init()
   return self
 end
 
+---Update LunarVim
+---pulls the latest changes from github and, resets the startup cache
+function M:update()
+  M:update_repo()
+  M:reset_cache()
+  vim.schedule(function()
+    -- TODO: add a changelog
+    vim.notify("Update complete", vim.log.levels.INFO)
+  end)
+end
+
+local function git_cmd(subcmd)
+  local Job = require "plenary.job"
+  local Log = require "core.log"
+  local repo_dir = join_paths(get_runtime_dir(), "lvim")
+  local args = { "-C", repo_dir }
+  vim.list_extend(args, subcmd)
+
+  local stderr = {}
+  local stdout, ret = Job
+    :new({
+      command = "git",
+      args = args,
+      cwd = repo_dir,
+      on_stderr = function(_, data)
+        table.insert(stderr, data)
+      end,
+    })
+    :sync()
+
+  if not vim.tbl_isempty(stderr) then
+    Log:debug(stderr)
+  end
+
+  if not vim.tbl_isempty(stdout) then
+    Log:debug(stdout)
+  end
+
+  return ret
+end
+
+---pulls the latest changes from github
+function M:update_repo()
+  local Log = require "core.log"
+  local sub_commands = {
+    fetch = { "fetch" },
+    diff = { "diff", "--quiet", "@{upstream}" },
+    merge = { "merge", "--ff-only", "--progress" },
+  }
+  Log:info "Checking for updates"
+
+  local ret = git_cmd(sub_commands.fetch)
+  if ret ~= 0 then
+    error "Update failed! Check the log for further information"
+  end
+
+  ret = git_cmd(sub_commands.diff)
+
+  if ret == 0 then
+    Log:info "LunarVim is already up-to-date"
+    return
+  end
+
+  ret = git_cmd(sub_commands.merge)
+
+  if ret ~= 0 then
+    error "Error: unable to guarantee data integrity while updating your branch"
+    error "Please pull the changes manually instead."
+  end
+end
+
+---Reset any startup cache files used by Packer and Impatient
+---Tip: Useful for clearing any outdated settings
+function M:reset_cache()
+  _G.__luacache.clear_cache()
+  _G.__luacache.save_cache()
+  require("plugin-loader"):cache_reset()
+end
+
 return M

+ 2 - 0
lua/core/commands.lua

@@ -12,6 +12,8 @@ M.defaults = {
   ]],
   -- :LvimInfo
   [[command! LvimInfo lua require('core.info').toggle_popup(vim.bo.filetype)]],
+  [[ command! LvimCacheReset lua require('bootstrap').reset_cache() ]],
+  [[ command! LvimUpdate lua require('bootstrap').update() ]],
 }
 
 M.load = function(commands)

+ 45 - 0
lua/core/telescope.lua

@@ -114,6 +114,51 @@ function M.grep_lunarvim_files(opts)
   require("telescope.builtin").live_grep(opts)
 end
 
+function M.view_lunarvim_changelog()
+  local finders = require "telescope.finders"
+  local make_entry = require "telescope.make_entry"
+  local pickers = require "telescope.pickers"
+  local previewers = require "telescope.previewers"
+  local actions = require "telescope.actions"
+  local opts = {}
+
+  local conf = require("telescope.config").values
+  opts.entry_maker = make_entry.gen_from_git_commits(opts)
+
+  pickers.new(opts, {
+    prompt_title = "LunarVim changelog",
+
+    finder = finders.new_oneshot_job(
+      vim.tbl_flatten {
+        "git",
+        "log",
+        "--pretty=oneline",
+        "--abbrev-commit",
+        "--",
+        ".",
+      },
+      opts
+    ),
+    previewer = {
+      previewers.git_commit_diff_to_parent.new(opts),
+      previewers.git_commit_diff_to_head.new(opts),
+      previewers.git_commit_diff_as_was.new(opts),
+      previewers.git_commit_message.new(opts),
+    },
+
+    --TODO: consider opening a diff view when pressing enter
+    attach_mappings = function(_, map)
+      map("i", "<enter>", actions._close)
+      map("n", "<enter>", actions._close)
+      map("i", "<esc>", actions._close)
+      map("n", "<esc>", actions._close)
+      map("n", "q", actions._close)
+      return true
+    end,
+    sorter = conf.file_sorter(opts),
+  }):find()
+end
+
 function M.setup()
   local telescope = require "telescope"
 

+ 5 - 0
lua/core/which-key.lua

@@ -192,6 +192,10 @@ M.config = function()
           "<cmd>lua require('core.info').toggle_popup(vim.bo.filetype)<cr>",
           "Toggle LunarVim Info",
         },
+        I = {
+          "<cmd>lua require('core.telescope').view_lunarvim_changelog()<cr>",
+          "View LunarVim's changelog",
+        },
         l = {
           name = "+logs",
           d = {
@@ -210,6 +214,7 @@ M.config = function()
           P = { "<cmd>exe 'edit '.stdpath('cache').'/packer.nvim.log'<cr>", "Open the Packer logfile" },
         },
         r = { "<cmd>lua require('utils').reload_lv_config()<cr>", "Reload configurations" },
+        u = { "<cmd>LvimUpdate<cr>", "Update LunarVim" },
       },
       s = {
         name = "Search",

+ 1 - 13
lua/utils/init.lua

@@ -140,19 +140,7 @@ function utils.is_file(filename)
   return stat and stat.type == "file" or false
 end
 
-function utils.join_paths(...)
-  local path_sep = vim.loop.os_uname().version:match "Windows" and "\\" or "/"
-  local result = table.concat(vim.tbl_flatten { ... }, path_sep):gsub(path_sep .. "+", path_sep)
-  return result
-end
-
-function utils.lvim_cache_reset()
-  _G.__luacache.clear_cache()
-  _G.__luacache.save_cache()
-  require("plugin-loader"):cache_reset()
-end
-
-vim.cmd [[ command! LvimCacheReset lua require('utils').lvim_cache_reset() ]]
+utils.join_paths = _G.join_paths
 
 return utils
 

+ 37 - 52
utils/installer/install.sh

@@ -1,5 +1,4 @@
 #!/usr/bin/env bash
-
 set -eo pipefail
 
 #Set branch to master unless specified by the user
@@ -75,52 +74,40 @@ function parse_arguments() {
   done
 }
 
+function msg() {
+  local text="$1"
+  local div_width="80"
+  printf "%${div_width}s\n" ' ' | tr ' ' -
+  printf "%s\n" "$text"
+}
+
 function main() {
   parse_arguments "$@"
 
-  cat <<'EOF'
+  print_logo
 
-      88\                                                   88\
-      88 |                                                  \__|
-      88 |88\   88\ 888888$\   888888\   888888\ 88\    88\ 88\ 888888\8888\
-      88 |88 |  88 |88  __88\  \____88\ 88  __88\\88\  88  |88 |88  _88  _88\
-      88 |88 |  88 |88 |  88 | 888888$ |88 |  \__|\88\88  / 88 |88 / 88 / 88 |
-      88 |88 |  88 |88 |  88 |88  __88 |88 |       \88$  /  88 |88 | 88 | 88 |
-      88 |\888888  |88 |  88 |\888888$ |88 |        \$  /   88 |88 | 88 | 88 |
-      \__| \______/ \__|  \__| \_______|\__|         \_/    \__|\__| \__| \__|
-
-EOF
-
-  __add_separator "80"
-
-  echo "Detecting platform for managing any additional neovim dependencies"
+  msg "Detecting platform for managing any additional neovim dependencies"
   detect_platform
 
   check_system_deps
 
-  __add_separator "80"
-
   if [ "$ARGS_INSTALL_DEPENDENCIES" -eq 1 ]; then
-    echo "Would you like to install lunarvim's NodeJS dependencies?"
+    msg "Would you like to install lunarvim's NodeJS dependencies?"
     read -p "[y]es or [n]o (default: no) : " -r answer
     [ "$answer" != "${answer#[Yy]}" ] && install_nodejs_deps
 
-    echo "Would you like to install lunarvim's Python dependencies?"
+    msg "Would you like to install lunarvim's Python dependencies?"
     read -p "[y]es or [n]o (default: no) : " -r answer
     [ "$answer" != "${answer#[Yy]}" ] && install_python_deps
 
-    echo "Would you like to install lunarvim's Rust dependencies?"
+    msg "Would you like to install lunarvim's Rust dependencies?"
     read -p "[y]es or [n]o (default: no) : " -r answer
     [ "$answer" != "${answer#[Yy]}" ] && install_rust_deps
   fi
 
-  __add_separator "80"
-
-  echo "Backing up old LunarVim configuration"
+  msg "Backing up old LunarVim configuration"
   backup_old_config
 
-  __add_separator "80"
-
   if [ "$ARGS_OVERWRITE" -eq 1 ]; then
     for dir in "${__lvim_dirs[@]}"; do
       [ -d "$dir" ] && rm -rf "$dir"
@@ -129,11 +116,8 @@ EOF
 
   install_packer
 
-  __add_separator "80"
-
   if [ -e "$LUNARVIM_RUNTIME_DIR/lvim/init.lua" ]; then
-    echo "Updating LunarVim"
-    update_lvim
+    bash "$LUNARVIM_RUNTIME_DIR/lvim/utils/installer/update_lvim.sh"
   else
     if [ "$ARGS_LOCAL" -eq 1 ]; then
       link_local_lvim
@@ -143,7 +127,9 @@ EOF
     setup_lvim
   fi
 
-  __add_separator "80"
+  msg "Thank you for installing LunarVim!!"
+  echo "You can start it by running: $INSTALL_PREFIX/bin/lvim"
+  echo "Do not forget to use a font with glyphs (icons) support [https://github.com/ryanoasis/nerd-fonts]"
 }
 
 function detect_platform() {
@@ -287,18 +273,18 @@ function backup_old_config() {
 
 function install_packer() {
   if [ -e "$LUNARVIM_PACK_DIR/packer/start/packer.nvim" ]; then
-    echo "Packer already installed"
+    msg "Packer already installed"
   else
     if ! git clone --depth 1 "https://github.com/wbthomason/packer.nvim" \
       "$LUNARVIM_PACK_DIR/packer/start/packer.nvim"; then
-      echo "Failed to clone Packer. Installation failed."
+      msg "Failed to clone Packer. Installation failed."
       exit 1
     fi
   fi
 }
 
 function clone_lvim() {
-  echo "Cloning LunarVim configuration"
+  msg "Cloning LunarVim configuration"
   if ! git clone --branch "$LV_BRANCH" \
     --depth 1 "https://github.com/${LV_REMOTE}" "$LUNARVIM_RUNTIME_DIR/lvim"; then
     echo "Failed to clone repository. Installation failed."
@@ -338,12 +324,12 @@ EOF
 function remove_old_cache_files() {
   local packer_cache="$LUNARVIM_CONFIG_DIR/plugin/packer_compiled.lua"
   if [ -e "$packer_cache" ]; then
-    echo "Removing old packer cache file"
+    msg "Removing old packer cache file"
     rm -f "$packer_cache"
   fi
 
   if [ -e "$LUNARVIM_CACHE_DIR/luacache" ] || [ -e "$LUNARVIM_CACHE_DIR/lvim_cache" ]; then
-    echo "Removing old startup cache file"
+    msg "Removing old startup cache file"
     rm -f "$LUNARVIM_CACHE_DIR/{luacache,lvim_cache}"
   fi
 }
@@ -352,7 +338,7 @@ function setup_lvim() {
 
   remove_old_cache_files
 
-  echo "Installing LunarVim shim"
+  msg "Installing LunarVim shim"
 
   setup_shim
 
@@ -368,26 +354,25 @@ function setup_lvim() {
   echo "Packer setup complete"
 
   cp "$LUNARVIM_RUNTIME_DIR/lvim/utils/installer/config.example.lua" "$LUNARVIM_CONFIG_DIR/config.lua"
-
-  echo "Thank you for installing LunarVim!!"
-  echo "You can start it by running: $INSTALL_PREFIX/bin/lvim"
-  echo "Do not forget to use a font with glyphs (icons) support [https://github.com/ryanoasis/nerd-fonts]"
 }
 
 function update_lvim() {
-  git -C "$LUNARVIM_RUNTIME_DIR/lvim" fetch --quiet
-  if ! git -C "$LUNARVIM_RUNTIME_DIR/lvim" diff --quiet "@{upstream}"; then
-    git -C "$LUNARVIM_RUNTIME_DIR/lvim" merge --ff-only --progress ||
-      echo "Unable to guarantee data integrity while updating. Please do that manually instead." && exit 1
-  fi
-  echo "Clearing up old startup cache"
-  "$INSTALL_PREFIX/bin/lvim" --headless +LvimCacheReset +q
-  echo "Your LunarVim installation is now up to date!"
+  "$INSTALL_PREFIX/bin/lvim" --headless +'LvimUpdate' +q
 }
 
-function __add_separator() {
-  local DIV_WIDTH="$1"
-  printf "%${DIV_WIDTH}s\n" ' ' | tr ' ' -
+function print_logo() {
+  cat <<'EOF'
+
+      88\                                                   88\
+      88 |                                                  \__|
+      88 |88\   88\ 888888$\   888888\   888888\ 88\    88\ 88\ 888888\8888\
+      88 |88 |  88 |88  __88\  \____88\ 88  __88\\88\  88  |88 |88  _88  _88\
+      88 |88 |  88 |88 |  88 | 888888$ |88 |  \__|\88\88  / 88 |88 / 88 / 88 |
+      88 |88 |  88 |88 |  88 |88  __88 |88 |       \88$  /  88 |88 | 88 | 88 |
+      88 |\888888  |88 |  88 |\888888$ |88 |        \$  /   88 |88 | 88 | 88 |
+      \__| \______/ \__|  \__| \_______|\__|         \_/    \__|\__| \__| \__|
+
+EOF
 }
 
 main "$@"