From 579d4bd178f4aae76a4f4dc2ea34bf5e8fa6231b Mon Sep 17 00:00:00 2001 From: William Boman Date: Sat, 22 Apr 2023 19:20:52 +0200 Subject: feat(registry): add .get_all_package_specs() (#1247) This is a faster method than .get_all_packages() due to the fact that it only loads package specifications without instantiating mason-core.package instances. Useful for situations where one only needs to read spec data. --- lua/mason-registry/sources/github.lua | 49 +++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 19 deletions(-) (limited to 'lua/mason-registry/sources/github.lua') diff --git a/lua/mason-registry/sources/github.lua b/lua/mason-registry/sources/github.lua index ec758144..2e9c5210 100644 --- a/lua/mason-registry/sources/github.lua +++ b/lua/mason-registry/sources/github.lua @@ -6,11 +6,9 @@ local fetch = require "mason-core.fetch" local fs = require "mason-core.fs" local log = require "mason-core.log" local path = require "mason-core.path" -local platform = require "mason-core.platform" local providers = require "mason-core.providers" local registry_installer = require "mason-core.installer.registry" local settings = require "mason.settings" -local spawn = require "mason-core.spawn" -- Parse sha256sum text output to a table structure local parse_checksums = _.compose(_.from_pairs, _.map(_.compose(_.reverse, _.split " ")), _.split "\n", _.trim) @@ -48,43 +46,56 @@ function GitHubRegistrySource:is_installed() return fs.sync.file_exists(self.data_file) and fs.sync.file_exists(self.info_file) end +---@return RegistryPackageSpec[] +function GitHubRegistrySource:get_all_package_specs() + if not self:is_installed() then + return {} + end + local data = vim.json.decode(fs.sync.read_file(self.data_file)) --[[@as RegistryPackageSpec[] ]] + return _.filter_map( + ---@param spec RegistryPackageSpec + function(spec) + -- registry+v1 specifications doesn't include a schema property, so infer it + spec.schema = spec.schema or "registry+v1" + + if not registry_installer.SCHEMA_CAP[spec.schema] then + log.fmt_debug("Excluding package=%s with unsupported schema_version=%s", spec.name, spec.schema) + return Optional.empty() + end + + -- XXX: this is for compatibilty with the PackageSpec structure + spec.desc = spec.description + return Optional.of(spec) + end, + data + ) +end + function GitHubRegistrySource:reload() if not self:is_installed() then return end - local data = vim.json.decode(fs.sync.read_file(self.data_file)) self.buffer = _.compose( _.index_by(_.prop "name"), - _.filter_map( + _.map( ---@param spec RegistryPackageSpec function(spec) - -- registry+v1 specifications doesn't include a schema property, so infer it - spec.schema = spec.schema or "registry+v1" - - if not registry_installer.SCHEMA_CAP[spec.schema] then - log.fmt_debug("Excluding package=%s with unsupported schema_version=%s", spec.name, spec.schema) - return Optional.empty() - end - -- hydrate Pkg.Lang index _.each(function(lang) local _ = Pkg.Lang[lang] end, spec.languages) - -- XXX: this is for compatibilty with the PackageSpec structure - spec.desc = spec.description - local pkg = self.buffer and self.buffer[spec.name] if pkg then -- Apply spec to the existing Package instance. This is important as to not have lingering package -- instances. pkg.spec = spec - return Optional.of(pkg) + return pkg end - return Optional.of(Pkg.new(spec)) + return Pkg.new(spec) end ) - )(data) + )(self:get_all_package_specs()) return self.buffer end @@ -99,7 +110,7 @@ function GitHubRegistrySource:get_package(pkg) end function GitHubRegistrySource:get_all_package_names() - return _.keys(self:get_buffer()) + return _.map(_.prop "name", self:get_all_package_specs()) end function GitHubRegistrySource:get_installer() -- cgit v1.2.3-70-g09d2