52 lines
1.7 KiB
Lua
52 lines
1.7 KiB
Lua
local M = {}
|
|
|
|
---@param pkg Package The mason package to install.
|
|
---@param version? string Pin the installation to a specific version.
|
|
local function install_package(pkg, version)
|
|
vim.notify(("[mason-conform] installing %s"):format(pkg.name))
|
|
|
|
pkg:install({ version = version }):once(
|
|
"closed",
|
|
vim.schedule_wrap(function()
|
|
if pkg:is_installed() then
|
|
vim.notify(("[mason-conform] %s was successfully installed"):format(pkg.name))
|
|
else
|
|
vim.notify(
|
|
("[mason-conform] failed to install %s. Installation logs are available in :Mason and :MasonLog"):format(
|
|
pkg.name
|
|
),
|
|
vim.log.levels.ERROR
|
|
)
|
|
end
|
|
end)
|
|
)
|
|
end
|
|
|
|
---Install the mason package if it exists in the registry and is not already
|
|
---installed or installing.
|
|
---@param mason_package_name string Mason package identifier, optionally `name@version`.
|
|
function M.try_install(mason_package_name)
|
|
local Package = require("mason-core.package")
|
|
local registry = require("mason-registry")
|
|
local package_name, version = Package.Parse(mason_package_name)
|
|
|
|
local ok, pkg = pcall(registry.get_package, package_name)
|
|
if not ok or pkg == nil then
|
|
vim.notify(
|
|
("[mason-conform] Formatter %q is not available in the mason registry. Make sure it is a valid formatter name, or add it to 'ignore_install' to silence this warning."):format(
|
|
package_name
|
|
),
|
|
vim.log.levels.WARN
|
|
)
|
|
return
|
|
end
|
|
|
|
if pkg:is_installed() or pkg:is_installing() then
|
|
return
|
|
end
|
|
|
|
install_package(pkg, version)
|
|
end
|
|
|
|
return M
|