aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2023-04-22 22:35:14 +0200
committerGitHub <noreply@github.com>2023-04-22 22:35:14 +0200
commit54c9176cb82fe1c227978c6df2e2b29f985cbd02 (patch)
treee329148d6211513feec0e02ef8610883f55a9e0f /lua/mason-core
parentfeat(registry): add .get_all_package_specs() (#1247) (diff)
downloadmason-54c9176cb82fe1c227978c6df2e2b29f985cbd02.tar
mason-54c9176cb82fe1c227978c6df2e2b29f985cbd02.tar.gz
mason-54c9176cb82fe1c227978c6df2e2b29f985cbd02.tar.bz2
mason-54c9176cb82fe1c227978c6df2e2b29f985cbd02.tar.lz
mason-54c9176cb82fe1c227978c6df2e2b29f985cbd02.tar.xz
mason-54c9176cb82fe1c227978c6df2e2b29f985cbd02.tar.zst
mason-54c9176cb82fe1c227978c6df2e2b29f985cbd02.zip
refactor(schemas): don't vendor schemas in mason.nvim (#1248)
Instead, schemas are now defined in the package registry and downloaded during installation. See https://github.com/mason-org/mason-registry/pull/1319.
Diffstat (limited to 'lua/mason-core')
-rw-r--r--lua/mason-core/installer/registry/init.lua11
-rw-r--r--lua/mason-core/installer/registry/link.lua9
-rw-r--r--lua/mason-core/installer/registry/schemas.lua64
-rw-r--r--lua/mason-core/package/init.lua16
4 files changed, 92 insertions, 8 deletions
diff --git a/lua/mason-core/installer/registry/init.lua b/lua/mason-core/installer/registry/init.lua
index 6c22d227..f5655572 100644
--- a/lua/mason-core/installer/registry/init.lua
+++ b/lua/mason-core/installer/registry/init.lua
@@ -5,6 +5,7 @@ local _ = require "mason-core.functional"
local a = require "mason-core.async"
local link = require "mason-core.installer.registry.link"
local log = require "mason-core.log"
+local schemas = require "mason-core.installer.registry.schemas"
local M = {}
@@ -170,6 +171,16 @@ function M.compile(spec, opts)
-- Run installer
try(parsed.provider.install(ctx, parsed.source, parsed.purl))
+ if spec.schemas then
+ local result = schemas.download(ctx, spec, parsed.purl, parsed.source):on_failure(function(err)
+ log.error("Failed to download schemas", ctx.package, err)
+ end)
+ if opts.strict then
+ -- schema download sources are not considered stable nor a critical feature, so we only fail in strict mode
+ try(result)
+ end
+ end
+
-- Expand & register links
if spec.bin then
try(link.bin(ctx, spec, parsed.purl, parsed.source))
diff --git a/lua/mason-core/installer/registry/link.lua b/lua/mason-core/installer/registry/link.lua
index 2b4027e9..76741112 100644
--- a/lua/mason-core/installer/registry/link.lua
+++ b/lua/mason-core/installer/registry/link.lua
@@ -273,9 +273,10 @@ end
---@param spec RegistryPackageSpec
---@param purl Purl
---@param source ParsedPackageSource
+---@nodiscard
M.bin = function(ctx, spec, purl, source)
return expand_bin(ctx, spec, purl, source):on_success(function(links)
- ctx.links.bin = links
+ ctx.links.bin = vim.tbl_extend("force", ctx.links.bin, links)
end)
end
@@ -284,9 +285,10 @@ end
---@param spec RegistryPackageSpec
---@param purl Purl
---@param source ParsedPackageSource
+---@nodiscard
M.share = function(ctx, spec, purl, source)
return expand_file_spec(ctx, purl, source, spec.share):on_success(function(links)
- ctx.links.share = links
+ ctx.links.share = vim.tbl_extend("force", ctx.links.share, links)
end)
end
@@ -295,9 +297,10 @@ end
---@param spec RegistryPackageSpec
---@param purl Purl
---@param source ParsedPackageSource
+---@nodiscard
M.opt = function(ctx, spec, purl, source)
return expand_file_spec(ctx, purl, source, spec.opt):on_success(function(links)
- ctx.links.opt = links
+ ctx.links.opt = vim.tbl_extend("force", ctx.links.opt, links)
end)
end
diff --git a/lua/mason-core/installer/registry/schemas.lua b/lua/mason-core/installer/registry/schemas.lua
new file mode 100644
index 00000000..a52fc5bf
--- /dev/null
+++ b/lua/mason-core/installer/registry/schemas.lua
@@ -0,0 +1,64 @@
+local Result = require "mason-core.result"
+local _ = require "mason-core.functional"
+local expr = require "mason-core.installer.registry.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))
+ 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
+ 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(download_lsp_schema(ctx, interpolated_schemas.lsp))
+ end
+ end)
+end
+
+return M
diff --git a/lua/mason-core/package/init.lua b/lua/mason-core/package/init.lua
index 7ebac5af..e0a361ca 100644
--- a/lua/mason-core/package/init.lua
+++ b/lua/mason-core/package/init.lua
@@ -63,6 +63,9 @@ local PackageMt = { __index = Package }
---@field id string PURL-compliant identifier.
---@field version_overrides? RegistryPackageSourceVersionOverride[]
+---@class RegistryPackageSchemas
+---@field lsp string?
+
---@class RegistryPackageSpec
---@field schema '"registry+v1"'
---@field name string
@@ -72,6 +75,7 @@ local PackageMt = { __index = Package }
---@field languages string[]
---@field categories string[]
---@field source RegistryPackageSource
+---@field schemas RegistryPackageSchemas?
---@field bin table<string, string>?
---@field share table<string, string>?
---@field opt table<string, string>?
@@ -127,7 +131,7 @@ function Package:new_handle()
return handle
end
----@alias PackageInstallOpts { version?: string, debug?: boolean, target?: string, force?: boolean }
+---@alias PackageInstallOpts { version?: string, debug?: boolean, target?: string, force?: boolean, strict?: boolean }
---@param opts? PackageInstallOpts
---@return InstallHandle
@@ -292,11 +296,13 @@ function Package:check_new_version(callback)
end
function Package:get_lsp_settings_schema()
- local ok, schema = pcall(require, ("mason-schemas.lsp.%s"):format(self.name))
- if not ok then
- return Optional.empty()
+ local schema_file = path.share_prefix(path.concat { "mason-schemas", "lsp", ("%s.json"):format(self.name) })
+ if fs.sync.file_exists(schema_file) then
+ return Result.pcall(vim.json.decode, fs.sync.read_file(schema_file), {
+ luanil = { object = true, array = true },
+ }):ok()
end
- return Optional.of(schema)
+ return Optional.empty()
end
---@return boolean