diff options
| author | William Boman <william@redwill.se> | 2022-04-11 17:19:01 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-11 17:19:01 +0200 |
| commit | 88f590ce0e01767bcc8dfdc862a456efde77d4a0 (patch) | |
| tree | 2f5faaffa76b9147a873b2adc3286b6624144976 /lua/nvim-lsp-installer/core/fetch.lua | |
| parent | fix(verible): use correct unpacked directory name on Windows (#589) (diff) | |
| download | mason-88f590ce0e01767bcc8dfdc862a456efde77d4a0.tar mason-88f590ce0e01767bcc8dfdc862a456efde77d4a0.tar.gz mason-88f590ce0e01767bcc8dfdc862a456efde77d4a0.tar.bz2 mason-88f590ce0e01767bcc8dfdc862a456efde77d4a0.tar.lz mason-88f590ce0e01767bcc8dfdc862a456efde77d4a0.tar.xz mason-88f590ce0e01767bcc8dfdc862a456efde77d4a0.tar.zst mason-88f590ce0e01767bcc8dfdc862a456efde77d4a0.zip | |
more async refactor (#587)
Diffstat (limited to 'lua/nvim-lsp-installer/core/fetch.lua')
| -rw-r--r-- | lua/nvim-lsp-installer/core/fetch.lua | 98 |
1 files changed, 20 insertions, 78 deletions
diff --git a/lua/nvim-lsp-installer/core/fetch.lua b/lua/nvim-lsp-installer/core/fetch.lua index bd1a582d..d97da59b 100644 --- a/lua/nvim-lsp-installer/core/fetch.lua +++ b/lua/nvim-lsp-installer/core/fetch.lua @@ -1,6 +1,8 @@ local log = require "nvim-lsp-installer.log" -local process = require "nvim-lsp-installer.process" local platform = require "nvim-lsp-installer.platform" +local Result = require "nvim-lsp-installer.core.result" +local spawn = require "nvim-lsp-installer.core.spawn" +local powershell = require "nvim-lsp-installer.core.managers.powershell" local USER_AGENT = "nvim-lsp-installer (+https://github.com/williamboman/nvim-lsp-installer)" @@ -10,89 +12,29 @@ local HEADERS = { iwr = ("-Headers @{'User-Agent' = '%s'}"):format(USER_AGENT), } -local function with_headers(headers, args) - local result = {} - vim.list_extend(result, headers) - vim.list_extend(result, args) - return result -end - ----@alias FetchCallback fun(err: string|nil, raw_data: string) - ----@param url string The url to fetch. ----@param callback_or_opts FetchCallback|{custom_fetcher: { cmd: string, args: string[] }} ----@param callback FetchCallback -local function fetch(url, callback_or_opts, callback) - local opts = type(callback_or_opts) == "table" and callback_or_opts or {} - callback = type(callback_or_opts) == "function" and callback_or_opts or callback - local stdio = process.in_memory_sink() +---@async +---@param url string @The url to fetch. +local function fetch(url) log.fmt_debug("Fetching URL %s", url) - local on_exit = function(success) - if success then - log.fmt_debug("Successfully fetched URL %s", url) - callback(nil, table.concat(stdio.buffers.stdout, "")) - else - local stderr = table.concat(stdio.buffers.stderr, "") - log.fmt_warn("Failed to fetch URL %s. stderr=%s", url, stderr) - callback(("Failed to fetch url %q.\n%s"):format(url, stderr), nil) - end - end - local job_variants = { - process.lazy_spawn("wget", { - args = with_headers(HEADERS.wget, { "-nv", "-O", "-", url }), - stdio_sink = stdio.sink, - }), - process.lazy_spawn("curl", { - args = with_headers(HEADERS.curl, { "-fsSL", url }), - stdio_sink = stdio.sink, - }), - } + local platform_specific = Result.failure() if platform.is_win then - local ps_script = { - "$ProgressPreference = 'SilentlyContinue';", - "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;", - ("Write-Output (iwr %s -UseBasicParsing -Uri %q).Content"):format(HEADERS.iwr, url), - } - table.insert( - job_variants, - 1, - process.lazy_spawn("powershell.exe", { - args = { "-NoProfile", "-Command", table.concat(ps_script, ";") }, - stdio_sink = stdio.sink, - env = process.graft_env({}, { "PSMODULEPATH" }), - }) - ) - end - - if opts.custom_fetcher then - table.insert( - job_variants, - 1, - process.lazy_spawn(opts.custom_fetcher.cmd, { - args = opts.custom_fetcher.args, - stdio_sink = stdio.sink, - }) + platform_specific = powershell.command( + ([[Write-Output (iwr %s -UseBasicParsing -Uri %q).Content;]]):format(HEADERS.iwr, url) ) 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, - } + return platform_specific + :recover_catching(function() + return spawn.wget({ HEADERS.wget, "-nv", "-O", "-", url }):get_or_throw() + end) + :recover_catching(function() + return spawn.curl({ HEADERS.curl, "-fsSL", url }):get_or_throw() + end) + :map(function(result) + return result.stdout + end) end -return setmetatable({ - with_headers = with_headers, - HEADERS = HEADERS, -}, { - __call = function(_, ...) - return fetch(...) - end, -}) +return fetch |
