1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
---@class RegistrySource
---@field id string
---@field get_package fun(self: RegistrySource, pkg_name: string): Package?
---@field get_all_package_names fun(self: RegistrySource): string[]
---@field get_all_package_specs fun(self: RegistrySource): RegistryPackageSpec[]
---@field get_display_name fun(self: RegistrySource): string
---@field is_installed fun(self: RegistrySource): boolean
---@field install fun(self: RegistrySource): Result
---@alias RegistrySourceType '"github"' | '"lua"' | '"file"'
---@class LazySource
---@field type RegistrySourceType
---@field id string
---@field init fun(id: string): RegistrySource
local LazySource = {}
LazySource.__index = LazySource
---@param id string
function LazySource.GitHub(id)
local namespace, name = id:match "^(.+)/(.+)$"
if not namespace or not name then
error(("Failed to parse repository from GitHub registry: %q"):format(id), 0)
end
local name, version = unpack(vim.split(name, "@"))
local GitHubRegistrySource = require "mason-registry.sources.github"
return GitHubRegistrySource:new {
id = id,
namespace = namespace,
name = name,
version = version,
}
end
---@param id string
function LazySource.Lua(id)
local LuaRegistrySource = require "mason-registry.sources.lua"
return LuaRegistrySource:new {
id = id,
mod = id,
}
end
---@param id string
function LazySource.File(id)
local FileRegistrySource = require "mason-registry.sources.file"
return FileRegistrySource:new {
id = id,
path = id,
}
end
---@param type RegistrySourceType
---@param id string
---@param init fun(id: string): RegistrySource
function LazySource:new(type, id, init)
local instance = setmetatable({}, self)
instance.type = type
instance.id = id
instance.init = init
return instance
end
function LazySource:get()
if not self.instance then
self.instance = self.init(self.id)
end
return self.instance
end
function LazySource:__tostring()
return ("LazySource(type=%s, id=%s)"):format(self.type, self.id)
end
---@param str string
local function split_once_left(str, char)
for i = 1, #str do
if str:sub(i, i) == char then
local segment = str:sub(1, i - 1)
return segment, str:sub(i + 1)
end
end
return str
end
---@param registry_id string
local function parse(registry_id)
local type, id = split_once_left(registry_id, ":")
assert(id, ("Malformed registry %q"):format(registry_id))
if type == "github" then
return LazySource:new(type, id, LazySource.GitHub)
elseif type == "lua" then
return LazySource:new(type, id, LazySource.Lua)
elseif type == "file" then
return LazySource:new(type, id, LazySource.File)
end
error(("Unknown registry type: %s"):format(type))
end
---@class LazySourceCollection
---@field list LazySource[]
local LazySourceCollection = {}
LazySourceCollection.__index = LazySourceCollection
---@return LazySourceCollection
function LazySourceCollection:new()
local instance = {}
setmetatable(instance, self)
instance.list = {}
return instance
end
---@param registry string
function LazySourceCollection:append(registry)
table.insert(self.list, parse(registry))
return self
end
---@param registry string
function LazySourceCollection:prepend(registry)
table.insert(self.list, 1, parse(registry))
return self
end
function LazySourceCollection:is_all_installed()
for source in self:iterate { include_uninstalled = true } do
if not source:is_installed() then
return false
end
end
return true
end
function LazySourceCollection:checksum()
---@type string[]
local registry_ids = vim.tbl_map(
---@param source LazySource
function(source)
return source.id
end,
self.list
)
table.sort(registry_ids)
return vim.fn.sha256(table.concat(registry_ids, ""))
end
---@param opts? { include_uninstalled?: boolean }
function LazySourceCollection:iterate(opts)
opts = opts or {}
local idx = 1
return function()
while idx <= #self.list do
local source = self.list[idx]:get()
idx = idx + 1
if opts.include_uninstalled or source:is_installed() then
return source
end
end
end
end
---@param opts? { include_uninstalled?: boolean }
function LazySourceCollection:to_list(opts)
opts = opts or {}
local list = {}
for source in self:iterate(opts) do
table.insert(list, source)
end
return list
end
function LazySourceCollection:__tostring()
return ("LazySourceCollection(list={%s})"):format(table.concat(vim.tbl_map(tostring, self.list), ", "))
end
return LazySourceCollection
|