From c96dafbaf665a5496a8cbb96cda6c93abbf3682f Mon Sep 17 00:00:00 2001 From: Oleksandr Smirnov Date: Thu, 27 Feb 2025 23:10:00 +0200 Subject: [PATCH] refactor(installer): add a way to install deps synchronously --- Taskfile.yml | 2 +- lua/gopher/installer.lua | 45 +++++++++++++++++++++++++++------------- plugin/gopher.vim | 1 + 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index b2a7825..2704b42 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -36,7 +36,7 @@ tasks: ci:install-deps: desc: install dependencies for CI - cmds: ['nvim --headless -u "./scripts/minimal_init.lua" -c "GoInstallDeps" -c "qa!"'] + cmds: ['nvim --headless -u "./scripts/minimal_init.lua" -c "GoInstallDepsSync" -c "qa!"'] docgen: desc: generate vimhelp diff --git a/lua/gopher/installer.lua b/lua/gopher/installer.lua index de9cf4b..cb3dd88 100644 --- a/lua/gopher/installer.lua +++ b/lua/gopher/installer.lua @@ -11,26 +11,43 @@ local urls = { iferr = "github.com/koron/iferr", } ----@param pkg string -local function install(pkg) - local url = urls[pkg] .. "@latest" - local function on_exit(opt) - if opt.code ~= 0 then - u.deferred_notify("go install failed: " .. url) - log.debug("go install failed:", "url", url, "stderr", opt.stderr) - return - end - - u.deferred_notify("go install'ed: " .. url) +---@param opt vim.SystemCompleted +---@param url string +local function handle_intall_exit(opt, url) + if opt.code ~= 0 then + u.deferred_notify("go install failed: " .. url) + log.debug("go install failed:", "url", url, "stderr", opt.stderr) + return end - r.async({ c.go, "install", url }, on_exit) + u.deferred_notify("go install-ed: " .. url) +end + +---@param url string +local function install(url) + r.async({ c.go, "install", url }, function(opt) + handle_intall_exit(opt, url) + end) +end + +---@param url string +local function install_sync(url) + local rs = r.sync { c.go, "install", url } + handle_intall_exit(rs, url) end ---Install required go deps -function installer.install_deps() +---@param sync? boolean +function installer.install_deps(sync) + sync = sync or false + for pkg, _ in pairs(urls) do - install(pkg) + local url = urls[pkg] .. "@latest" + if sync then + install_sync(url) + else + install(url) + end end end diff --git a/plugin/gopher.vim b/plugin/gopher.vim index a219a1c..d458849 100644 --- a/plugin/gopher.vim +++ b/plugin/gopher.vim @@ -11,4 +11,5 @@ command! -nargs=* GoGenerate :lua require"gopher".generate() command! GoCmt :lua require"gopher".comment() command! GoIfErr :lua require"gopher".iferr() command! GoInstallDeps :lua require"gopher".install_deps() +command! GoInstallDepsSync :lua require"gopher".install_deps(true) command! GopherLog :lua vim.cmd("tabnew " .. require("gopher._utils.log").get_outfile())