Skip to content

Commit 58c25cd

Browse files
logrusxs1n7ax
andauthored
feat: adds Mason 2.0 support (#402)
* feat: Mason 2.0 migration * feat: Mason 2.0 migration Pt. 2: backward compatibility with Mason 1.x * feat: Mason 2.0 migration Pt. 3: fixed formatting and typos, added missing diagnostic disable's * feat: Mason 2.0 migration Pt. 4: fixed formatting * chore: release 3.0.0 Release-As: 3.0.0 --------- Co-authored-by: s1n7ax <srineshnisala@gmail.com>
1 parent 0831149 commit 58c25cd

File tree

5 files changed

+106
-46
lines changed

5 files changed

+106
-46
lines changed

lazy.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ return {
1414
commit = '218c0c26c14d99feca778e4d13f5ec3e8b1b60f0',
1515
},
1616
{
17-
'williamboman/mason.nvim',
17+
'mason-org/mason.nvim',
1818
-- opts = {
1919
-- registries = {
2020
-- 'github:nvim-java/mason-registry',

lua/java/startup/mason-dep.lua

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ local notify = require('java-core.utils.notify')
66
local async = require('java-core.utils.async')
77
local lazy = require('java.ui.lazy')
88
local sync = async.sync
9+
local mason_v2 = require('mason.version').MAJOR_VERSION == 2
910

1011
local List = require('java-core.utils.list')
1112

1213
local M = {}
1314

14-
---Add custom registries to mason
15+
---Add custom registries to Mason 1.x
1516
---@param registries java.Config
16-
function M.add_custom_registries(registries)
17+
local function add_custom_registries_v1(registries)
1718
local mason_default_config = require('mason.settings').current
1819

1920
local new_registries =
@@ -24,6 +25,21 @@ function M.add_custom_registries(registries)
2425
})
2526
end
2627

28+
---Add custom registries to Mason 2.x
29+
---@param registries java.Config
30+
local function add_custom_registries_v2(registries)
31+
for _, reg in ipairs(registries) do
32+
---@diagnostic disable-next-line: undefined-field
33+
require('mason-registry').sources:prepend(reg)
34+
end
35+
end
36+
37+
if mason_v2 then
38+
M.add_custom_registries = add_custom_registries_v2
39+
else
40+
M.add_custom_registries = add_custom_registries_v1
41+
end
42+
2743
---Install mason package dependencies for nvim-java
2844
---@param config java.Config
2945
function M.install(config)
@@ -51,7 +67,7 @@ function M.refresh_and_install(packages)
5167
lazy.close_lazy_if_opened()
5268

5369
mason_ui.open()
54-
notify.warn('Please close and re-open after dependecies are installed')
70+
notify.warn('Please close and re-open after dependencies are installed')
5571
end)
5672

5773
mason_util.refresh_registry()

lua/java/startup/mason-registry-check.lua

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
1-
local mason_source = require('mason-registry.sources')
1+
local mason_v2 = require('mason.version').MAJOR_VERSION == 2
22

3-
local M = {
4-
JAVA_REG_ID = 'github:nvim-java/mason-registry',
5-
}
3+
local mason_sources
4+
5+
if mason_v2 then
6+
-- compiler will complain when Mason 1.x is used
7+
---@diagnostic disable-next-line: undefined-field
8+
mason_sources = require('mason-registry').sources
9+
else
10+
mason_sources = require('mason-registry.sources')
11+
end
12+
13+
local M = {}
14+
if mason_v2 then
15+
M.JAVA_REG_ID = 'nvim-java/mason-registry'
16+
else
17+
M.JAVA_REG_ID = 'github:nvim-java/mason-registry'
18+
end
619

720
function M.is_valid()
8-
local has_reg = false
21+
local iterator
922

10-
for reg in mason_source.iter() do
11-
if reg.id == M.JAVA_REG_ID then
12-
has_reg = true
13-
goto continue
14-
end
23+
if mason_v2 then
24+
-- the compiler will complain when Mason 1.x is in use
25+
---@diagnostic disable-next-line: undefined-field
26+
iterator = mason_sources.iterate
27+
else
28+
-- the compiler will complain when Mason 2.x is in use
29+
---@diagnostic disable-next-line: undefined-field
30+
iterator = mason_sources.iter
1531
end
1632

17-
::continue::
18-
19-
if has_reg then
20-
return {
21-
success = true,
22-
continue = true,
23-
}
33+
for reg in iterator(mason_sources) do
34+
if reg.id == M.JAVA_REG_ID then
35+
return {
36+
success = true,
37+
continue = true,
38+
}
39+
end
2440
end
2541

2642
return {

lua/java/utils/mason.lua

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,60 @@ local log = require('java.utils.log')
22
local mason_reg = require('mason-registry')
33
local async = require('java-core.utils.async')
44
local await = async.wait_handle_ok
5+
local mason_v2 = require('mason.version').MAJOR_VERSION == 2
56

67
local M = {}
78

89
function M.is_available(package_name, package_version)
9-
local has_pkg = mason_reg.has_package(package_name)
10+
-- get_package errors if the package is not available in Mason 2.x
11+
-- it works fine in Mason 1.x this way too.
12+
local has_pkg, pkg = pcall(mason_reg.get_package, package_name)
1013

1114
if not has_pkg then
1215
return false
1316
end
1417

15-
local has_version = false
16-
17-
local pkg = mason_reg.get_package(package_name)
18-
pkg:get_installed_version(function(success, version)
19-
if success and version == package_version then
20-
has_version = true
21-
end
22-
end)
18+
local installed_version
19+
if mason_v2 then
20+
-- the compiler will complain when Mason 1.x is in use
21+
---@diagnostic disable-next-line: missing-parameter
22+
installed_version = pkg:get_installed_version()
23+
else
24+
-- the compiler will complain when mason 2.x is in use
25+
---@diagnostic disable-next-line: param-type-mismatch
26+
pkg:get_installed_version(function(success, version)
27+
if success then
28+
installed_version = version
29+
end
30+
end)
31+
end
2332

24-
return has_version
33+
return installed_version == package_version
2534
end
2635

2736
function M.is_installed(package_name, package_version)
28-
local pkg = mason_reg.get_package(package_name)
29-
local is_installed = pkg:is_installed()
37+
-- get_package errors if the package is not available in Mason 2.x
38+
-- it works fine in Mason 1.x this way too.
39+
local found, pkg = pcall(mason_reg.get_package, package_name)
3040

31-
if not is_installed then
41+
if not found or not pkg:is_installed() then
3242
return false
3343
end
3444

3545
local installed_version
36-
pkg:get_installed_version(function(ok, version)
37-
if not ok then
38-
return
39-
end
40-
41-
installed_version = version
42-
end)
46+
if mason_v2 then
47+
-- the compiler will complain when Mason 1.x is in use
48+
---@diagnostic disable-next-line: missing-parameter
49+
installed_version = pkg:get_installed_version()
50+
else
51+
-- the compiler will complain when Mason 2.x is in use
52+
---@diagnostic disable-next-line: param-type-mismatch
53+
pkg:get_installed_version(function(success, version)
54+
if success then
55+
installed_version = version
56+
end
57+
end)
58+
end
4359

4460
return installed_version == package_version
4561
end
@@ -69,10 +85,22 @@ function M.install_pkgs(packages)
6985
if not M.is_installed(dep.name, dep.version) then
7086
local pkg = mason_reg.get_package(dep.name)
7187

72-
pkg:install({
73-
version = dep.version,
74-
force = true,
75-
})
88+
-- install errors if installation is already running in Mason 2.x
89+
local guard
90+
if mason_v2 then
91+
-- guard if the package is already installing in Mason 2.x
92+
-- the compiler will complain about the following line with Mason 1.x
93+
---@diagnostic disable-next-line: undefined-field
94+
guard = pkg:is_installing()
95+
else
96+
guard = false
97+
end
98+
if not guard then
99+
pkg:install({
100+
version = dep.version,
101+
force = true,
102+
})
103+
end
76104
end
77105
end
78106
end

tests/prepare-config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ require('lazy').setup({
5050
lazy = false,
5151
},
5252
{
53-
'williamboman/mason.nvim',
53+
'mason-org/mason.nvim',
5454
lazy = false,
5555
},
5656
{

0 commit comments

Comments
 (0)