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
|
local EventEmitter = require "mason-core.EventEmitter"
local InstallLocation = require "mason-core.installer.InstallLocation"
local Optional = require "mason-core.optional"
local Purl = require "mason-core.purl"
local Result = require "mason-core.result"
local _ = require "mason-core.functional"
local fs = require "mason-core.fs"
local log = require "mason-core.log"
local path = require "mason-core.path"
local settings = require "mason.settings"
local Semaphore = require("mason-core.async.control").Semaphore
---@alias PackageInstallOpts { version?: string, debug?: boolean, target?: string, force?: boolean, strict?: boolean, location?: InstallLocation }
---@alias PackageUninstallOpts { bypass_permit?: boolean, location?: InstallLocation }
---@class AbstractPackage : EventEmitter
---@field name string
---@field spec RegistryPackageSpec
---@field registry RegistrySource
---@field private install_handle InstallHandle? The currently associated installation handle.
---@field private uninstall_handle InstallHandle? The currently associated uninstallation handle.
local AbstractPackage = {}
AbstractPackage.__index = AbstractPackage
setmetatable(AbstractPackage, { __index = EventEmitter })
AbstractPackage.SEMAPHORE = Semaphore:new(settings.current.max_concurrent_installers)
---@type PackageInstallOpts
AbstractPackage.DEFAULT_INSTALL_OPTS = {
debug = false,
force = false,
strict = false,
target = nil,
version = nil,
}
---@param spec RegistryPackageSpec
---@param reg RegistrySource
function AbstractPackage:new(spec, reg)
local instance = EventEmitter.new(self)
instance.name = spec.name -- for convenient access
instance.spec = spec
instance.registry = reg
return instance
end
---@param spec RegistryPackageSpec
---@param reg RegistrySource
function AbstractPackage:update(spec, reg)
self.name = spec.name -- shouldn't be necessary but might as well
self.spec = spec
self.registry = reg
return self
end
---@return boolean
function AbstractPackage:is_installing()
return self:get_install_handle()
:map(
---@param handle InstallHandle
function(handle)
return not handle:is_closed()
end
)
:or_else(false)
end
---@return boolean
function AbstractPackage:is_uninstalling()
return self:get_uninstall_handle()
:map(
---@param handle InstallHandle
function(handle)
return not handle:is_closed()
end
)
:or_else(false)
end
function AbstractPackage:get_install_handle()
return Optional.of_nilable(self.install_handle)
end
function AbstractPackage:get_uninstall_handle()
return Optional.of_nilable(self.uninstall_handle)
end
---@param location InstallLocation
function AbstractPackage:new_handle(location)
assert(location, "Cannot create new handle without a location.")
local InstallHandle = require "mason-core.installer.InstallHandle"
local handle = InstallHandle:new(self, location)
-- Ideally we'd decouple this and leverage Mason's event system, but to allow loading as little as possible during
-- setup (i.e. not load modules related to Mason's event system) of the mason.nvim plugin we explicitly call into
-- terminator here.
require("mason-core.terminator").register(handle)
return handle
end
---@param location? InstallLocation
function AbstractPackage:new_install_handle(location)
location = location or InstallLocation.global()
log.fmt_trace("Creating new installation handle for %s", self)
self:get_install_handle():if_present(function(handle)
assert(handle:is_closed(), "Cannot create new install handle because existing handle is not closed.")
end)
self.install_handle = self:new_handle(location)
self:emit("install:handle", self.install_handle)
return self.install_handle
end
---@param location? InstallLocation
function AbstractPackage:new_uninstall_handle(location)
location = location or InstallLocation.global()
log.fmt_trace("Creating new uninstallation handle for %s", self)
self:get_uninstall_handle():if_present(function(handle)
assert(handle:is_closed(), "Cannot create new uninstall handle because existing handle is not closed.")
end)
self.uninstall_handle = self:new_handle(location)
self:emit("uninstall:handle", self.uninstall_handle)
return self.uninstall_handle
end
---@param opts? PackageInstallOpts
function AbstractPackage:is_installable(opts)
return require("mason-core.installer.compiler").parse(self.spec, opts or {}):is_success()
end
---@param location? InstallLocation
---@return Optional # Optional<InstallReceipt>
function AbstractPackage:get_receipt(location)
location = location or InstallLocation.global()
local receipt_path = location:receipt(self.name)
if fs.sync.file_exists(receipt_path) then
local receipt = require "mason-core.receipt"
return Optional.of(receipt.InstallReceipt.from_json(vim.json.decode(fs.sync.read_file(receipt_path))))
end
return Optional.empty()
end
---@param location? InstallLocation
---@return boolean
function AbstractPackage:is_installed(location)
error "Unimplemented."
end
---@return Result # Result<string[]>
function AbstractPackage:get_all_versions()
local compiler = require "mason-core.installer.compiler"
return Result.try(function(try)
---@type Purl
local purl = try(Purl.parse(self.spec.source.id))
---@type InstallerCompiler
local compiler = try(compiler.get_compiler(purl))
return compiler.get_versions(purl, self.spec.source)
end)
end
---@return string
function AbstractPackage:get_latest_version()
return Purl.parse(self.spec.source.id)
:map(_.prop "version")
:get_or_throw(("Unable to retrieve version from malformed purl: %s."):format(self.spec.source.id))
end
---@param location? InstallLocation
---@return string?
function AbstractPackage:get_installed_version(location)
return self:get_receipt(location)
:and_then(
---@param receipt InstallReceipt
function(receipt)
local source = receipt:get_source()
if source.id then
return Purl.parse(source.id):map(_.prop "version"):ok()
else
return Optional.empty()
end
end
)
:or_else(nil)
end
---@param opts? PackageInstallOpts
---@param callback? InstallRunnerCallback
---@return InstallHandle
function AbstractPackage:install(opts, callback)
error "Unimplemented."
end
---@param opts? PackageUninstallOpts
---@param callback? InstallRunnerCallback
---@return InstallHandle
function AbstractPackage:uninstall(opts, callback)
error "Unimplemented."
end
---@private
---@param location? InstallLocation
function AbstractPackage:unlink(location)
location = location or InstallLocation.global()
log.fmt_trace("Unlinking", self, location)
local linker = require "mason-core.installer.linker"
return self:get_receipt(location):ok_or("Unable to find receipt."):and_then(function(receipt)
return linker.unlink(self, receipt, location)
end)
end
---@async
---@private
---@return Permit
function AbstractPackage:acquire_permit()
error "Unimplemented."
end
return AbstractPackage
|