From d0119c18adff184c5c75f7ec59b6f12b301d268d Mon Sep 17 00:00:00 2001 From: William Boman Date: Mon, 11 Sep 2023 00:37:05 +0200 Subject: refactor!: consolidate Lua registry sources and the Package API (#1498) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **This removes the following APIs:** - `Package:check_new_version()`. Instead use the new `Package:get_latest_version()`. **This has a breaking change in the following APIs:** - `Package:get_installed_version()` now no longer takes a callback but instead returns the installed version or `nil` if not installed.
To handle these breaking changes in plugins, leverage the `mason.version` module, for example: ```lua local mason_version = require("mason.version") local registry = require("mason-registry") local pkg = registry.get_package("rust-analyzer") if mason_version.MAJOR_VERSION < 2 then -- before pkg:check_new_version(function (success, new_version) -- … end) pkg:get_installed_version(function (success, installed_version) -- … end) else -- after local new_version = pkg:get_latest_version() local installed_version = pkg:get_installed_version() fi ```
---
This change also introduces breaking changes for Lua registry sources, by consolidating the package schema with the registry. The following is an example of a package defined in a Lua registry, following the new schema: ```lua local Pkg = require("mason-core.package") return Pkg.new { schema = "registry+v1", name = "ripgrep", description = "ripgrep recursively searches directories for a regex pattern while respecting your gitignore.", homepage = "https://github.com/BurntSushi/ripgrep", licenses = { Pkg.License.MIT }, languages = {}, categories = {}, source = { id = "pkg:mason/ripgrep@13.0.0", ---@param ctx InstallContext ---@param purl Purl install = function(ctx, purl) -- Arbitrary installation code. end, }, bin = { rg = "./bin/rg", }, } ```
--- lua/mason-core/package/version-check.lua | 80 -------------------------------- 1 file changed, 80 deletions(-) delete mode 100644 lua/mason-core/package/version-check.lua (limited to 'lua/mason-core/package/version-check.lua') diff --git a/lua/mason-core/package/version-check.lua b/lua/mason-core/package/version-check.lua deleted file mode 100644 index 66d9ad13..00000000 --- a/lua/mason-core/package/version-check.lua +++ /dev/null @@ -1,80 +0,0 @@ -local Result = require "mason-core.result" -local cargo = require "mason-core.managers.cargo" -local composer = require "mason-core.managers.composer" -local gem = require "mason-core.managers.gem" -local git = require "mason-core.managers.git" -local github = require "mason-core.managers.github" -local go = require "mason-core.managers.go" -local log = require "mason-core.log" -local luarocks = require "mason-core.managers.luarocks" -local npm = require "mason-core.managers.npm" -local pip3 = require "mason-core.managers.pip3" - ----@param field_name string -local function version_in_receipt(field_name) - ---@param receipt InstallReceipt - ---@return Result - return function(receipt) - return Result.success(receipt.primary_source[field_name]) - end -end - ----@type table -local get_installed_version_by_type = { - ["npm"] = npm.get_installed_primary_package_version, - ["pip3"] = pip3.get_installed_primary_package_version, - ["gem"] = gem.get_installed_primary_package_version, - ["cargo"] = cargo.get_installed_primary_package_version, - ["composer"] = composer.get_installed_primary_package_version, - ["git"] = git.get_installed_revision, - ["go"] = go.get_installed_primary_package_version, - ["luarocks"] = luarocks.get_installed_primary_package_version, - ["github_release_file"] = version_in_receipt "release", - ["github_release"] = version_in_receipt "release", - ["github_tag"] = version_in_receipt "tag", -} - ----@class NewPackageVersion ----@field name string ----@field current_version string ----@field latest_version string - -local get_new_version_by_type = { - ["npm"] = npm.check_outdated_primary_package, - ["pip3"] = pip3.check_outdated_primary_package, - ["git"] = git.check_outdated_git_clone, - ["cargo"] = cargo.check_outdated_primary_package, - ["composer"] = composer.check_outdated_primary_package, - ["gem"] = gem.check_outdated_primary_package, - ["go"] = go.check_outdated_primary_package, - ["luarocks"] = luarocks.check_outdated_primary_package, - ["github_release_file"] = github.check_outdated_primary_package_release, - ["github_release"] = github.check_outdated_primary_package_release, - ["github_tag"] = github.check_outdated_primary_package_tag, -} - ----@param provider_mapping table -local function version_check(provider_mapping) - ---@param receipt InstallReceipt - ---@param install_dir string - return function(receipt, install_dir) - local check = provider_mapping[receipt.primary_source.type] - if not check then - return Result.failure( - ("Packages installed via %s does not yet support version check."):format(receipt.primary_source.type) - ) - end - return check(receipt, install_dir) - :on_success(function(version) - log.debug("Version check", version) - end) - :on_failure(function(failure) - log.debug("Version check failed", tostring(failure)) - end) - end -end - -return { - get_installed_version = version_check(get_installed_version_by_type), - get_new_version = version_check(get_new_version_by_type), -} -- cgit v1.2.3-70-g09d2