refactor!: download packages asynchronously by diego-velez · Pull Request #487 · nvim-java/nvim-java · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions lua/java.lua
37 changes: 21 additions & 16 deletions lua/pkgm/downloaders/curl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,34 @@ function Curl:_init(opts)
end

---Download file using curl
---@return string|nil # Path to downloaded file, or nil on failure
---@return string|nil # Error message if failed
function Curl:download()
---@param on_finished fun(file_path: string|nil, err: string|nil)
function Curl:download(on_finished)
log.debug('curl downloading:', self.url, 'to', self.dest)
local cmd = string.format(
'curl --retry %d --connect-timeout %d -o %s %s',
local cmd = {
'curl',
'--retry',
self.retry_count,
'--connect-timeout',
self.timeout,
vim.fn.shellescape(self.dest),
vim.fn.shellescape(self.url)
)
'-o',
self.dest,
self.url,
}
log.debug('curl command:', cmd)

local result = vim.fn.system(cmd)
local exit_code = vim.v.shell_error
vim.system(cmd, { text = true }, function(out)
local result = out.stderr
local exit_code = out.code

if exit_code ~= 0 then
log.error('curl failed:', exit_code, result)
return nil, string.format('curl failed (exit %d): %s', exit_code, result)
end
if exit_code ~= 0 then
log.error('curl failed:', exit_code, result)
on_finished(nil, string.format('curl failed (exit %d): %s', exit_code, result))
return
end

log.debug('curl download completed:', self.dest)
return self.dest, nil
log.debug('curl download completed:', self.dest)
on_finished(self.dest, nil)
end)
end

return Curl
35 changes: 22 additions & 13 deletions lua/pkgm/downloaders/powershell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function PowerShell:_init(opts)
end

---Download file using PowerShell
---@return string # Path to downloaded file
function PowerShell:download()
---@param on_finished fun(file_path: string)
function PowerShell:download(on_finished)
local pwsh = vim.fn.executable('pwsh') == 1 and 'pwsh' or 'powershell'
log.debug('PowerShell downloading:', self.url, 'to', self.dest)
log.debug('Using PowerShell binary:', pwsh)
Expand All @@ -49,24 +49,33 @@ function PowerShell:download()
self.dest
)

local cmd = string.format(
local inner_cmd = string.format(
-- luacheck: ignore
"%s -NoProfile -NonInteractive -Command \"$ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; %s\"",
pwsh,
"$ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; %s",
pwsh_cmd
)

local cmd = {
pwsh,
'-NoProfile',
'-NonInteractive',
'-Command',
inner_cmd,
}
log.debug('PowerShell command:', cmd)

local result = vim.fn.system(cmd)
local exit_code = vim.v.shell_error
vim.system(cmd, { text = true }, function(out)
local result = out.stderr
local exit_code = out.code

if exit_code ~= 0 then
local err = string.format('PowerShell download failed (exit %d): %s', exit_code, result)
err_util.throw(err)
end
if exit_code ~= 0 then
local err = string.format('PowerShell download failed (exit %d): %s', exit_code, result)
err_util.throw(err)
end

log.debug('PowerShell download completed:', self.dest)
return self.dest
log.debug('PowerShell download completed:', self.dest)
on_finished(self.dest)
end)
end

return PowerShell
36 changes: 20 additions & 16 deletions lua/pkgm/downloaders/wget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,33 @@ function Wget:_init(opts)
end

---Download file using wget
---@return string|nil # Path to downloaded file, or nil on failure
---@return string|nil # Error message if failed
function Wget:download()
---@param on_finished fun(file_path: string|nil, err: string|nil)
function Wget:download(on_finished)
log.debug('wget downloading:', self.url, 'to', self.dest)
local cmd = string.format(
'wget -t %d -T %d -O %s %s',
local cmd = {
'wget',
'-t',
self.retry_count,
'-T',
self.timeout,
vim.fn.shellescape(self.dest),
vim.fn.shellescape(self.url)
)
'-O',
self.dest,
self.url,
}
log.debug('wget command:', cmd)

local result = vim.fn.system(cmd)
local exit_code = vim.v.shell_error
vim.system(cmd, { text = true }, function(out)
local result = out.stderr
local exit_code = out.code

if exit_code ~= 0 then
log.error('wget failed:', exit_code, result)
return nil, string.format('wget failed (exit %d): %s', exit_code, result)
end
if exit_code ~= 0 then
log.error('wget failed:', exit_code, result)
on_finished(nil, string.format('wget failed (exit %d): %s', exit_code, result))
end

log.debug('wget download completed:', self.dest)
return self.dest, nil
log.debug('wget download completed:', self.dest)
on_finished(self.dest, nil)
end)
end

return Wget
100 changes: 66 additions & 34 deletions lua/pkgm/manager.lua