From 6c50df36e9dd21317fa4d1883c56f22f8d32f865 Mon Sep 17 00:00:00 2001 From: Matt Leong Date: Sun, 7 Nov 2021 16:29:01 -0800 Subject: [PATCH] feat(core): Add CosmicUpdate, CosmicReload, CosmicReloadConfig commands --- lua/cosmic/core/commands.lua | 5 ++++ lua/cosmic/init.lua | 1 + lua/cosmic/utils.lua | 55 ++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 lua/cosmic/core/commands.lua diff --git a/lua/cosmic/core/commands.lua b/lua/cosmic/core/commands.lua new file mode 100644 index 0000000..fe51b75 --- /dev/null +++ b/lua/cosmic/core/commands.lua @@ -0,0 +1,5 @@ +vim.cmd([[ +command! CosmicUpdate lua require('cosmic.utils').update() +command! CosmicReloadConfig lua require('cosmic.utils').reload_user_config() +command! CosmicReload lua require('cosmic.utils').reload_cosmic() +]]) diff --git a/lua/cosmic/init.lua b/lua/cosmic/init.lua index d8a4806..83c5ed1 100644 --- a/lua/cosmic/init.lua +++ b/lua/cosmic/init.lua @@ -2,6 +2,7 @@ local cosmic_modules = { 'cosmic.disabled', 'cosmic.pluginsInit', 'cosmic.compiled', + 'cosmic.core.commands', 'cosmic.editor', 'cosmic.mappings', 'cosmic.core.theme.highlights', diff --git a/lua/cosmic/utils.lua b/lua/cosmic/utils.lua index 43ce6dc..032881c 100644 --- a/lua/cosmic/utils.lua +++ b/lua/cosmic/utils.lua @@ -47,4 +47,59 @@ function M.get_active_lsp_client_names() return client_names end +local function reload(module_pattern) + for module, _ in pairs(package.loaded) do + if module:match(module_pattern) then + package.loaded[module] = nil + require(module) + end + end +end + +function M.reload_user_config() + reload('cosmic.config') +end + +function M.reload_cosmic() + reload('cosmic') +end + +function M.get_install_dir() + local config_dir = os.getenv('COSMICNVIM_INSTALL_DIR') + if not config_dir then + return vim.fn.stdpath('config') + end + return config_dir +end + +-- update instance of CosmicNvim +function M.update() + local Job = require('plenary.job') + local path = M.get_install_dir() + local err = false + + Job + :new({ + command = 'git', + args = { 'pull' }, + cwd = path, + on_exit = function() + if not err then + vim.notify( + 'Please restart CosmicNvim and run `:PackerSync`', + vim.log.levels.INFO, + { title = 'CosmicNvim Updated!', timeout = 30000 } + ) + end + end, + on_stderr = function(_, return_val) + err = return_val + vim.notify(err, vim.log.levels.ERROR, { + title = 'CosmicNvim Update Failed!', + }) + end, + }) + :sync() -- or start() +end + return M