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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
local _ = require "mason-core.functional"
local log = require "mason-core.log"
---@class RegistrySource
---@field id string
---@field system boolean
---@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
---@field serialize fun(self: RegistrySource): InstallReceiptRegistry
---@field is_same_location fun(self: RegistrySource, other: RegistrySource): boolean
---@alias RegistrySourceType '"github"' | '"lua"' | '"file"' | '"synthesized"'
---@class LazySource
---@field type RegistrySourceType
---@field id string
---@field init fun(id: string, system: boolean): RegistrySource
---@field system boolean
local LazySource = {}
LazySource.__index = LazySource
---@param id string
---@param system boolean
function LazySource.GitHub(id, system)
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,
}, system)
end
---@param id string
---@param system boolean
function LazySource.Lua(id, system)
local LuaRegistrySource = require "mason-registry.sources.lua"
return LuaRegistrySource:new({
id = id,
mod = id,
}, system)
end
---@param id string
---@param system boolean
function LazySource.File(id, system)
local FileRegistrySource = require "mason-registry.sources.file"
return FileRegistrySource:new({
id = id,
path = id,
}, system)
end
function LazySource.Synthesized()
local SynthesizedSource = require "mason-registry.sources.synthesized"
return SynthesizedSource:new()
end
---@param type RegistrySourceType
---@param id string
---@param init fun(id: string): RegistrySource
---@param system boolean
function LazySource:new(type, id, init, system)
---@type LazySource
local instance = setmetatable({}, self)
instance.type = type
instance.id = id
instance.init = init
instance.system = system
return instance
end
function LazySource:get()
if not self.instance then
self.instance = self.init(self.id, self.system)
end
return self.instance
end
---@param other LazySource
function LazySource:is_same_location(other)
if self.type == other.type then
return self:get():is_same_location(other:get())
end
return false
end
function LazySource:get_full_id()
return ("%s:%s"):format(self.type, self.id)
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
---@param system boolean
local function parse(registry_id, system)
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, system)
elseif type == "lua" then
return LazySource:new(type, id, LazySource.Lua, system)
elseif type == "file" then
return LazySource:new(type, id, LazySource.File, system)
end
error(("Unknown registry type: %s"):format(type))
end
---@class LazySourceCollection
---@field state_file string
---@field system boolean?
---@field list LazySource[]
---@field synthesized LazySource
---@field install_channel OneShotChannel?
local LazySourceCollection = {}
LazySourceCollection.__index = LazySourceCollection
---@return LazySourceCollection
---@param state_file string
---@param system boolean?
function LazySourceCollection:new(state_file, system)
---@type LazySourceCollection
local instance = {}
setmetatable(instance, self)
instance.state_file = state_file
instance.system = system
instance.list = {}
instance.synthesized = LazySource:new("synthesized", "synthesized", LazySource.Synthesized)
return instance
end
---@return { checksum: string, timestamp: integer }?
function LazySourceCollection:get_install_state()
local fs = require "mason-core.fs"
if fs.sync.file_exists(self.state_file) then
local parse_state_file =
_.compose(_.evolve { timestamp = tonumber }, _.zip_table { "checksum", "timestamp" }, _.split "\n")
return parse_state_file(fs.sync.read_file(self.state_file))
end
end
function LazySourceCollection:get_state_file()
return self.state_file
end
---@param registry string
function LazySourceCollection:append(registry)
self:unique_insert(parse(registry, not not self.system))
return self
end
---@param registry string
function LazySourceCollection:prepend(registry)
self:unique_insert(parse(registry, not not self.system), 1)
return self
end
---@param source LazySource
---@param idx? integer
function LazySourceCollection:unique_insert(source, idx)
idx = idx or #self.list + 1
if idx > 1 then
for i = 1, math.min(idx, #self.list) do
if self.list[i]:is_same_location(source) then
log.fmt_warn(
"Ignoring duplicate registry entry %s (duplicate of %s)",
source:get_full_id(),
self.list[i]:get_full_id()
)
return
end
end
end
for i = #self.list, idx, -1 do
if self.list[i]:is_same_location(source) then
table.remove(self.list, i)
end
end
table.insert(self.list, idx, source)
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
---@alias LazySourceCollectionIterate { include_uninstalled?: boolean, include_synthesized?: boolean }
---@param opts? LazySourceCollectionIterate
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
-- We've exhausted the true registry sources, fall back to the synthesized registry source.
if idx == #self.list + 1 and opts.include_synthesized ~= false then
idx = idx + 1
return self.synthesized:get()
end
end
end
---@param opts? LazySourceCollectionIterate
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
---@param idx integer
function LazySourceCollection:get(idx)
return self.list[idx]
end
function LazySourceCollection:size()
return #self.list
end
function LazySourceCollection:__tostring()
return ("LazySourceCollection(list={%s})"):format(table.concat(vim.tbl_map(tostring, self.list), ", "))
end
return LazySourceCollection
|