|
| 1 | +local Package = require "mason-core.package" |
| 2 | +local Result = require "mason-core.result" |
| 3 | +local _ = require "mason-core.functional" |
| 4 | +local InstallReceipt = require("mason-core.receipt").InstallReceipt |
| 5 | +local InstallLocation = require "mason-core.installer.InstallLocation" |
| 6 | +local fs = require "mason-core.fs" |
| 7 | +local log = require "mason-core.log" |
| 8 | + |
| 9 | +---@class SynthesizedRegistrySource : RegistrySource |
| 10 | +---@field buffer table<string, Package> |
| 11 | +local SynthesizedRegistrySource = {} |
| 12 | +SynthesizedRegistrySource.__index = SynthesizedRegistrySource |
| 13 | + |
| 14 | +function SynthesizedRegistrySource:new() |
| 15 | + ---@type SynthesizedRegistrySource |
| 16 | + local instance = {} |
| 17 | + setmetatable(instance, self) |
| 18 | + instance.buffer = {} |
| 19 | + return instance |
| 20 | +end |
| 21 | + |
| 22 | +function SynthesizedRegistrySource:is_installed() |
| 23 | + return true |
| 24 | +end |
| 25 | + |
| 26 | +---@return RegistryPackageSpec[] |
| 27 | +function SynthesizedRegistrySource:get_all_package_specs() |
| 28 | + return {} |
| 29 | +end |
| 30 | + |
| 31 | +---@param pkg_name string |
| 32 | +---@param receipt InstallReceipt |
| 33 | +---@return Package |
| 34 | +function SynthesizedRegistrySource:load_package(pkg_name, receipt) |
| 35 | + local installed_version = receipt:get_installed_package_version() |
| 36 | + local source = { |
| 37 | + id = ("pkg:mason/%s@%s"):format(pkg_name, installed_version or "N%2FA"), -- N%2FA = N/A |
| 38 | + install = function() |
| 39 | + error("This package can no longer be installed because it has been removed from the registry.", 0) |
| 40 | + end, |
| 41 | + } |
| 42 | + ---@type RegistryPackageSpec |
| 43 | + local spec = { |
| 44 | + schema = "registry+v1", |
| 45 | + name = pkg_name, |
| 46 | + description = "", |
| 47 | + categories = {}, |
| 48 | + languages = {}, |
| 49 | + homepage = "", |
| 50 | + licenses = {}, |
| 51 | + deprecation = { |
| 52 | + since = receipt:get_installed_package_version() or "N/A", |
| 53 | + message = "This package has been removed from the registry.", |
| 54 | + }, |
| 55 | + source = source, |
| 56 | + } |
| 57 | + local existing_pkg = self.buffer[pkg_name] |
| 58 | + if existing_pkg then |
| 59 | + existing_pkg:update(spec, self) |
| 60 | + return existing_pkg |
| 61 | + else |
| 62 | + local pkg = Package:new(spec, self) |
| 63 | + self.buffer[pkg_name] = pkg |
| 64 | + return pkg |
| 65 | + end |
| 66 | +end |
| 67 | + |
| 68 | +---@param pkg_name string |
| 69 | +---@return Package? |
| 70 | +function SynthesizedRegistrySource:get_package(pkg_name) |
| 71 | + local receipt_path = InstallLocation.global():receipt(pkg_name) |
| 72 | + if fs.sync.file_exists(receipt_path) then |
| 73 | + local ok, receipt_json = pcall(vim.json.decode, fs.sync.read_file(receipt_path)) |
| 74 | + if ok then |
| 75 | + local receipt = InstallReceipt.from_json(receipt_json) |
| 76 | + return self:load_package(pkg_name, receipt) |
| 77 | + else |
| 78 | + log.error("Failed to decode package receipt", pkg_name, receipt_json) |
| 79 | + end |
| 80 | + end |
| 81 | +end |
| 82 | + |
| 83 | +function SynthesizedRegistrySource:get_all_package_names() |
| 84 | + return vim.tbl_keys(self.buffer) |
| 85 | +end |
| 86 | + |
| 87 | +---@async |
| 88 | +function SynthesizedRegistrySource:install() |
| 89 | + return Result.success() |
| 90 | +end |
| 91 | + |
| 92 | +function SynthesizedRegistrySource:get_display_name() |
| 93 | + return "SynthesizedRegistrySource" |
| 94 | +end |
| 95 | + |
| 96 | +function SynthesizedRegistrySource:serialize() |
| 97 | + return {} |
| 98 | +end |
| 99 | + |
| 100 | +---@param other SynthesizedRegistrySource |
| 101 | +function SynthesizedRegistrySource:is_same_location(other) |
| 102 | + return true |
| 103 | +end |
| 104 | + |
| 105 | +function SynthesizedRegistrySource:__tostring() |
| 106 | + return "SynthesizedRegistrySource" |
| 107 | +end |
| 108 | + |
| 109 | +return SynthesizedRegistrySource |
0 commit comments