Skip to content

Commit 0edb02c

Browse files
authored
feat: auto refresh the mason registory when pkgs are not available (nvim-java#41)
1 parent 29e6318 commit 0edb02c

File tree

3 files changed

+119
-59
lines changed

3 files changed

+119
-59
lines changed

lua/java.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ function M.setup(custom_config)
1414
local config = vim.tbl_deep_extend('force', global_config, custom_config)
1515

1616
nvim_dep.check()
17-
mason_dep.install(config)
1817

19-
setup_wrap.setup(config)
20-
decomple_watch.setup()
18+
local is_installing = mason_dep.install(config)
2119

22-
dap.setup_dap_on_lsp_attach()
20+
if not is_installing then
21+
setup_wrap.setup(config)
22+
decomple_watch.setup()
23+
dap.setup_dap_on_lsp_attach()
24+
end
2325
end
2426

2527
----------------------------------------------------------------------

lua/java/startup/mason-dep.lua

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,54 @@
11
local log = require('java.utils.log')
2-
local mason_reg = require('mason-registry')
32
local mason_ui = require('mason.ui')
3+
local mason_util = require('java.utils.mason')
44
local notify = require('java-core.utils.notify')
5+
local async = require('java-core.utils.async')
6+
local sync = async.sync
57

68
local M = {}
79

8-
local dependecies = {
9-
{ name = 'jdtls', version = 'v1.29.0' },
10-
{ name = 'java-test', version = '0.40.1' },
11-
{ name = 'java-debug-adapter', version = '0.52.0' },
12-
}
13-
1410
---Install mason package dependencies for nvim-java
1511
---@param config java.Config
1612
function M.install(config)
17-
log.info('check mason dependecies')
18-
19-
local is_installing = false
20-
21-
if config.jdk.auto_install then
22-
table.insert(dependecies, { name = 'openjdk-17', version = '17.0.2' })
23-
end
24-
25-
for _, dep in ipairs(dependecies) do
26-
if not M.is_installed(dep.name, dep.version) then
27-
log.info('installing mason pkg: ' .. tostring(dep.name))
28-
29-
is_installing = true
30-
31-
local pkg = mason_reg.get_package(dep.name)
32-
33-
pkg:install({
34-
version = dep.version,
35-
force = true,
36-
})
37-
end
13+
local packages = M.get_pkg_list(config)
14+
local is_outdated = mason_util.is_outdated(packages)
15+
16+
if is_outdated then
17+
sync(function()
18+
M.refresh_and_install(packages)
19+
end)
20+
.catch(function(err)
21+
notify.error('Failed to setup nvim-java ' .. tostring(err))
22+
log.error('failed to setup nvim-java ' .. tostring(err))
23+
end)
24+
.run()
3825
end
3926

40-
if is_installing then
41-
mason_ui.open()
42-
notify.warn(
43-
'Please restart the editor after dependency installation is done'
44-
)
45-
end
27+
return is_outdated
4628
end
4729

48-
---Returns true if the package and its expected version is already installed
49-
---@private
50-
---@param pkg_name string name of the package
51-
---@param expc_version string expected version of the package
52-
---@return boolean true if the package and its version is already installed
53-
function M.is_installed(pkg_name, expc_version)
54-
local pkg = mason_reg.get_package(pkg_name)
55-
56-
if not pkg:is_installed() then
57-
return false
58-
end
30+
function M.refresh_and_install(packages)
31+
vim.schedule(function()
32+
mason_ui.open()
33+
notify.warn('Please close and re-open after dependecies are installed')
34+
end)
5935

60-
---@type string | nil
61-
local installed_version
36+
mason_util.refresh_registry()
37+
mason_util.install_pkgs(packages)
38+
end
6239

63-
pkg:get_installed_version(function(ok, version)
64-
if ok then
65-
installed_version = version
66-
end
67-
end)
40+
function M.get_pkg_list(config)
41+
local dependecies = {
42+
{ name = 'jdtls', version = 'v1.29.0' },
43+
{ name = 'java-test', version = '0.40.1' },
44+
{ name = 'java-debug-adapter', version = '0.52.0' },
45+
}
6846

69-
if installed_version == expc_version then
70-
return true
47+
if config.jdk.auto_install then
48+
table.insert(dependecies, { name = 'openjdk-17', version = '17.0.2' })
7149
end
7250

73-
return false
51+
return dependecies
7452
end
7553

7654
return M

lua/java/utils/mason.lua

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
local log = require('java.utils.log')
2+
local mason_reg = require('mason-registry')
3+
local async = require('java-core.utils.async')
4+
local await = async.wait_handle_ok
5+
6+
local M = {}
7+
8+
function M.is_available(package_name, package_version)
9+
local has_pkg = mason_reg.has_package(package_name)
10+
11+
if not has_pkg then
12+
return false
13+
end
14+
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)
23+
24+
return has_version
25+
end
26+
27+
function M.is_installed(package_name, package_version)
28+
local pkg = mason_reg.get_package(package_name)
29+
local is_installed = pkg:is_installed()
30+
31+
if not is_installed then
32+
return false
33+
end
34+
35+
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)
43+
44+
return installed_version == package_version
45+
end
46+
47+
function M.is_outdated(packages)
48+
for _, pkg in ipairs(packages) do
49+
if not M.is_available(pkg.name, pkg.version) then
50+
return true
51+
end
52+
53+
if not M.is_installed(pkg.name, pkg.version) then
54+
return true
55+
end
56+
end
57+
end
58+
59+
function M.refresh_registry()
60+
await(function(callback)
61+
mason_reg.update(callback)
62+
end)
63+
end
64+
65+
function M.install_pkgs(packages)
66+
log.info('check mason dependecies')
67+
68+
for _, dep in ipairs(packages) do
69+
if not M.is_installed(dep.name, dep.version) then
70+
local pkg = mason_reg.get_package(dep.name)
71+
72+
pkg:install({
73+
version = dep.version,
74+
force = true,
75+
})
76+
end
77+
end
78+
end
79+
80+
return M

0 commit comments

Comments
 (0)