From 047ec18da56ad8f331e5c6bc7417dc5a9a6e71cc Mon Sep 17 00:00:00 2001 From: William Boman Date: Wed, 11 Oct 2023 16:31:50 +0200 Subject: refactor!: refactor installer internals and add new Package class methods (#1523) This contains the following changes: 1) `Package:install()` now accepts a second, optional, callback argument which is called when installation finishes (successfully or not). 2) Adds a `Package:is_installing()` method. This contains the following breaking changes: 1) `Package:install()` will now error when called while an installation is already ongoing. Use the new `Package:is_installing()` method to check whether an installation is already running. This also refactors large portions of the tests by removing test globals, removing async_test, and adding the `mason-test` Lua module instead. Test helpers via globals are problematic to work with due to not being detected through tools like the Lua language server without additional configuration. This has been replaced with a Lua module `mason-test`. `async_test` has also been removed in favour of explicitly making use of the `mason-core.async` API. These changes stands for a significant portion of the diff. --- lua/mason-core/installer/compiler/schemas.lua | 75 +++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 lua/mason-core/installer/compiler/schemas.lua (limited to 'lua/mason-core/installer/compiler/schemas.lua') diff --git a/lua/mason-core/installer/compiler/schemas.lua b/lua/mason-core/installer/compiler/schemas.lua new file mode 100644 index 00000000..5e578dbd --- /dev/null +++ b/lua/mason-core/installer/compiler/schemas.lua @@ -0,0 +1,75 @@ +local Result = require "mason-core.result" +local _ = require "mason-core.functional" +local a = require "mason-core.async" +local expr = require "mason-core.installer.compiler.expr" +local fetch = require "mason-core.fetch" +local log = require "mason-core.log" +local path = require "mason-core.path" +local std = require "mason-core.installer.managers.std" + +local M = {} + +---@async +---@param ctx InstallContext +---@param url string +local function download_lsp_schema(ctx, url) + return Result.try(function(try) + local is_vscode_schema = _.starts_with("vscode:", url) + local out_file = path.concat { "mason-schemas", "lsp.json" } + local share_file = path.concat { "mason-schemas", "lsp", ("%s.json"):format(ctx.package.name) } + + if is_vscode_schema then + local url = unpack(_.match("^vscode:(.+)$", url)) + ctx.stdio_sink.stdout(("Downloading LSP configuration schema from %q…\n"):format(url)) + local json = try(fetch(url)) + + ---@type { contributes?: { configuration?: table } } + local schema = try(Result.pcall(vim.json.decode, json)) + local configuration = schema.contributes and schema.contributes.configuration + + if configuration then + ctx.fs:write_file(out_file, vim.json.encode(configuration) --[[@as string]]) + ctx.links.share[share_file] = out_file + else + return Result.failure "Unable to find LSP entry in VSCode schema." + end + else + ctx.stdio_sink.stdout(("Downloading LSP configuration schema from %q…\n"):format(url)) + try(std.download_file(url, out_file)) + ctx.links.share[share_file] = out_file + end + end) +end + +---@async +---@param ctx InstallContext +---@param spec RegistryPackageSpec +---@param purl Purl +---@param source ParsedPackageSource +---@nodiscard +function M.download(ctx, spec, purl, source) + return Result.try(function(try) + log.debug("schemas: download", ctx.package, spec.schemas) + local schemas = spec.schemas + if not schemas then + return + end + ---@type RegistryPackageSchemas + local interpolated_schemas = try(expr.tbl_interpolate(schemas, { version = purl.version, source = source })) + ctx.fs:mkdir "mason-schemas" + + if interpolated_schemas.lsp then + try(a.wait_first { + function() + return download_lsp_schema(ctx, interpolated_schemas.lsp) + end, + function() + a.sleep(5000) + return Result.failure "Schema download timed out." + end, + }) + end + end) +end + +return M -- cgit v1.2.3-70-g09d2