aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2021-10-05 17:29:47 +0200
committerGitHub <noreply@github.com>2021-10-05 17:29:47 +0200
commit31431474a5b0d6f27de815c9a31249165cb54fbc (patch)
tree3d1be0b24d426b0374ea195b5e9c1ae71ac4c64a /lua
parentbetter error messaging (diff)
downloadmason-31431474a5b0d6f27de815c9a31249165cb54fbc.tar
mason-31431474a5b0d6f27de815c9a31249165cb54fbc.tar.gz
mason-31431474a5b0d6f27de815c9a31249165cb54fbc.tar.bz2
mason-31431474a5b0d6f27de815c9a31249165cb54fbc.tar.lz
mason-31431474a5b0d6f27de815c9a31249165cb54fbc.tar.xz
mason-31431474a5b0d6f27de815c9a31249165cb54fbc.tar.zst
mason-31431474a5b0d6f27de815c9a31249165cb54fbc.zip
attempt curl if wget is not available (#129)
Diffstat (limited to 'lua')
-rw-r--r--lua/nvim-lsp-installer/installers/context.lua41
-rw-r--r--lua/nvim-lsp-installer/installers/shell.lua4
-rw-r--r--lua/nvim-lsp-installer/installers/std.lua20
-rw-r--r--lua/nvim-lsp-installer/process.lua36
4 files changed, 80 insertions, 21 deletions
diff --git a/lua/nvim-lsp-installer/installers/context.lua b/lua/nvim-lsp-installer/installers/context.lua
index 20097915..8cb671b9 100644
--- a/lua/nvim-lsp-installer/installers/context.lua
+++ b/lua/nvim-lsp-installer/installers/context.lua
@@ -18,23 +18,42 @@ local function fetch(url, callback)
callback(("Failed to fetch url %q.\n%s"):format(url, stderr), nil)
end
end
- if platform.is_unix then
- process.spawn("wget", {
+
+ local job_variants = {
+ process.lazy_spawn("wget", {
args = { "-nv", "-O", "-", url },
stdio_sink = stdio.sink,
- }, on_exit)
- elseif platform.is_win then
- local script = {
+ }),
+ process.lazy_spawn("curl", {
+ args = { "-fsSL", url },
+ stdio_sink = stdio.sink,
+ }),
+ }
+
+ if platform.is_win then
+ local ps_script = {
"$ProgressPreference = 'SilentlyContinue'",
("Write-Output (iwr -Uri %q).Content"):format(url),
}
- process.spawn("powershell.exe", {
- args = { "-Command", table.concat(script, ";") },
- stdio_sink = stdio.sink,
- }, on_exit)
- else
- error "Unexpected error: Unsupported OS."
+ table.insert(
+ job_variants,
+ 1,
+ process.lazy_spawn("powershell.exe", {
+ args = { "-Command", table.concat(ps_script, ";") },
+ stdio_sink = stdio.sink,
+ })
+ )
end
+
+ process.attempt {
+ jobs = job_variants,
+ on_iterate = function()
+ log.debug("Flushing stdout/stderr buffers.")
+ stdio.buffers.stdout = {}
+ stdio.buffers.stderr = {}
+ end,
+ on_finish = on_exit,
+ }
end
function M.github_release_file(repo, file)
diff --git a/lua/nvim-lsp-installer/installers/shell.lua b/lua/nvim-lsp-installer/installers/shell.lua
index d06f5898..0e57cf0d 100644
--- a/lua/nvim-lsp-installer/installers/shell.lua
+++ b/lua/nvim-lsp-installer/installers/shell.lua
@@ -49,10 +49,6 @@ function M.sh(raw_script, opts)
}
end
-function M.remote_bash(url, opts)
- return M.bash(("wget -nv -O - %q | bash"):format(url), opts)
-end
-
function M.cmd(raw_script, opts)
local default_opts = {
env = {},
diff --git a/lua/nvim-lsp-installer/installers/std.lua b/lua/nvim-lsp-installer/installers/std.lua
index edf312b2..35c9ab21 100644
--- a/lua/nvim-lsp-installer/installers/std.lua
+++ b/lua/nvim-lsp-installer/installers/std.lua
@@ -9,11 +9,21 @@ local M = {}
function M.download_file(url, out_file)
return installers.when {
unix = function(server, callback, context)
- process.spawn("wget", {
- args = { "-O", out_file, url },
- cwd = server.root_dir,
- stdio_sink = context.stdio_sink,
- }, callback)
+ process.attempt {
+ jobs = {
+ process.lazy_spawn("wget", {
+ args = { "-nv", "-O", out_file, url },
+ cwd = server.root_dir,
+ stdio_sink = context.stdio_sink,
+ }),
+ process.lazy_spawn("curl", {
+ args = { "-fsSL", "-o", out_file, url },
+ cwd = server.root_dir,
+ stdio_sink = context.stdio_sink,
+ }),
+ },
+ on_finish = callback,
+ }
end,
win = shell.powershell(("iwr -Uri %q -OutFile %q"):format(url, out_file)),
}
diff --git a/lua/nvim-lsp-installer/process.lua b/lua/nvim-lsp-installer/process.lua
index dca7b328..e7ede690 100644
--- a/lua/nvim-lsp-installer/process.lua
+++ b/lua/nvim-lsp-installer/process.lua
@@ -120,7 +120,7 @@ function M.spawn(cmd, opts, callback)
if handle == nil then
log.fmt_error("Failed to spawn process. cmd=%s, err=%s", cmd, pid_or_err)
if type(pid_or_err) == "string" and pid_or_err:find "ENOENT" == 1 then
- opts.stdio_sink.stderr(("Could not find required executable %q in path.\n"):format(cmd))
+ opts.stdio_sink.stderr(("Could not find executable %q in path.\n"):format(cmd))
else
opts.stdio_sink.stderr(("Failed to spawn process cmd=%s err=%s\n"):format(cmd, pid_or_err))
end
@@ -215,4 +215,38 @@ function M.debounced(debounced_fn)
end
end
+function M.lazy_spawn(cmd, opts)
+ return function(callback)
+ return M.spawn(cmd, opts, callback)
+ end
+end
+
+function M.attempt(opts)
+ local jobs, on_finish, on_iterate = opts.jobs, opts.on_finish, opts.on_iterate
+ if #jobs == 0 then
+ error "process.attempt(...) need at least one job."
+ end
+ local function spawn(idx)
+ jobs[idx](function(success)
+ if success then
+ -- this job succeeded. exit early
+ on_finish(true)
+ elseif jobs[idx + 1] then
+ -- iterate
+ if on_iterate then
+ on_iterate()
+ end
+ log.debug "Previous job failed, attempting next."
+ spawn(idx + 1)
+ else
+ -- we exhausted all jobs without success
+ log.debug "All jobs failed."
+ on_finish(false)
+ end
+ end)
+ end
+
+ spawn(1)
+end
+
return M