Skip to content

Commit

Permalink
feat: add the ability to do conditional installing (WhoIsSethDaniel#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lei Wang committed Jan 18, 2025
1 parent c5e07b8 commit e3656b4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ require('mason-tool-installer').setup {
-- you can turn off/on auto_update per tool
{ 'bash-language-server', auto_update = true },

-- you can do conditional installing
{ 'gopls', condition = function() return not os.execute("go version") end },
'lua-language-server',
'vim-language-server',
'gopls',
'stylua',
'shellcheck',
'editorconfig-checker',
Expand Down
72 changes: 39 additions & 33 deletions lua/mason-tool-installer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,48 +146,54 @@ local check_install = function(force_update, sync)
end
local ensure_installed = function()
for _, item in ipairs(SETTINGS.ensure_installed or {}) do
local name, version, auto_update
local name, version, auto_update, condition
if type(item) == 'table' then
name = item[1]
version = item.version
auto_update = item.auto_update
condition = item.condition
else
name = item
end
if mlsp then
name = mlsp.get_mappings().lspconfig_to_mason[name] or name
end
if mnls then
name = mnls.getPackageFromNullLs(name) or name
end
if mdap then
name = mdap.nvim_dap_to_package[name] or name
end
local p = mr.get_package(name)
if p:is_installed() then
if version ~= nil then
p:get_installed_version(function(ok, installed_version)
if ok and installed_version ~= version then
do_install(p, version, on_close)
else
vim.schedule(on_close)
end
end)
elseif
force_update or (force_update == nil and (auto_update or (auto_update == nil and SETTINGS.auto_update)))
then
p:check_new_version(function(ok, version)
if ok then
do_install(p, version.latest_version, on_close)
else
vim.schedule(on_close)
end
end)

if condition ~= nil and not condition() then
vim.schedule(on_close)
else
if mlsp then
name = mlsp.get_mappings().lspconfig_to_mason[name] or name
end
if mnls then
name = mnls.getPackageFromNullLs(name) or name
end
if mdap then
name = mdap.nvim_dap_to_package[name] or name
end
local p = mr.get_package(name)
if p:is_installed() then
if version ~= nil then
p:get_installed_version(function(ok, installed_version)
if ok and installed_version ~= version then
do_install(p, version, on_close)
else
vim.schedule(on_close)
end
end)
elseif
force_update or (force_update == nil and (auto_update or (auto_update == nil and SETTINGS.auto_update)))
then
p:check_new_version(function(ok, version)
if ok then
do_install(p, version.latest_version, on_close)
else
vim.schedule(on_close)
end
end)
else
vim.schedule(on_close)
end
else
vim.schedule(on_close)
do_install(p, version, on_close)
end
else
do_install(p, version, on_close)
end
end
end
Expand Down

0 comments on commit e3656b4

Please sign in to comment.