Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion lazy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ return {
commit = '218c0c26c14d99feca778e4d13f5ec3e8b1b60f0',
},
{
'williamboman/mason.nvim',
'mason-org/mason.nvim',
-- opts = {
-- registries = {
-- 'github:nvim-java/mason-registry',
Expand Down
22 changes: 19 additions & 3 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,21 @@ 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
---@diagnostic disable-next-line: undefined-field
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 Expand Up @@ -51,7 +67,7 @@ function M.refresh_and_install(packages)
lazy.close_lazy_if_opened()

mason_ui.open()
notify.warn('Please close and re-open after dependecies are installed')
notify.warn('Please close and re-open after dependencies are installed')
end)

mason_util.refresh_registry()
Expand Down
50 changes: 33 additions & 17 deletions lua/java/startup/mason-registry-check.lua
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
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
-- compiler will complain when Mason 1.x is used
---@diagnostic disable-next-line: undefined-field
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.iter() 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
76 changes: 52 additions & 24 deletions lua/java/utils/mason.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +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)
local has_pkg = mason_reg.has_package(package_name)
-- 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 has_version = false

local pkg = mason_reg.get_package(package_name)
pkg:get_installed_version(function(success, version)
if success and version == package_version then
has_version = true
end
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 has_version
return installed_version == package_version
end

function M.is_installed(package_name, package_version)
local pkg = mason_reg.get_package(package_name)
local is_installed = pkg:is_installed()
-- 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 is_installed then
if not found or not pkg:is_installed() then
return false
end

local installed_version
pkg:get_installed_version(function(ok, version)
if not ok then
return
end

installed_version = version
end)
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 @@ -69,10 +85,22 @@ function M.install_pkgs(packages)
if not M.is_installed(dep.name, dep.version) then
local pkg = mason_reg.get_package(dep.name)

pkg:install({
version = dep.version,
force = true,
})
-- 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
Expand Down
2 changes: 1 addition & 1 deletion tests/prepare-config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ require('lazy').setup({
lazy = false,
},
{
'williamboman/mason.nvim',
'mason-org/mason.nvim',
lazy = false,
},
{
Expand Down