NvChadUpdater: Make update repo and url configurable | Improve logging

This commit is contained in:
Akianonymus 2021-08-19 11:20:00 +05:30 committed by siduck76
parent 4801ec86f1
commit 880d4dc312
3 changed files with 41 additions and 19 deletions

View file

@ -38,6 +38,9 @@ M.options = {
mapleader = " ", mapleader = " ",
autosave = false, autosave = false,
enable_insertNav = true, -- navigation in insertmode enable_insertNav = true, -- navigation in insertmode
-- used for updater
update_url = "https://github.com/NvChad/NvChad",
update_branch = "main",
} }
-- enable and disable plugins (false for disable) -- enable and disable plugins (false for disable)

View file

@ -37,6 +37,9 @@ M.options = {
mapleader = " ", mapleader = " ",
autosave = false, autosave = false,
enable_insertNav = true, -- navigation in insertmode enable_insertNav = true, -- navigation in insertmode
-- used for updater
update_url = "https://github.com/NvChad/NvChad",
update_branch = "main",
} }
-- enable and disable plugins (false for disable) -- enable and disable plugins (false for disable)

View file

@ -149,13 +149,13 @@ M.close_buffer = function(bufexpr, force)
end end
-- wrapper to use vim.api.nvim_echo -- wrapper to use vim.api.nvim_echo
-- 1st arg - text - required -- table of {string, highlight}
-- 2nd arg - highlight group - if not present then use None -- e.g echo({{"Hello", "Title"}, {"World"}})
M.echo = function(text, group) M.echo = function(opts)
if text == nil then if opts == nil or type(opts) ~= "table" then
return return
end end
vim.api.nvim_echo({ { text, group or "None" } }, false, {}) vim.api.nvim_echo(opts, false, {})
end end
-- 1st arg - r or w -- 1st arg - r or w
@ -419,18 +419,29 @@ M.update_nvchad = function()
local config_file_backup = config_path .. "/" .. config_name .. ".lua.bak." .. math.random() local config_file_backup = config_path .. "/" .. config_name .. ".lua.bak." .. math.random()
local utils = require "utils" local utils = require "utils"
local echo = utils.echo local echo = utils.echo
local current_config = utils.load_config()
local update_url = current_config.options.update_url or "https://github.com/NvChad/NvChad"
local update_branch = current_config.options.update_branch or "main"
-- ask the user for confirmation to update because we are going to run git reset --hard -- ask the user for confirmation to update because we are going to run git reset --hard
echo( echo { { "Url: ", "Title" }, { update_url } }
"Updater will run git reset --hard in config folder, so changes to existing repo files except " echo { { "Branch: ", "Title" }, { update_branch } }
.. config_name echo {
.. " will be lost!\nUpdate NvChad ? [y/N]", { "\nUpdater will run", "WarningMsg" },
"WarningMsg" { " git reset --hard " },
) {
"in config folder, so changes to existing repo files except ",
"WarningMsg",
},
{ config_name },
{ " will be lost!\n\nUpdate NvChad ? [y/N]", "WarningMsg" },
}
local ans = string.lower(vim.fn.input "-> ") == "y" local ans = string.lower(vim.fn.input "-> ") == "y"
utils.clear_cmdline() utils.clear_cmdline()
if not ans then if not ans then
echo("Update cancelled!", "Title") echo { { "Update cancelled!", "Title" } }
return return
end end
@ -449,26 +460,31 @@ M.update_nvchad = function()
end) then end) then
-- config restored succesfully, remove backup file that was created -- config restored succesfully, remove backup file that was created
if not pcall(os.remove, config_file_backup) then if not pcall(os.remove, config_file_backup) then
echo("Warning: Failed to remove backup chadrc, remove manually.", "WarningMsg") echo { { "Warning: Failed to remove backup chadrc, remove manually.", "WarningMsg" } }
echo("Path: " .. config_file_backup, "WarningMsg") echo { { "Path: ", "WarningMsg" }, { config_file_backup } }
end end
else else
echo("Error: Restoring " .. config_name .. " failed.\n", "ErrorMsg") echo { { "Error: Restoring " .. config_name .. " failed.\n", "ErrorMsg" } }
echo("Backed up " .. config_name .. " path: " .. config_file_backup .. "\n\n", "None") echo { { "Backed up " .. config_name .. " path: " .. config_file_backup .. "\n\n", "None" } }
end end
-- close the terminal buffer only if update was success, as in case of error, we need the error message -- close the terminal buffer only if update was success, as in case of error, we need the error message
if code == 0 then if code == 0 then
vim.cmd "bd!" vim.cmd "bd!"
echo("NvChad succesfully updated.\n", "String") echo { { "NvChad succesfully updated.\n", "String" } }
else else
echo("Error: NvChad Update failed.\n", "ErrorMsg") echo { { "Error: NvChad Update failed.\n", "ErrorMsg" } }
end end
end end
-- git commands that will executed, reset in case config was modfied -- git commands that will executed, reset in case config was modfied
-- use --ff-only to not mess up if the local repo is outdated -- use --ff-only to not mess up if the local repo is outdated
local update_script = [[git reset --hard && git pull --set-upstream https://github.com/NvChad/NvChad main --ff-only]] local update_script = table.concat({
"git reset --hard && git pull --set-upstream",
update_url,
update_branch,
"--ff-only",
}, " ")
-- open a new buffer -- open a new buffer
vim.cmd "new" vim.cmd "new"