Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Mason 2.0 migration Pt. 2: backward compatibility with Mason 1.x
  • Loading branch information
logrusx committed Aug 5, 2025
commit cecd82a58afaa243f65c083adb7774687ddb9e1b
19 changes: 17 additions & 2 deletions lua/java/startup/mason-dep.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ local notify = require('java-core.utils.notify')
local async = require('java-core.utils.async')
local lazy = require('java.ui.lazy')
local sync = async.sync
local mason_v2 = require("mason.version").MAJOR_VERSION == 2

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

local M = {}

---Add custom registries to mason
---Add custom registries to Mason 1.x
---@param registries java.Config
function M.add_custom_registries(registries)
local function add_custom_registries_v1(registries)
local mason_default_config = require('mason.settings').current

local new_registries =
Expand All @@ -24,6 +25,20 @@ function M.add_custom_registries(registries)
})
end

---Add custom registries to Mason 2.x
---@param registries java.Config
local function add_custom_registries_v2(registries)
for _, reg in ipairs(registries) do
require("mason-registry").sources:prepend(reg)
end
end

if (mason_v2) then
M.add_custom_registries = add_custom_registries_v2
else
M.add_custom_registries = add_custom_registries_v1
end

---Install mason package dependencies for nvim-java
---@param config java.Config
function M.install(config)
Expand Down
47 changes: 31 additions & 16 deletions lua/java/startup/mason-registry-check.lua
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
local mason_source = require('mason-registry.sources')
local mason_v2 = require('mason.version').MAJOR_VERSION==2

local M = {
JAVA_REG_ID = 'github:nvim-java/mason-registry',
}
local mason_sources

if mason_v2 then
mason_sources = require('mason-registry').sources
else
mason_sources = require('mason-registry.sources')
end

local M = {}
if mason_v2 then
M.JAVA_REG_ID = 'nvim-java/mason-registry'
else
M.JAVA_REG_ID = 'github:nvim-java/mason-registry'
end

function M.is_valid()
local has_reg = false
local iterator

for reg in mason_source:iterate() do
if reg.id == M.JAVA_REG_ID then
has_reg = true
goto continue
end
if mason_v2 then
-- the compiler will complain when Mason 1.x is in use
---@diagnostic disable-next-line: undefined-field
iterator = mason_sources.iterate
else
-- the compiler will complain when Mason 2.x is in use
---@diagnostic disable-next-line: undefined-field
iterator = mason_sources.iter
end

::continue::

if has_reg then
return {
success = true,
continue = true,
}
for reg in iterator(mason_sources) do
if reg.id == M.JAVA_REG_ID then
return {
success = true,
continue = true
}
end
end

return {
Expand Down
50 changes: 44 additions & 6 deletions lua/java/utils/mason.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,60 @@ 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.0
-- 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 = pkg:get_installed_version()
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.0
-- 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 = pkg:get_installed_version()
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
Expand Down Expand Up @@ -56,8 +85,17 @@ function M.install_pkgs(packages)
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.0
if not pkg:is_installing() then
-- 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,
Expand Down