diff options
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-lsp-installer/installers/context.lua | 77 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/process.lua | 14 |
2 files changed, 56 insertions, 35 deletions
diff --git a/lua/nvim-lsp-installer/installers/context.lua b/lua/nvim-lsp-installer/installers/context.lua index 2f56e25c..20097915 100644 --- a/lua/nvim-lsp-installer/installers/context.lua +++ b/lua/nvim-lsp-installer/installers/context.lua @@ -7,17 +7,22 @@ local M = {} local function fetch(url, callback) local stdio = process.in_memory_sink() + 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 if platform.is_unix then process.spawn("wget", { args = { "-nv", "-O", "-", url }, stdio_sink = stdio.sink, - }, function(success) - if success then - callback(nil, table.concat(stdio.buffers.stdout, "")) - else - callback(("Failed to fetch url=%s"):format(url), nil) - end - end) + }, on_exit) elseif platform.is_win then local script = { "$ProgressPreference = 'SilentlyContinue'", @@ -26,30 +31,39 @@ local function fetch(url, callback) process.spawn("powershell.exe", { args = { "-Command", table.concat(script, ";") }, stdio_sink = stdio.sink, - }, function(success) - if success then - callback(nil, table.concat(stdio.buffers.stdout, "")) - else - callback(("Failed to fetch url=%s"):format(url), nil) - end - end) + }, on_exit) else error "Unexpected error: Unsupported OS." end end function M.github_release_file(repo, file) - local function get_download_url(version) - return ("https://github.com/%s/releases/download/%s/%s"):format( - repo, - version, - type(file) == "function" and file(version) or file - ) - end - return function(server, callback, context) + local function get_download_url(version) + local target_file = type(file) == "function" and file(version) or file + if not target_file then + log.fmt_error( + "Unable to find which release file to download. server_name=%s, repo=%s", + server.name, + repo + ) + context.stdio_sink.stderr( + ( + "Could not find which release file to download. Most likely, the current operating system or architecture (%s) is not supported.\n" + ):format(platform.arch) + ) + return nil + end + + return ("https://github.com/%s/releases/download/%s/%s"):format(repo, version, target_file) + end if context.requested_server_version then - context.github_release_file = get_download_url(context.requested_server_version) + -- User has already provided a version - don't fetch the latest version from GitHub + local download_url = get_download_url(context.requested_server_version) + if not download_url then + return callback(false) + end + context.github_release_file = download_url callback(true) else context.stdio_sink.stdout "Fetching latest release version from GitHub API...\n" @@ -57,15 +71,18 @@ function M.github_release_file(repo, file) ("https://api.github.com/repos/%s/releases/latest"):format(repo), vim.schedule_wrap(function(err, response) if err then - context.stdio_sink.stderr "Failed to fetch latest release version from GitHub API.\n" + context.stdio_sink.stderr(tostring(err)) + return callback(false) + end + local version = Data.json_decode(response).tag_name + log.debug("Resolved latest version", server.name, version) + context.requested_server_version = version + local download_url = get_download_url(version) + if not download_url then return callback(false) - else - local version = Data.json_decode(response).tag_name - log.debug("Resolved latest version", server.name, version) - context.requested_server_version = version - context.github_release_file = get_download_url(version) - callback(true) end + context.github_release_file = download_url + callback(true) end) ) end diff --git a/lua/nvim-lsp-installer/process.lua b/lua/nvim-lsp-installer/process.lua index bb40b25b..dca7b328 100644 --- a/lua/nvim-lsp-installer/process.lua +++ b/lua/nvim-lsp-installer/process.lua @@ -95,8 +95,8 @@ function M.spawn(cmd, opts, callback) } end) - local handle, pid - handle, pid = uv.spawn(cmd, spawn_opts, function(exit_code, signal) + local handle, pid_or_err + handle, pid_or_err = uv.spawn(cmd, spawn_opts, function(exit_code, signal) local successful = exit_code == 0 and signal == 0 handle:close() if not stdin:is_closing() then @@ -118,13 +118,17 @@ function M.spawn(cmd, opts, callback) end) if handle == nil then - log.error("Failed to spawn process", cmd, pid) - opts.stdio_sink.stderr(("Failed to spawn process cmd=%s pid=%s\n"):format(cmd, pid)) + 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)) + else + opts.stdio_sink.stderr(("Failed to spawn process cmd=%s err=%s\n"):format(cmd, pid_or_err)) + end callback(false) return nil, nil end - log.debug("Spawned with pid", pid) + log.debug("Spawned with pid", pid_or_err) stdout:read_start(connect_sink(stdout, opts.stdio_sink.stdout)) stderr:read_start(connect_sink(stderr, opts.stdio_sink.stderr)) |
