diff options
| author | William Boman <william@redwill.se> | 2021-09-29 13:48:39 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-29 13:48:39 +0200 |
| commit | e267fe517feaaf7a48323349e3775944db49c158 (patch) | |
| tree | 1c9d869d71c32b0d3b2f9304d9433dbb4ced9903 /lua/nvim-lsp-installer/installers/context.lua | |
| parent | fix stylua (#104) (diff) | |
| download | mason-e267fe517feaaf7a48323349e3775944db49c158.tar mason-e267fe517feaaf7a48323349e3775944db49c158.tar.gz mason-e267fe517feaaf7a48323349e3775944db49c158.tar.bz2 mason-e267fe517feaaf7a48323349e3775944db49c158.tar.lz mason-e267fe517feaaf7a48323349e3775944db49c158.tar.xz mason-e267fe517feaaf7a48323349e3775944db49c158.tar.zst mason-e267fe517feaaf7a48323349e3775944db49c158.zip | |
support installing specific version of language servers, defaults to latest (#106)
Diffstat (limited to 'lua/nvim-lsp-installer/installers/context.lua')
| -rw-r--r-- | lua/nvim-lsp-installer/installers/context.lua | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/lua/nvim-lsp-installer/installers/context.lua b/lua/nvim-lsp-installer/installers/context.lua new file mode 100644 index 00000000..057c3620 --- /dev/null +++ b/lua/nvim-lsp-installer/installers/context.lua @@ -0,0 +1,89 @@ +local Data = require "nvim-lsp-installer.data" +local Log = require "nvim-lsp-installer.log" +local process = require "nvim-lsp-installer.process" +local platform = require "nvim-lsp-installer.platform" + +local M = {} + +local function fetch(url, callback) + local stdio = process.in_memory_sink() + 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) + elseif platform.is_win then + local script = { + "$ProgressPreference = 'SilentlyContinue'", + ("Write-Output (iwr -Uri %q).Content"):format(url), + } + 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) + 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) + if context.requested_server_version then + context.github_release_file = get_download_url(context.requested_server_version) + callback(true) + else + context.stdio_sink.stdout "Fetching latest release version from GitHub API..." + fetch( + ("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." + 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 + end) + ) + end + end +end + +function M.capture(fn) + return function(server, callback, context, ...) + local installer = fn(context) + installer(server, callback, context, ...) + end +end + +function M.set(fn) + return function(_, callback, context) + fn(context) + callback(true) + end +end + +return M |
