-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathmason.lua
More file actions
108 lines (92 loc) · 2.76 KB
/
mason.lua
File metadata and controls
108 lines (92 loc) · 2.76 KB
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
local log = require('java.utils.log')
local mason_reg = require('mason-registry')
local async = require('java-core.utils.async')
local await = async.wait_handle_ok
local mason_v2 = require('mason.version').MAJOR_VERSION == 2
local M = {}
function M.is_available(package_name, package_version)
-- get_package errors if the package is not available in Mason 2.x
-- it works fine in Mason 1.x this way too.
local has_pkg, pkg = pcall(mason_reg.get_package, package_name)
if not has_pkg then
return false
end
local installed_version
if mason_v2 then
-- the compiler will complain when Mason 1.x is in use
---@diagnostic disable-next-line: missing-parameter
installed_version = pkg:get_installed_version()
else
-- the compiler will complain when mason 2.x is in use
---@diagnostic disable-next-line: param-type-mismatch
pkg:get_installed_version(function(success, version)
if success then
installed_version = version
end
end)
end
return installed_version == package_version
end
function M.is_installed(package_name, package_version)
-- get_package errors if the package is not available in Mason 2.x
-- it works fine in Mason 1.x this way too.
local found, pkg = pcall(mason_reg.get_package, package_name)
if not found or not pkg:is_installed() then
return false
end
local installed_version
if mason_v2 then
-- the compiler will complain when Mason 1.x is in use
---@diagnostic disable-next-line: missing-parameter
installed_version = pkg:get_installed_version()
else
-- the compiler will complain when Mason 2.x is in use
---@diagnostic disable-next-line: param-type-mismatch
pkg:get_installed_version(function(success, version)
if success then
installed_version = version
end
end)
end
return installed_version == package_version
end
function M.is_outdated(packages)
for _, pkg in ipairs(packages) do
if not M.is_available(pkg.name, pkg.version) then
return true
end
if not M.is_installed(pkg.name, pkg.version) then
return true
end
end
end
function M.refresh_registry()
await(function(callback)
mason_reg.update(callback)
end)
end
function M.install_pkgs(packages)
log.info('check mason dependecies')
for _, dep in ipairs(packages) do
if not M.is_installed(dep.name, dep.version) then
local pkg = mason_reg.get_package(dep.name)
-- install errors if installation is already running in Mason 2.x
local guard
if mason_v2 then
-- guard if the package is already installing in Mason 2.x
-- the compiler will complain about the following line with Mason 1.x
---@diagnostic disable-next-line: undefined-field
guard = pkg:is_installing()
else
guard = false
end
if not guard then
pkg:install({
version = dep.version,
force = true,
})
end
end
end
end
return M